Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm using the following script

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>my.script.keeprunning</string>
    <key>KeepAlive</key>
    <true/>
    <key>ProgramArguments</key>
    <array>
       <string>/path/to/file</string>
    </array>
</dict>
</plist>

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

2 Answers 2

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 APPLICATION.app"
                                        -- 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" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>some.label</string>
  <key>ProgramArguments</key>
  <array>
    <string>lsappinfo</string>
    <string>launch</string>
    <string>launchandhide=true</string>
    <string>nofront=true</string>
    <string>/Applications/Mail.app</string>
  </array>
  <key>StartInterval</key>
  <integer>60</integer> <!-- run the program every 60 seconds -->
</dict>
</plist>

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/Mail.app. 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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.