macos - 如何启动 AppleScript 应用程序并从另一个 AppleScript 运行 shell 脚本?

标签 macos shell applescript

这是我的文件:

启动器.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 编辑器运行“launcher.applescript”
  • 听“起始脚本”

  • 实际行为:
  • 通过在查找器中手动打开来运行“shell-script-launcher.app”会产生预期的行为
  • 运行“launcher.applescript”会打开“shell-script-launcher.app”,但它永远不会执行shell脚本。

  • 我尝试将应用程序另存为“仅运行”和“保持打开状态”。还是没有进展。你有什么建议吗。最终结果必须是应用程序而不是 Applescript。

    最佳答案

    从概念上讲:

  • run命令启动并运行隐藏的应用程序
  • activate命令启动、运行和激活应用程序(使其成为最前面的应用程序)
  • launch , according to Apple , “如果应用程序尚未运行,则启动该应用程序,但不会向其发送运行命令。”
  • 对于基于 AppleScript 的应用程序,这应该意味着它们已加载,但未执行(即,不调用它们的 - 隐式或显式 - on run 处理程序),但实际上在 10.9 之前并非如此 - 见下文。
  • (对我而言)不清楚这对于非基于 AppleScript 的应用程序究竟意味着什么


  • 这是苹果的方法 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先指挥。

  • 选项
  • @user309603 的 务实的解决方案简单地使用 do shell script与标准open实用程序 绕过问题 - 这应该有效,无论应用程序是否基于 AppleScript:

  • do shell script "open " & ¬
        quoted form of POSIX path of ¬
        alias ":path:to:applescript:apps:shell-script-launcher.app"
    
  • 如果您 了解应用类型 你在前面调用:
  • 运行 基于 AppleScript 应用程序:最好使用 run script file ,正如@regulus6633 所建议的那样 - 这具有额外的优势,即被调用的基于 AppleScript 的应用程序可以将对象直接返回给调用者:


  • run script file ":path:to:applescript:apps:shell-script-launcher.app"
    
    注意:还有 load script file ,这确实让您只加载脚本代码而不立即执行它。
  • 运行 非 AppleScript 应用程序:使用 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
    
  • 最后,在 OSX <= 10.9 上,您可以尝试 只需使用 launch命令 (虽然这对 OP 不起作用,可能是因为在 <= 10.7 OSX 版本上工作):

  •  launch application ":path:to:applescript:apps:shell-script-launcher.app"
    
    然而,那是 不可取 有两个原因:
  • 在 OSX 10.10 中,Apple 修复了 launch行为也不再执行,因此您的代码在那里运行时会中断。
  • 虽然非 AppleScript 应用程序在使用 launch 调用时通常会运行(隐藏) , documentation says AppleScript“不向它发送运行命令”和“允许您在不执行其通常的启动程序(例如打开新窗口)的情况下打开应用程序” - 这究竟意味着什么尚不清楚,不同的应用程序似乎对此有不同的处理方式。
  • 关于macos - 如何启动 AppleScript 应用程序并从另一个 AppleScript 运行 shell 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22693509/

    相关文章:

    swift - 存档的应用程序不显示窗口内容,在 Xcode 上运行时工作正常

    objective-c - 键值编码和对多关系

    macos - Osx:核心音频:使用AudioToolbox解析原始的压缩音频数据(以获取PCM)

    swift - 迭代类型数组时出现 "Use of undeclared type "错误

    linux - 对httpd感到困惑

    applescript - 通过 Applescript 打开 URL 并激活 Google Chrome

    javascript - Applescript : wait that the page is load on chrome

    linux - 从 haskell 程序中运行 vi(处理 ptys)

    linux - 如何在 Shell 脚本中测试 ant 命令的输出?

    macos - 在 OSX Mavericks 中使用 AppleScript 的非 GUI 应用程序的辅助功能设置?