如何在脚本运行时保持应用程序激活/在前台? [英] How to keep an application activated/in foreground while a script is running?
问题描述
我正在使用 Applescript 从 Xcode 4 自动部署应用程序.为此,我使用系统事件单击菜单栏中的菜单项.我让整个事情都在工作,但有一个怪癖.如果用户在我的脚本运行时点击其他地方,即 XCode 4 窗口退出前景,我的整个脚本就会失败.有没有办法从我的脚本中强制 Xcode 处于前台?
I'm using Applescript to automate deployment of applications from Xcode 4. I use System Events to click the menu items in the menu bar for that. I have the whole thing working, but with one quirk. If the user clicks elsewhere while my script is running, that is the XCode 4 window goes out of foreground, my entire script fails. Is there a way to force Xcode to be in the foreground from my script?
现在,如果只有 Xcode 4 至少与 Xcode 3 一样可编写脚本,我就不必求助于 GUI 自动化了.
Now, if only Xcode 4 was at least as scriptable as Xcode 3, I wouldn't have to resort to GUI automation.
推荐答案
mcgrailm 和 James Bedford 的答案组合奏效.我将编辑方案..."的菜单单击循环,直到编辑方案表变得存在.我还必须在单击Run without building"之前激活应用程序.
A combination of mcgrailm and James Bedford's answers worked. I put the menu click for "Edit Schemes…" in a loop until the edit scheme sheet becomes existent. I also had to activate the application just before I clicked "Run without building".
代码:
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
这篇关于如何在脚本运行时保持应用程序激活/在前台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!