How can I run/stop/relaunch an application automatically, at boot/login/some other time? How do you make a script or app to run or stop running automatically when the system is booted, a user logs on, at a specific time, when a disk is attached, etc.?
Gui Method

If you want something to run when a user logs in, the easiest way is to use the GUI. You can


Go to the System Prefs &gt; Accounts &gt; Login Items screen, and add the item there by

clicking the + symbol
dragging and dropping the .app onto the pane

In the dock, bring up the context menu for an app, select Options, then select Open at Login


Launchd method

If you want to launch something that is not a .app, or you want to have more control over launching it, such as:


at a certain time or at a specified interval
continuously re-launching if it crashes
not related to user login
as a function of network access
only run when another process is running 
when a file is added to a folder
etc.


then you want to use the technical solution that is launchd.

The easiest way to set up a launchd config file is to use a tool to help. At the time of writing the most popular method is Lingon (which has an older free version available from SourceForge), or the PList Website tool.

Hand Coding

If you don't want to use Lingon or the PList Website and only want to use the raw tools available on the system, you can write your own launchd plist by hand

Here is my launchd script to run SomeApp continuously after the system boots (independent of a user logging in). It is in /System/Library/LaunchAgents/ and called SomeApp.restart.plist. If it is run based on a user login, it could be stored in ~/Library/LaunchAgents/

&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
&lt;plist version="1.0"&gt;
&lt;dict&gt;
        &lt;key&gt;RunAtLoad&lt;/key&gt;  
        &lt;true/&gt;
        &lt;key&gt;KeepAlive&lt;/key&gt;
        &lt;true/&gt;
        &lt;key&gt;Label&lt;/key&gt;
        &lt;string&gt;SomeApp.restart&lt;/string&gt;
        &lt;key&gt;ProgramArguments&lt;/key&gt;
        &lt;array&gt;
                &lt;string&gt;/path/to/SomeApp.app/Contents/MacOS/SomeApp&lt;/string&gt;
        &lt;/array&gt;
        &lt;key&gt;StartCalendarInterval&lt;/key&gt;
        &lt;dict&gt;
            &lt;key&gt;Hour&lt;/key&gt;
            &lt;integer&gt;5&lt;/integer&gt;
            &lt;key&gt;Minute&lt;/key&gt;
            &lt;integer&gt;10&lt;/integer&gt;
       &lt;/dict&gt;
&lt;/dict&gt;
&lt;/plist&gt;


Load it once with

launchctl load ~/Library/LaunchAgents/SomeApp.restart.plist



The RunAtLoad option will launch the application the first time launchctl runs this.
The KeepAlive option will re-launch the application if it crashes.
The StartCalendarInterval will run it at a certain time. Presumably this is mutually exclusive with the previous two, but is included to show what can be done with launchd
Launchctl will run this after reboots.


There are many additional options that can be found by experimenting with Lingon or by reading the man pages for launchctl, launchd.plist, plist, launchd.conf, etc.