我可以强制Applecript同步运行吗?

时间:2017-10-24 21:09:54

标签: docker scripting applescript iterm2

我正在为iTerm2编写一个简单的applescript脚本,它会生成一堆标签并开始在其中运行服务(我有很多微服务,所有都需要运行才能在本地测试)。

事情大部分都在发挥作用,但是我遇到了一些有点奇怪的行为,我认为这些行为与提前发送命令的AppleScript有关。让我们看一个具体的例子:

create tab with default profile   
tell the current session
  write text "cd services/myservice"
  write text "make build-docker"
  write text "make run-docker"
end tell

理论上,这个块应该

1)创建一个新标签 2)转换到新目录 3)构建一个泊坞窗图像和 4)运行该docker镜像。

这种情况偶尔会有效,但更常见的是我在第4步遇到了问题。具体来说,我只会检查标签,然后找出' make build-docker'是最后一个命令运行。这个命令需要一些时间,所以我假设" make run-docker"在构建运行时发送并被忽略。有没有办法迫使applescript / iTerm2等待这个命令完成,以便正确执行运行?

希望这很清楚。谢谢你的阅读!

1 个答案:

答案 0 :(得分:2)

如果您启用了iTerm的shell集成,则可以轮询is at shell prompt以确定命令是否已完成:

tell application "iTerm2"
  tell current window
    create tab with profile "BentoBox"
    tell current session
        write text "sleep 5"
        repeat while not (is at shell prompt)
           delay 0.5
       end repeat
       write text "sleep 5"
    end tell
  end tell
end tell

否则,您需要将所有命令串在一起告诉:

write text "cd services/myservice; make build-docker; etc; etc; etc.."

或者将它们放在shell脚本中并执行:

write text "my_super_duper_shell_script.sh"

或者使用AppleScript将cmds写入tmp文件并执行:

re:How can I create or replace a file using Applescript?