The first 3 of these can be easily accomplished with a small bit of shell scripting:
This one is technically AppleScript being run as a shell command:
osascript -e 'tell application "System Events" to set visible of process "Safari" to false'
Replace "Safari" with your app of choice.
Show App IFF it is already running
APP='BBEdit' ; pgrep -qx "$APP" && osascript -e "tell application \"$APP\" to activate"
Replace 'BBEdit' with your app of choice.
Launch App if Not Running
APP='Disk Utility' ; pgrep -qx "$APP" || open -g -j -a "$APP"
Note what the
-j arguments to
open will do:
-g Do not bring the application to the foreground.
-j Launches the app hidden.
Remove one or both if you do not want those features.
Also note that some apps do not respect
-j. These apps are to be considered to be of poor quality (often "cross-platform" apps which have not been given sufficient attention for their Mac port).
Send [cmd+n] if no windows open
I'm not sure how to do this one without Keyboard Maestro, but in Keyboard Maestro it would be simple:
- Activate 'App'
- Pause until 'App' is in the foreground
- If no front window, press ⌘N