这是我的文件:
启动器.applescript
tell application ":path:to:applescript:apps:shell-script-launcher.app" to launch
shell-script-launcher.app [AppleScript,另存为应用程序]
do shell script "say starting script"
期望的行为:
实际行为:
我尝试将应用程序另存为“仅运行”和“保持打开状态”。还是没有进展。你有什么建议吗。最终结果必须是应用程序而不是 Applescript。
最佳答案
从概念上讲:
run
命令启动并运行隐藏的应用程序 activate
命令启动、运行和激活应用程序(使其成为最前面的应用程序)launch
, according to Apple , “如果应用程序尚未运行,则启动该应用程序,但不会向其发送运行命令。”on run
处理程序),但实际上在 10.9 之前并非如此 - 见下文。 这是苹果的方法 thinks it works with AppleScript-based applications , 即 仅从 OSX 10.10 (Yosemite) 开始为真 :
A script can send commands to a script application just as it can to other applications. To launch a non-stay-open application and run its script, use a launch command followed by a run command, like this:
launch application "NonStayOpen"
run application "NonStayOpen"
The launch command launches the script application without sending it an implicit run command. When the run command is sent to the script application, it processes the command, sends back a reply if necessary, and quits.
OSX 10.8、10.9 上的错误行为(在 OSX 10.10 中已修复):
launch
本身就足以运行应用程序,并且确实是唯一适用于基于 AppleScript 的应用程序的命令 .任何执行
run
的尝试或 activate
(无论是否除了 launch
之外)运行应用程序 - 从 AppleScript 编辑器运行时甚至两次(!;只有一次 osascript
) - 但报告失败 <appName> got an error: Connection is invalid
.这让我印象深刻 错误 .
不确定 OSX 版本 <= 10.7 的行为方式。
注意:我目睹了
launch
的非执行行为一次,但我在 OS X 10.9.2 和 OS X 10.8.5 上从头开始创建的每个非保持打开的基于 AppleScript 的测试应用程序也使用 launch
执行脚本- 与文档所说的相矛盾。如果您的系统行为不同和/或旧版本的行为方式,请告诉我。在哪个 OSX 版本上不使用
launch
执行的应用程序创建?上 OSX 10.10 , 行为与文档一致 ,有一点值得注意:
run application
够用 - 不需要单独的 launch application
先指挥。 选项
do shell script
与标准open
实用程序 绕过问题 - 这应该有效,无论应用程序是否基于 AppleScript:do shell script "open " & ¬
quoted form of POSIX path of ¬
alias ":path:to:applescript:apps:shell-script-launcher.app"
run script file
,正如@regulus6633 所建议的那样 - 这具有额外的优势,即被调用的基于 AppleScript 的应用程序可以将对象直接返回给调用者:run script file ":path:to:applescript:apps:shell-script-launcher.app"
注意:还有 load script file
,这确实让您只加载脚本代码而不立即执行它。run
/activate
运行隐藏/最前面的应用程序:run application ":path:to:applescript:apps:shell-script-launcher.app"
run
即使使用基于 AppleScript 的应用程序,只需 忽略错误 与 try ... end try
,正如@atonus 所建议的那样 - 缺点是您将无法检测到调用应用程序的实际失败。 您可以通过选择性地仅忽略特定的
Connection invalid
来缓解这种情况。错误(假设此错误不会合法发生)[在 OSX 10.10 上不再需要]:try
run application "Macintosh HD:Applications:_Sandbox-AppleScript0.app"
on error number -609 # 'Connection is invalid' error that is spuriously reported
# simply ignore
end try
launch
命令 (虽然这对 OP 不起作用,可能是因为在 <= 10.7 OSX 版本上工作): launch application ":path:to:applescript:apps:shell-script-launcher.app"
然而,那是 不可取 有两个原因:launch
行为也不再执行,因此您的代码在那里运行时会中断。 launch
调用时通常会运行(隐藏) , documentation says AppleScript“不向它发送运行命令”和“允许您在不执行其通常的启动程序(例如打开新窗口)的情况下打开应用程序” - 这究竟意味着什么尚不清楚,不同的应用程序似乎对此有不同的处理方式。 关于macos - 如何启动 AppleScript 应用程序并从另一个 AppleScript 运行 shell 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22693509/