Domanda

Can Login Items be added via a command in terminal?

I would like to add a few essential utilities like ShiftIt and Alfred to my Login Items via a script when I'm configuring a new Mac. In versions of macOS previous to High Sierra these items were stored in a plist in ~/Library/Preferences/com.apple.loginitems.plist but since High Sierra has been released it seems they've been migrated to be loaded via a new mechanism.

I am interested in ways to set Login Items from a terminal primarily for High Sierra but interested from an academic perspective to know how it could be done in prior releases of macOS.

È stato utile?

Soluzione

macOS Catalina Update: While the information in this answer holds true as stated and tested in the versions of OS X/macOS listed below, it does not work in macOS Catalina 10.15.


I tested the following under macOS 10.14.6, macOS 10.13.1 and OS X 10.8.6.

In Terminal, using the following syntax:

osascript -e 'tell application "System Events" to make login item at end with properties {path:"/path/to/itemname", hidden:false}'
  • Note: If there are spaces in a path filename, do not escape the spaces with a backslash (\) when it is in quotes.

Tested with an application named EventScripts:

osascript -e 'tell application "System Events" to make login item at end with properties {path:"/Applications/EventScripts.app", hidden:false}'

It returned:

login item UNKNOWN

Which is apparently the normal output with this command and was verified by preforming the same action in Script Editor.

Then looking in System Preferences > Users & Groups > $USER > Login Items, EventScripts was added.


Notes:

In OS X 10.8.6 it updated the following file:

~/Library/Preferences/com.apple.loginitems.plist

In macOS 10.13.1[1] it updated the following file:

~/Library/Application Support/com.apple.backgroundtaskmanagementagent/backgrounditems.btm

Which, by the way, is also a plist file, but with a btm extension.

You can also delete a Login Item from Terminal using the following syntax:

osascript -e 'tell application "System Events" to delete login item "name"'

Where "name" in the command is as show in the output of the following command:

osascript -e 'tell application "System Events" to get the name of every login item'

[1] Update: The backgrounditems.btm file is still the target file since macOS 10.13.1 and later.

Altri suggerimenti

On Catalina & beyond the path to Applications has changed, so, adding Login Items might require using something like "/System/Applications/Awesome App.app"

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a apple.stackexchange
scroll top