Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using the following script

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
<plist version="1.0">

to run an app even when it crashes, but after system restarts it the focus switches to that specific app. How can I improve the script to run the app hidden or minimized? Would it be easier to write applescript for it and run it as service?

Thank you for your suggestions ;)

share|improve this question

I have an script that solves a similar issue, and here is a modified version that has been tested on several different applications.

on idle
    tell application "System Events"
        set pName to the name of every process
        if pName does not contain "MY APPLICATION" then
            do shell script "open -g /Applications/MY"
                                        -- or wherever your application is
        else if pName contains "MY APPLICATION" then
            set makeFalse to visible of window 1 of application "MY APPLICATION"
            if makeFalse is true then set visible of window 1 of application "MY APPLICATION" to false
        end if
    end tell
end idle

Note that this script should be saved as a "stay open" application, and can be added to your list of start up items so that it loads automatically when you restart your system.

Once it's running, the script will do the following:

  • Check to the running processes to see if your application is open.
  • If your app isn't among the list, it will open it in the background with the do shell script "open -g command.
  • If the application is open, it will hide the main window.
  • The on idle handler will automatically check every 30 seconds. If you want to have a longer delay, you can add return and then the number of seconds you would like the script to wait before checking again (e.g. return 300 for five minutes). This should be added before end idle

I use this information to hide the dock icon for the app and have it run completely in the background.

share|improve this answer

For example save this plist as ~/Library/LaunchAgents/some.label.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">
  <integer>60</integer> <!-- run the program every 60 seconds -->

Then run launchctl load ~/Library/LaunchAgents/some.label.plist.

You could also use open -jga Mail or pgrep -qx Mail||open -jga Mail instead of lsappinfo launch launchandhide=true nofront=true /Applications/ open -jg opens an application hidden and without making it frontmost, except that for some applications like Mail and TextEdit, it creates a new visible window if the application is running but has no open windows. For those applications, you can use pgrep to check if the application is already open.

lsappinfo was added in 10.9. open -j and pgrep were added in 10.8.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .