我怀疑您收到此消息的原因是因为每次您使用 osascript 从命令行调用脚本时,都会编译脚本。
编译的行为告诉应用程序据我所知会使应用程序启动。
使用预编译文件中的 osascript 从命令行调用脚本,即scpt不会导致此行为,因为无需进行编译。
但是从纯文本(.txt、.sh)文件调用它将会启动应用程序。
如果您不想使用 .scpt 文件并想使用纯文本文件,那么您可以尝试将运行脚本applescript 中的命令。
on is_running(appName)
tell application "System Events" to (name of processes) contains appName
end is_running
set safRunning to is_running("Safari")
if safRunning then
run script "tell application \"Safari\"
open location \"http://google.com\"
end tell"
return "Running"
else
return "Not running"
end if
该脚本在运行脚本仅在需要时才编译。您需要转义任何字符,例如我的示例中的引号。
如果您首先在普通的 applescript 文档中编写脚本并编译它以检查错误,则会更容易。
然后将其复制到纯文本文件中。
UPDATE **
我上面使用的方法来自我在回答这里之前一段时间用来解决这个问题的旧脚本。
答案是有效的,并且并不是试图变得优雅。 ;-)
我其实喜欢用户1804762 method https://stackoverflow.com/a/16069999/1107226以下。因为它确实有效,但感觉答案不够清楚,所以我将给出一个使用它的例子。
set appName to "Safari"
if application appName is running then
tell application id (id of application appName)
open location "http://google.com"
end tell
return "Running"
else
return "Not running"
end if
该脚本可以从命令行运行奥萨脚本
example:
osascript /Users/USERNAME/Desktop/foo.scpt
请注意,该脚本被保存为已编译的脚本。这将正常工作,您也可以将其保存并用作纯文本脚本。
i.e.
osascript /Users/USERNAME/Desktop/foo.applescript