Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Adding and removing individual login items from Terminal System
As shown in this hint, there is a way to use the defaults command-line tool to add items to the Login Items list of the Accounts or Users & Groups System Preference panel:
defaults write loginwindow AutoLaunchedApplicationDictionary -array-add '{ "Path" = "/path/to/itemname"; "Hide" = 0; }'
Unfortunately, Apple provides no easy way to use defaults to remove a given login item from the list. While you can remove all login items by deleting AutoLaunchedApplicationDictionary, that's usually not what you want to do.

Luckily, there's another way to do it that does allow removal of individual items as well as other things.

A nice work-around solution is to instead use the osascript command-line tool to do it with simple AppleScript commands:

Add an item:
osascript -e 'tell application "System Events" to make login item at end with properties {path:"/path/to/itemname", hidden:false}' 
Remove an item:
osascript -e 'tell application "System Events" to delete login item "itemname"' 
List all items:
osascript -e 'tell application "System Events" to get the name of every login item'
Enjoy!

[crarko adds: Note that the easiest way to get the path to the login item 'itemname' may be to open System Preferences » Accounts, click the 'Login Items' tab and then right-click (Ctrl+click) the name of the item. This displays a 'Reveal in Finder' submenu and you can determine the path from that.]
    •    
  • Currently 3.00 / 5
  You rated: 5 / 5 (4 votes cast)
 
[18,950 views]  

Adding and removing individual login items from Terminal | 4 comments | Create New Account
Click here to return to the 'Adding and removing individual login items from Terminal' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Adding and removing individual login items from Terminal
Authored by: llee on Dec 27, '11 10:14:57AM

Here's an AppleScript to deal with login items already in the list.

--Run the script

--Select one or more login items from the list and click OK.

--Choose whether to reveal the login item in the Finder or to remove it from the current login items.

--See if it works for you! (Tip: try revealing 'em first and if you don't really want 'em removed after you test removing, just open login items in the Accounts (or Users and Groups) System prefpane and drag 'em back in there!)


tell application "System Events"

activate

set loginItems to name of every login item

choose from list loginItems with multiple selections allowed

end tell

set theResult to result

if theResult is false then

tell me to quit

else

activate

if (count of items of theResult) is not 0 then

if (count of items of theResult) is 1 then

display dialog "What do you want to do with" & space & every item of theResult & "?" buttons {"Cancel", "Reveal it", "Remove it"}

else

display dialog "What do you want to do with the selected login items?" buttons {"Cancel", "Reveal them", "Remove them"}

end if

if button returned of result contains "Remove" then

repeat with i in theResult

tell application "System Events"

delete (every login item whose name is (i as text))

end tell

end repeat

else if button returned of result contains "Reveal" then

repeat with i in theResult

set target_string to (do shell script "defaults read loginwindow | grep Path | grep" & space & (quoted form of i))

set this_offset to offset of "\"" in target_string

set x to text (this_offset + 1) thru -3 of target_string

tell application "Finder" to set home_path to (POSIX path of (path to "cusr"))

set actual_item to replace_chars(x, "~/", home_path)

set g to POSIX file actual_item

tell application "Finder"

activate

reveal g

end tell

end repeat

end if

end if

end if


on replace_chars(this_text, bahad, goohood)

set AppleScript's text item delimiters to the bahad

set the item_list to every text item of this_text

set AppleScript's text item delimiters to the goohood as string

set this_text to the item_list as string

set AppleScript's text item delimiters to ""

return this_text

end replace_chars



[ Reply to This | # ]
Adding and removing individual login items from Terminal
Authored by: joelprestonsmith on Dec 31, '11 04:02:39PM

This would be great if it worked! I just tested it on an 82-page TextEdit document (which OpenOffice says contains 38,000 plus words). The script says the document has 606 words. That's not terribly useful. It's possible this is operator. I'm new to Applescripts, and don't trust myself. But 38K is a long way from 606.



[ Reply to This | # ]
Adding and removing individual login items from Terminal
Authored by: llee on Jan 02, '12 01:42:35PM

How is your comment related to login items?



[ Reply to This | # ]
Adding and removing individual login items from Terminal
Authored by: BlaatMekker on Jan 06, '12 01:34:35PM
I just found a way to remove individual items from a dict. I haven't tested it on anything that I didn't need to, though it should also work for this hint. To remove a line:

/usr/libexec/PlistBuddy -c "Delete :Java:JVMArchs:0" /Applications/Minecraft.app/Contents/Info.plist
The relevant part of the Info.plist before modification was:

<dict>
        <key>Java</key>
        <dict>
                <key>JVMArchs</key>
                <array>
                        <string>x86_64</string>
                        <string>i386</string>
                        <string>ppc</string>
                </array>
        </dict>
</dict>
It's easy to guess what happens after running this command. To add an item, this command is used:

/usr/libexec/PlistBuddy -c "Add :Java:JVMArchs:0 string 'x86_64'" /Applications/Minecraft.app/Contents/Info.plist
For more info, just find the PlistBuddy man-page. This tool may possibly only be available when you have Xcode installed.

[ Reply to This | # ]