1

我对 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

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

4

1 回答 1

5
on run
    log "Something"
end run

log "Something"

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

此脚本不起作用,因为您不能在处理程序中拥有处理程序

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

但是,您可以在“运行时”之外定义一个处理程序并从内部调用它:

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

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

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
于 2012-06-25T13:14:39.080 回答