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 > Accounts > 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/
<?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>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>Label</key>
<string>SomeApp.restart</string>
<key>ProgramArguments</key>
<array>
<string>/path/to/SomeApp.app/Contents/MacOS/SomeApp</string>
</array>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>5</integer>
<key>Minute</key>
<integer>10</integer>
</dict>
</dict>
</plist>
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.
You can stop the item from launching with launchctl disable gui/$UID/bundle-id
.
If you don't know the bundle ID, you can find it by typing launchctl list
. That will list all processes running under your user, with their PIDs in the first column. So, if you saw this:
$ launchctl list
- 0 com.apple.SafariHistoryServiceAgent
277 0 com.apple.Finder
- 0 com.apple.quicklook
- 0 com.apple.parentalcontrols.check
[…]
189 0 com.example.ExampleDaemon
- 0 com.apple.java.InstallOnDemand
…you could use Activity Monitor to confirm that the unwanted app's PID is 189, which makes its bundle ID com.example.ExampleDaemon
. Then:
launchctl disable gui/$UID/com.example.ExampleDaemon
That's it. There is still a record of the login item on disk, just disabled. If you want to purge it, or just want to learn more, read on.
Counterintuitively (but amusingly?), the list is saved here:
/var/db/com.apple.xpc.launchd/disabled.$UID.plist
That file will look something like this:
<?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>com.example.ExampleDaemon</key>
<false/>
</dict>
</plist>
Yes, the false
means that an app with that bundle id should be launched at startup. After running launchctl disable
, the false
will change to a true
. (As far as I can tell, launchd doesn't write these files right away, so you may not see this until you reboot.) You can see the disabled items with this command:
launchctl print-disabled gui/$UID
There's also some extra info about each login item stored here, but removing it won't stop the item from launching:
/var/db/com.apple.xpc.launchd/loginitems.$UID.plist
Any changes you make while the system is running will be overwritten when you shut down/restart. So, as far as I can tell, the only way to completely remove an item from disabled.$UID.plist
is to reboot in single user mode and make the edits there. Even without having run launchctl disable
, removing a key
/false
pair from this file stops the app from launching at login. I'm not going to include instructions for editing these files in single user mode in this answer.
Best Answer
After several attempts I finally found the file that is created during the installation process. So to prevent AnyDesk from starting at login, you need to delete the file,
That solved my issues with autostarting AnyDesk at login.