How to keep an application activated/in foreground while a script is running?

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?

Now, if only Xcode 4 was at least as scriptable as Xcode 3, I wouldn’t have to resort to GUI automation.

  • Does iOS 7 support Applescript?
  • Appium on real iOS devices
  • Setting copyright statement on a per-project basis?
  • Automating TestFlightApp with Xcode and Provisioning Portal
  • Tell AppleScript To Build XCode Project
  • Quit app after window is closed in AppleScript
  • 3 Solutions Collect From Internet About “How to keep an application activated/in foreground while a script is running?”

    You can just use the activate command for every click call to ensure that the application is in the foreground. It’s not ideal. Really if you’re going to use System Events for scripting input like this you have to just accept that the user can’t really use the computer whilst the script is running!

    If you can break the script down into parts that require user input and parts that don’t, you could present a dialog to the user saying something like, “Are you ready to continue with the script? You’ll have to leave your computer for a while!”
    … and then when it’s finished, “Feel free to use your computer again now!”
    This might make the script a little less obtrusive. Just a suggestion.

    you should put up some of loading image or something while it is running also anytime you call a gui you should run a loop with a timeout that activates the app then checks for the existence of the items you want to click then when it exists click it and get out of the loop

    EDIT

    helpful link

    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”.

    The code:

    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