Skip to main content
Question

Removing login items for users via a bash script

  • July 16, 2015
  • 3 replies
  • 11 views

michael_rowland
Forum|alt.badge.img+1

I am needing to remove some obsolete login items from our classroom machines

I'm trying the following

#!/bin/bash -v
exec 2>&1

sudo -u $3 /usr/bin/osascript -e 'tell application "System Events" to delete login item "PCClient"'
sudo -u $3 /usr/bin/osascript -e 'tell application "System Events" to delete login item "fthf02.op.ac.nz"'

It works fine running it manually on a client but once I run it as a login policy I get

Script result: 
sudo -u $3 /usr/bin/osascript -e 'tell application "System Events" to delete login item "PCClient"'
43:53: syntax error: A class name can’t go after this identifier. (-2740)
sudo -u $3 /usr/bin/osascript -e 'tell application "System Events" to delete login item "fthf02.op.ac.nz"'
43:53: syntax error: A class name can’t go after this identifier. (-2740)

Any ideas what I'm doing wrong?

3 replies

Forum|alt.badge.img+11
  • Contributor
  • July 16, 2015

Have you tried adding in a sleep 30? Perhaps it is running too quickly at login.


bpavlov
Forum|alt.badge.img+18
  • Esteemed Contributor
  • July 16, 2015
#!/bin/bash

loggedInUser=$( ls -l /dev/console | awk '{print $3}' )
loggedInPID=$( ps -axj | awk "/^$loggedInUser/ && /Dock.app/ {print $2;exit}" )

/bin/launchctl bsexec "${loggedInPID}" sudo -u "${loggedInUser}" /usr/bin/osascript -e 'tell application "System Events" to delete login item "PCClient"'

Give this script a try. I got this a few months ago from a similar question someone else posed.


Forum|alt.badge.img+10
  • New Contributor
  • July 16, 2015

bpavlov: Be sure to test this technique under El Capitan. There might be some changes that affect this technique...