Like Lauri mentioned, open -g
doesn't always work, like with Evernote or NVAlt, etc. Her suggestion to use launch
in AppleScript works, but like she says, it makes everything come to the foreground. You can add a few lines to her AppleScript to make it so each dock program is hidden immediately after it's launched.
repeat with bundleID in paragraphs of (do shell script "defaults read com.apple.dock persistent-apps | sed -E -n 's/.*\"bundle-identifier\" = \"(.*)\";/\\1/p'")
launch application id bundleID
set programName to name of application id bundleID
tell application "Finder"
set visible of process programName to false
end tell
end repeat
This works for the first time you run the script, like right after you log in or turn on your computer. However, if you run it later on, when some of your programs are already running, it'll hide everything.
I suppose you could include some sort of check that would only hide programs that were actually launched, ignoring any applications that are already open. But I sadly lack the AppleScript chops to do that. Hrm.
Actually, launch
seems to usually open document-based applications on the background, but it raises windows from applications like iTunes if they weren't already open.
The process names aren't always the same as the application names. For example the process name of Firefox is firefox
in lowercase.
Dashboard, Growl, Launchpad, nvALT, Time Machine, The Unarchiver, and XQuartz weren't opened on the background with open -gj
. My coding style might be a bit questionable, but this should work with all of them, except XQuartz, which would need a longer delay.
#!/bin/bash
IFS=$'\n'
for a in $(comm -23 <(defaults read com.apple.dock persistent-apps | sed -E -n 's/.*"bundle-identifier" = "(.*)";/\1/p' | grep -Ev '^()$' | sort) <(sort <<< "com.apple.exposelauncher
com.apple.launchpad.launcher
com.apple.dashboardlauncher
com.apple.backup.launcher
$(osascript -e 'set text item delimiters to linefeed
tell app "System Events"
bundle identifier of processes as text
end' | grep -v 'missing value')" | uniq -u)); do
(open -gj -b "$a" && sleep 1 && osascript -e "tell app \"System Events\" to set visible of (processes where bundle identifier is \"$a\") to false")&
done
There are many cases where open -g
doesn't actually open applications on the background.
<ul>
<li>Notational Velocity and Mail are not opened on the background if they weren't already open</li>
<li>Alfred is not opened on the background if it was already open</li>
<li>Finder and Reeder windows are always raised above other windows</li>
</ul>
The launch
command in AppleScript is a bit better, but it also makes many applications frontmost if they weren't open before.
repeat with a in paragraphs of (do shell script "defaults read com.apple.dock persistent-apps | sed -E -n 's/.*\"bundle-identifier\" = \"(.*)\";/\\1/p'")
launch application id a
end repeat
But thanks for pointing out the -j
flag. I had missed it before, because it's only shown in the help message and not on the man page. open -jg
would be better than either open -g
or launch
, but even that doesn't always work with nvALT or Alfred.