Script to remove item from login items?

cghigliotty
New Contributor

Hi all.

On our current company image, we have a login item set that launches an application (eTime) upon login. We are now moving away from this application entirely and would like to push a script to end users to remove this login item and the eTime application. Anyone have any suggestions on script we can use to execute this?

6 REPLIES 6

mm2270
Legendary Contributor III

Hi.
You should technically be able to just delete the application and be done with it, with no need to touch the Login Items list. It will leave an orphaned entry in the plist but won't show any errors when users log in. The OS is smart enough to simply ignore orphaned entries in Login Items when logging in.

That said, if you really want to clean this up and remove it, while its pretty easy to use either defaults or PlistBuddy to add items into the login items plist, its not as easy to remove them with those tools. The most effective way I've found is through Applescript, or an osascript command in a shell script

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

The thing is, while this will work for the logged in user, it won't do anything for other user accounts. And even at that, you very likely need to direct this to the logged in user's space when its run from a Casper Suite policy or it may be trying to do this to the root account. You can try something like the following. Not tested so this may still fail with an error:

#!/bin/sh

loggedInUser=$( ls -l /dev/console | awk '{print $3}' )

sudo -u "$loggedInUser" /usr/bin/osascript -e 'tell application "System Events" to delete login item "itemname"'

You have to make sure you enter the correct 'item name' as it appears in System Preferences.

hopefully that helps put you on the right track.

jacob_salmela
Contributor II

I have used the following script to remove all of the login items from a specific user and then only add in the ones that I need. It could be modified to seek out a specific app.

#!/bin/bash
osascript -e <<EOF 'tell application "system events" 
delete login items
make login item at end with properties {path:"/Applications/Spotify.app",kind:application, hidden:true}
end tell'
EOF

I have used the following variable to get the currently logged in user:

currentUser=$(stat -f "%Su" /dev/console)

ronb
New Contributor II

So I must be missing something right in front of me. I'm trying to remove ClamXav from a Yosemite OS with the following script -

#!/bin/bash
#kills ClamXav processes
  killall clamd
  killall ClamXav Sentry
#Removes ClamXav application and system components
  rm -R /Applications/ClamXav.app
  rm -R /usr/local/clamXav
  rm -R /usr/lib/libclamunrar_iface.so
  rm -R /private/var/root/Library/Preferences/uk.co.markallan.clamxav.runfreshclam.plist
#Removes ClamXav sentry from users login
osascript -e 'tell application "System Events" to delete login item "ClamXav Sentry"'

it all seems to work but the "osascript" line. This command seems to be commonly used with other admins, but when I run this from a "Recurring Check-in" policy, I keep getting - "17:18: syntax error: Expected expression, property or key form, etc. but found unknown token. (-2741) error."
I'm logged in as a regular user and watching for the login item to disappear in System Preferences. But a logout policy had the same problem.

talkingmoose
Moderator
Moderator

I realize my opinion is coming after the fact on this thread, but I feel this is worth adding for folks going forward.

First, @mm2270 is correct that simply removing the application should be enough. Yes, each user will have an Unknown entry in his or her Login Items list but that's the worst outcome. Modifying a user's Login Items is an added risk that's not worth the effort.

Best practice and good admin philosophy are to leave a user's files to the user. That includes documents, preferences, login items and anything else in home folders. They belong to the user not the admin. It's OK (and sometimes necessary) to set defaults by writing to plists in a home folder but once a user touches them then leave them alone.

Administer settings with configuration profiles at the user or computer level or use launchd agents to open applications when a user logs in to the computer. None of these options touches user files.

This phrase from 2010: Odyssey Two comes to mind:

ALL THESE WORLDS ARE YOURS—EXCEPT EUROPA ATTEMPT NO LANDING THERE

@ronb, the issue you may be facing is you're running your script as your Casper admin or root account instead of each user's account. You haven't mentioned how you're calling it.

ronb
New Contributor II

talkingmoose,

I appreciate your logic, and can go with that. Probably a bit old school thinking, or just OCD trying to clean house more than necessary.
And you're spot on with the account business. I've not developed scripting in regards to user level settings before (other than user level AppleScript solutions) when utilizing the Casper $3 parameter. So this is the source of my problem I'm sure.

Thanks for the input.

bpavlov
Honored Contributor

I would just point out one thing. There may be some login items that you do not necessarily want set as login items. And so from that perspective it may be ideal to remove those login items. But @talkingmoose has provided a pretty good rule of thumb to go by.