【问题标题】:How to keep an application activated/in foreground while a script is running?如何在脚本运行时保持应用程序激活/在前台?
【发布时间】:2011-03-29 14:00:48
【问题描述】:

我正在使用 Applescript 从 Xcode 4 自动部署应用程序。 我使用系统事件来单击菜单栏中的菜单项。我有整个工作,但有一个怪癖。如果用户在我的脚本运行时单击其他地方,即 XCode 4 窗口退出前台,我的整个脚本将失败。有没有办法强制 Xcode 从我的脚本中显示在前台?

现在,如果只有 Xcode 4 至少与 Xcode 3 一样可编写脚本,我就不必求助于 GUI 自动化了。

【问题讨论】:

    标签: xcode automation applescript xcode4


    【解决方案1】:

    您可以只对每个click 调用使用activate 命令,以确保应用程序处于前台。这并不理想。真的,如果您要使用系统事件来编写这样的脚本输入,您必须接受用户在脚本运行时不能真正使用计算机!

    如果您可以将脚本分解为需要用户输入的部分和不需要用户输入的部分,您可以向用户显示一个对话框,说“您准备好继续执行脚本了吗?您必须暂时离开你的电脑!” ...然后当它完成时,“现在可以再次使用您的计算机了!” 这可能会使脚本不那么突兀。只是一个建议。

    【讨论】:

    • 我确实试过这个。我认为它只需要点击几下,对其他人无效。我再试一次。
    • 您可能需要在激活和单击之间设置一些小的延迟,因为 AppleScript 可能会超前。这在使用这种脚本编写方法时很常见。
    【解决方案2】:

    您应该在它运行时放置一些加载图像或其他东西,无论何时调用 gui,您都应该运行一个带有超时的循环,以激活应用程序,然后检查您想要单击的项目是否存在,然后当它存在点击它并退出循环

    编辑

    helpful link

    【讨论】:

    • 检查存在然后激活是个好主意。如果异常处理和恢复执行,我可以做一些事情吗?
    【解决方案3】:

    mcgrailm 和 James Bedford 的答案相结合奏效了。 我将“编辑方案...”的菜单单击放在一个循环中,直到编辑方案表存在。 我还必须在单击“运行而不构建”之前激活应用程序。

    代码:

    tell application id "com.apple.dt.Xcode"
        activate
    end tell
    tell application "System Events"
        tell process "Xcode"
                repeat until sheet 1 of window 2 exists
                        click menu item "Edit Scheme…" of menu "Product" of menu bar item "Product" of menu bar 1
                        tell application "Xcode"
                                activate
                                beep
                        end tell
                end repeat
                tell sheet 1 of window 2
                        set destination to pop up button 2 of group 1
                        click destination
                        set target to "iPad 4.3 Simulator"
                        click menu item target of destination's menu 1
                        set buildConfig to pop up button 3 of group 2
                        click buildConfig
                        click menu item "Debug" of menu 1 of buildConfig
                        click button "OK"
                end tell
                tell application "Xcode"
                        activate
                        beep
                end tell
                tell application id "com.apple.dt.Xcode"
                        activate
                end tell
                click menu item "Run Without Building" of menu 1 of menu item "Perform Action" of menu "Product" of menu bar item "Product" of menu bar 1
        end tell
    end tell
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多