【问题标题】:Make Shell script to wait until applescript finishes?让Shell脚本等到applescript完成?
【发布时间】:2017-03-06 02:55:00
【问题描述】:

我有一个调用 AppleScript 的 shell 脚本。 AppleScript 在具有给定文档的软件上运行一些自动化测试。

AppleScript 完成执行后,我将在我的 shell 脚本中使用 mv 命令将文档移动到其他文件夹,然后我将移动下一个文档,以便 AppleScript 可以使用这个新文档运行这些测试。但是,一旦调用 AppleScript,它就会将文档移动到另一个文件夹,而不会在该文档上运行这些测试。

如何在我的 shell 脚本中等待,以便只有在 AppleScript 执行完所有测试后才移动文档?

1. mv $file "/path/to/file" (Moving the document to the execution folder)
2.  osascript /pathto/applescript.app (it will use this execution folder to run its tests)
3.  mv "/path/to/file" /Users/Desktop/tempfolder (moving the document on which the test is completed to a temp folder)

步骤 2 和 3 一个接一个地进行,无需等待,因此要在其上运行测试的文档在测试完成之前被移动。

【问题讨论】:

  • 为什么是 -1 。请告诉我,如果需要,我可以修改任何内容
  • 可能投反对票的人希望看到一些“代码”,我的意思是您口头解释的任何内容,以及您尝试解决问题的一些“代码”。
  • 等待可以解决问题,但它不是一个好的解决方案,因为测试可能需要更多时间来处理特定文件。我试过 & 但它不会工作,因为即使 applescipt 在后台运行它也不会有运行测试所需的文件。
  • 这个带有答案的最新问题 (stackoverflow.com/questions/40215226/…) 看起来也很适合您的情况
  • 这是我在评论中提到的。我无法在后台运行它,因为一旦执行下一步,即第 3 步,文件就会被移动,而第 2 步没有要处理的文件。

标签: shell unix applescript


【解决方案1】:

通常,shell 命令按顺序执行。也就是说,下一条语句在当前语句执行完毕之前不会运行,无论它是否成功。因此,如果是这种情况,您可以只编写一个又一个命令的脚本。

我猜osascript /pathto/applescript.app 在不同的进程中运行一些任务,并且 osascript 在执行“你想要的”的进程仍在运行时从它自己的执行中返回。

你可以做的是ps -aux | grep applescript.app然后从中获取pid,然后执行一个while循环来查看程序何时停止。但这似乎是过度设计的,而且您不知道 applescript.app 的执行名称。

如果我在某处错了,请任何人纠正我

【讨论】:

    【解决方案2】:

    正如 Mayerz 所说,执行 shell 脚本命令正在等待指令结束以移动到下一个 AppleScript 命令。在任何情况下,您也可以使用“考虑应用程序响应”块。这是一个在 Finder 中移动的示例:

    set A to ("myVolume:Users:Me:Documents:sample.mov") as alias
    set B to ("Users:me:Desktop:") as alias
    tell application "Finder"
        considering application responses
            move A to B
        end considering
    end tell
    display dialog "done"
    

    与 do shell 脚本类似的例子:

    set A to ("myVolume:Users:Me:Documents:sample.mov") as alias
    set B to path to desktop
    set AU to POSIX path of A
    set BU to (POSIX path of B) & "test2.mov"
    tell application "Finder"
        considering application responses
            do shell script "mv " & (quoted form of AU) & " " & (quoted form of BU)
        end considering
    end tell
    display dialog "done"
    

    【讨论】:

    • 我之前也想过这样做,但我找不到正确的语法。如果假设我只想移动那些扩展名为 .mov 的文件,我必须在您建议的解决方案中添加哪些内容。我尝试使用 *.mov 但它没有用。
    • 要获取所有的 *.mov,解决方案将取决于您使用 Finder 方法还是 steel script 方法。我很容易使用 Finder 方法,因为您可以使用类似“获取文件夹 xxx 的每个文件,其扩展名为“mov”。
    • “考虑应用程序响应”也没有解决问题。一旦测试被applescript触发,它仍然会跳转到move命令。
    • 哪个测试?没有测试。您的“mv”命令应该在考虑.../结束考虑块内。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多