14

I have an applescript to initiate my work environment, but have a small quibble with it. I want the script to launch several programs, and then hide them, once they started. The code looks like this currently:

tell application "Firefox" to activate

delay 0.5

tell application "Finder"
  set visible of process "Firefox" to false
end tell

Obviously, delay 0.5 is just a placeholder, ideally I would want to hide the program as soon as it finished loading. Unfortunately, my load times vary a lot (from 0.2 - 5s) Is there something like a callback or a function to monitor the events of applications?

Christian Macht
  • 485
  • 1
  • 5
  • 16
  • Why not just call `open -aw Firefox` and then hide the app using AppleScript? – slhck Sep 03 '12 at 19:11
  • @slhck Sorry, I don't understand. Parameter `-a` requires a specific app to open with, and `-W` waits until the app that was open is closed. My understanding from `man open` is that it has no options that might be useful for this? – Christian Macht Sep 03 '12 at 19:39
  • Whoops, sorry, I misunderstood that. – slhck Sep 03 '12 at 19:45

2 Answers2

9

Query the visibility status in a loop, repeating to set it to invisible until it works:

set appname to "Firefox"
tell application appname to launch
tell application "System Events"
    repeat until visible of process appname is false
        set visible of process appname to false
    end repeat
end tell

Monitoring the event log of AppleScript Editor, it's obvious that it might take a few tries; the following was repeated 1490 times when I tried it with Xcode:

set visible of process "Xcode" to false
get visible of process "Xcode"
    --> true

Before it finally worked:

set visible of process "Xcode" to false
get visible of process "Xcode"
    --> false
Daniel Beck
  • 109,300
  • 14
  • 287
  • 334
  • Great, this works! I had to add a delay before System Events performs its checks or it would return --> true before Firefox even opened (slow machine ;) ). However, is there no "smoother" way of the app calling back once it finished loading? – Christian Macht Sep 03 '12 at 19:42
  • It didn't work for me either. The visibility gets set to true again once Firefox finishes opening. – Lri Sep 04 '12 at 13:21
2

You don't usually need to add any delays, but in this case even if you set the visibile property to false, it gets set back to true after the application finishes opening. So you can't check its value or if the process exists.

You could use launch or open -jg to open an application without visible windows. launch opens a new window if the application wasn't open before. open -jg opens a new window if the application is open but has no visible windows.

set b to "com.apple.TextEdit"
tell application "System Events"
    if bundle identifier of processes contains b then
        launch application id b
    else
        do shell script "open -jgb " & b
    end if
end tell

A few applications like Alfred, Growl, nvALT, The Unarchiver, and X11 didn't work with either of them. You might just need to add a fixed delay before setting visible to false.

Lri
  • 40,894
  • 7
  • 119
  • 157