【问题标题】:wait for command line program execution before issuing new action in AppleScript在 AppleScript 中发出新操作之前等待命令行程序执行
【发布时间】:2016-12-28 02:09:01
【问题描述】:

我对 AppleScript 还是很陌生。我需要一个打开 3 个 iTerm 选项卡并分别执行 3 个命令行程序的脚本。第一个程序终止,而后两个程序不确定地运行。

这是我所拥有的:

tell application "iTerm"
    activate
    set next to (make new terminal)
    tell next
        activate current session
        launch session "Default Session"
        tell the last session
            set name to "vagrant-db"
            write text "cd ~/Workspace/vagrant-db; vagrant up"
        end tell

        launch session "Default Session"
        tell the last session
            set name to "next/core"
            write text "cd ~/Workspace/next"
            write text "/usr/local/bin/sbt \"project core\" \"run\""
        end tell

        launch session "Default Session"
        tell the last session
            set name to "next/web"
            write text "cd ~/Workspace/next"
            write text "/usr/local/bin/sbt \"project web\" \"~re-start\""
        end tell

    end tell
end tell

问题是我需要等待第一个命令行操作结束(vagrant 启动),然后再发出第二个和第三个。有没有办法做到这一点?

【问题讨论】:

    标签: applescript


    【解决方案1】:

    不确定是否可行,但可能使用 applescript 中的“do shell script”命令,例如

    set response to do shell script "ls"
    

    将返回根文件夹的内容。

    另一种方式(但非常难看)是使用“延迟”。例如

    delay 5
    

    会延迟5秒

    【讨论】:

    • do shell script 似乎被阻塞了..但是它没有在终端上给我任何输出,这不是很理想..有没有办法重复类似“阅读终端上的最后一行“?这样当我读到“成功”之类的内容时,我可以继续启动 2 个程序..
    【解决方案2】:

    用类似的方法解决

                --first program exec
    
                set a to 0
                repeat until (a = 1)
                    if (text of current session contains "ready") then
                        set a to 1
                    end if
                end repeat
    
                --second program exec
                --third program exec
    

    【讨论】:

    • 所以你用上面的答案“类似”来阅读文本?当我尝试使用该语法时,我不断收到The action “Run AppleScript” encountered an error.。你能在上下文中发布文本检查吗?
    【解决方案3】:

    Rusty 的回答对我有用。我添加此答案是为了提供 fusio 要求的详细信息(抱歉,我宁愿将其作为评论提供,但与添加新答案相比,cmets 需要更多的声誉点数)。

    Rusty 的回答要求已经在与当前窗口对话的上下文中。

    on wait_for(str)
        tell application "iTerm"
            tell current window
                set a to 0
                repeat until (a = 1)
                    if (text of current session contains str) then
                        set a to 1
                    end if
                end repeat
            end tell
        end tell
    end wait_for
    

    然后,在您的代码中,调用:

    my wait_for("Provisioners marked to run always will still run.")
    

    上述论点出现在“就绪”之后。

    这仍然很丑陋,但它有效。

    【讨论】:

      猜你喜欢
      • 2017-08-11
      • 1970-01-01
      • 2019-12-31
      • 1970-01-01
      • 1970-01-01
      • 2013-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多