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