在运行argv和其他处理程序

时间:2012-06-25 09:18:39

标签: applescript

我对applecript很新,我无法超越从命令行接受参数的观点。我读过我可以这样做:

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,而它允许我定义任意数量的函数(除{{1}之外) })。怎么样?

1 个答案:

答案 0 :(得分: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