Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically run at startup on Mac OS X?

Tags:

How do I programmatically set an application bundle on Mac OS X to run when the user logs in?

Basically, the equivalent of the HKCU\Software\Microsoft\Windows\CurrentVersion\Run registry key in Windows.

like image 442
Jake Petroules Avatar asked Jul 28 '10 23:07

Jake Petroules


People also ask

Can we manage auto start of default application in Mac?

The simplest way to disable an app from launching on startup is from the Dock. Right-click on the app and hover over Options in the menu. Apps that are set to open automatically will have a check mark next to Open at Login. Click that option to uncheck it and disable it from opening.


2 Answers

You can add the application to the user's "Login Items" (under System Preferences=>Accounts=[user]) or you can add a launchd agent to the user's ~/Library/LaunchAgents folder (see man launchd.plist). Use ~/Library/LaunchDaemons/ if your app has no user-facing UI. As others point out, launchd gives you a lot of control over when the app starts, what happens if the app quits or crashes, etc. and is most appropriate for "daemon" style apps (with our without UI).

The first option (Login Items) can be manipulated programmatically (link from Gordon).

like image 132
Barry Wark Avatar answered Sep 19 '22 14:09

Barry Wark


A working example below.

Create a file

~/Library/LaunchAgents/my.everydaytasks.plist

With contents:

<?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.everydaytasks</string>     <key>ProgramArguments</key>     <array>         <string>/Applications/EverydayTasks.app/Contents/MacOS/EverydayTasks</string>     </array>     <key>ProcessType</key>     <string>Interactive</string>     <key>RunAtLoad</key>     <true/>     <key>KeepAlive</key>     <false/> </dict> </plist> 

See the original post that helped me to make this example:

https://superuser.com/a/229792/43997

To test you need to run this in terminal

launchctl load -w ~/Library/LaunchAgents/my.everydaytasks.plist 

To unload

launchctl unload -w ~/Library/LaunchAgents/my.everydaytasks.plist 

See also

https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man5/launchd.plist.5.html

The is the other way of adding your application to starup using "Login Items". See this example project on how to implement it:

https://github.com/justin/Shared-File-List-Example

like image 45
Dmitriy Avatar answered Sep 22 '22 14:09

Dmitriy