在运行argv和其他处理程序

pis*_*hio 1 applescript

我对applescript很新,我不能超越从命令行接受参数的观点.我读过我可以这样做:

on run argv
    log "SOMETHIG
end run

on readLines(unixPath)
    set targetFile to (open for access (POSIX file unixPath))
    set fileText to (read targetFile for (get eof targetFile) as text)
    set fileLines to every paragraph of fileText

    close access targetFile
    return fileLines
end readLines
Run Code Online (Sandbox Code Playgroud)

问题在于它不允许我on run argv与其他函数(或处理程序)一起定义,on而它允许我定义任意数量的函数(除了on run argv).怎么会?

ada*_*one 5

on run
    log "Something"
end run
Run Code Online (Sandbox Code Playgroud)

log "Something"
Run Code Online (Sandbox Code Playgroud)

做同样的事.第一个脚本具有显式运行处理程序,而第二个脚本具有隐式运行处理程序.脚本对象只能有一个运行处理程序.

这个脚本不起作用,因为你不能在处理程序中有一个处理程序

on run 
    log "SOMETHIG"

    on readLines(unixPath)
        set targetFile to (open for access (POSIX file unixPath))
        set fileText to (read targetFile for (get eof targetFile) as text)
        set fileLines to every paragraph of fileText
        close access targetFile
        return fileLines
    end readLines

end run
Run Code Online (Sandbox Code Playgroud)

但是,您可以在"运行中"之外定义处理程序并从内部调用它:

on run
    log "Something"
    readLines("/Users/pistacchio/Desktop/test.txt")
end run

on readLines(unixPath)
    set targetFile to (open for access (POSIX file unixPath))
    set fileText to (read targetFile for (get eof targetFile) as text)
    set fileLines to every paragraph of fileText

    close access targetFile
    return fileLines
end readLines
Run Code Online (Sandbox Code Playgroud)

或者,您可以简单地使用隐式运行处理程序:

log "Something"
readLines("/Users/pistacchio/Desktop/test.txt")

on readLines(unixPath)
    set targetFile to (open for access (POSIX file unixPath))
    set fileText to (read targetFile for (get eof targetFile) as text)
    set fileLines to every paragraph of fileText

    close access targetFile
    return fileLines
end readLines
Run Code Online (Sandbox Code Playgroud)