4 Replies Latest reply: Oct 9, 2009 3:02 PM by Caramby
Caramby Level 4 Level 4 (1,515 points)
Really easy one — forgive me!

When launching apps by AppleScript, how do you keep them in the background, & not take focus away from other apps that may be running at the same time?

IOW, is there a simple AppleScript command that keeps apps hidden when performing commands?

(Said it was easy…)

Dual 1.8 GHz PowerPC G5, Mac OS X (10.5.8), Logic Studio 8.0.2, Occam's Razor Pro
  • Niel Level 10 Level 10 (272,320 points)
    Use code such as:

    tell application "System Events"
    set visible of process "Safari" to false
    end tell

    The application will need to be open and visible briefly. If it's in the front when the script is run, the previous application will be in the front afterwards.

    (47938)
  • Caramby Level 4 Level 4 (1,515 points)
    Very helpful — I added these lines & the application was indeed hidden. Unfortunately, the script (which is running as a droplet app) is batch processing files, & these continue to open new windows (one at a time) which remain visible.

    That's much better than what was happening (i.e., constant interruptions!), but it would be even better if the entire batch was processed invisibly. Otherwise it's still a bit distracting.

    I tried making the script hidden as well (thinking that perhaps these windows belonged to the script), but it didn't like that since the application is in fact System Events.

    Anyway, already this is a massive improvement — so thanks!
  • Camelot Level 8 Level 8 (46,300 points)
    When launching apps by AppleScript, how do you keep them in the background, & not take focus away from other apps that may be running at the same time?


    That depends on how you launch them.

    AppleScript has two direct commands to launch an application in addition to indirect commands which will open an app if it's not already running):

    activate -- bring an application frontmost and launch it if it's not already running
    launch -- open the application in the background

    So you should try using 'launch' instead of 'activate'.

    The windows will still be visible, but it will launch behind the frontmost app so you don't lose focus. Note though that some commands (especially System Events and UI scripting commands) require the application to be frontmost so you should look at what you're trying to do once it's open to see if it truly is viable to run in the background.
  • Caramby Level 4 Level 4 (1,515 points)
    Very intriguing. I was indeed using 'activate' — & substituting 'launch' (& also removing the 'visible is false' lines again) had exactly the same effect as the first solution.

    IOW, each of the two proposed solutions produces the same result; although changing one word is more efficient than adding three lines.

    Being entirely new to AppleScript, I had attempted an Automator Workflow first, which did work; but this is better. I think you are probably right, & the Windows do need to be opened, which means they must be visible, which means this may be the best possible solution.

    It's slightly distracting having them open every few seconds, but at least now I don't have to keep pressing Command-Tab all the time — which is a bit confusing when you're trying to compose an email or something!