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

Can I force applescript to run synchronously?

我正在为 iTerm2 编写一个简单的 applescript 脚本,它生成一堆选项卡并在其中启动 运行ning 服务(我有很多微服务,并且都需要 运行在本地测试)。

一切正常,但我遇到了一些稍微奇怪的行为,我认为这与 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) 构建一个 docker 图像并 4) 运行 docker 图片。

这偶尔会奏效,但我 运行 更频繁地遇到第 4 步的问题。具体来说,我将检查选项卡只是为了发现 'make build-docker' 是最后一个命令 运行.此命令需要一些时间,所以我假设 "make run-docker" 在构建 运行ning 时发送并被忽略。有没有办法强制 applescript/iTerm2 等待此命令完成,以便正确执行 运行?

希望这是清楚的。感谢阅读!

如果您启用了 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 将命令写入 tmp 文件并执行:

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