Remember Casper is going to be running your scripts as the root user, not the locally logged in user... you may need to create a variable for the currently logged in user and then use a "sudo -c" to get it done...
I have tried
#!/bin/sh
loggedInUser=$( ls -l /dev/console | awk '{print $3}' )
sudo -u "$loggedInUser" /usr/bin/osascript -e 'tell application "System Events" to delete login items'
Same error.
Hey Everyone,
I have seen this before when using Applescripts to migrate end user's exchange server settings in Outlook. I actually had to save my Applescript as an application, and then run the open command to have the user open it. I think this is due to some of the newer sandbox environments OS X has added since 10.7. So, I would save the Applescript as an app and toss it in my scripts folder in my CasperShare. Then run something like this:
#!/bin/bash
currentUser=$(ls -l /dev/console | awk '{ print $3 }')
sudo -u ${currentUser} open /Volumes/CasperShare/Scripts/myapplescript.app
exit 0
The code would execute as that end user, and it would run like an app instead of an actual Applescript. This is how I got around the whole run as root and sandboxed environments of OS X.
Hope this helps you,
Tom
fails out with the following error
Running script Clear Login Items.sh...
Script exit code: 0
Script result: LSOpenURLsWithRole() failed with error -10810 for the file /Users/Shared/DeleteLoginItems.app.
Hi msardes,
This is what I did and I just double checked this in my test environment:
Open Applescript Editor and create this script:
tell application "System Events"
delete login items
end tell
Then export that script to an application, via the export menu. I have a local test account on my Macbook Pro named, "Tom Waits." The shortname is 'tomwaits,' and I fast user switched to that user, opened up a terminal session, switched the terminal session to the root user, and opened the app from my home folder, as root, but as the currently logged in user:
bash-3.2# whoami
root
bash-3.2# sudo -u tomwaits open /Users/tlarkin/Desktop/deleteLoginItems.app
bash-3.2#
In return the start up items I set for the user account Tom Waits, were in fact deleted. I had added Safari and VMware Fusion assistant as two start up items. If this is further not working for you I will toss it in my virtual JSS when I have a few minutes later today to fully test. As of right now I have only tested it locally. I did get the expected results though.
I hope this fixes it,
Thanks,
Tom
it all works fine when I run in terminal locally however its when i run a script invoking the app that I get the error.
Script
#!/bin/bash
currentUser=$(ls -l /dev/console | awk '{ print $3 }')
sudo -u ${currentUser} open /Users/Shared/DeleteLoginItems.app
exit 0
This may be overkill, but you could try this-
#!/bin/sh
loggedInUser=$( ls -l /dev/console | awk '{print $3}' )
loggedInPID=$( ps -axj | awk "/^$loggedInUser/ && /Dock.app/ {print $2;exit}" )
/bin/launchctl bsexec "${loggedInPID}" sudo -iu "${loggedInUser}" "open /Users/Shared/DeleteLoginItems.app"
Stolen, err, borrowed code from others here that have done this before me. I use similar syntax in another script that has to call terminal-notifier.app as the logged in user.
If the above works, I'd go back and try using your original osascript code above instead of the call to an app to see if that works as well. Only thing is, you need to backslash escape any quotes in the osascript line (singles or doubles) or it won't work.
Could you post both your apple script and the bash script you are using to run it?
Thanks,
Tom
as in:
#!/bin/sh
loggedInUser=$( ls -l /dev/console | awk '{print $3}' )
sudo -u "$loggedInUser" /usr/bin/osascript -e ' ell application "System Events" to delete login items'
as in:
#!/bin/sh
loggedInUser=$( ls -l /dev/console | awk '{print $3}' )
sudo -u "$loggedInUser" /usr/bin/osascript -e ' ell application "System Events" to delete login items'
AppleScript
tell application "System Events"
delete login items
end tell
Shell Script:
#!/bin/bash
currentUser=$(ls -l /dev/console | awk '{ print $3 }')
sudo -u ${currentUser} open /Users/Shared/DeleteLoginItems.app
exit 0
alternatively I would just like this to work with shell script as in:
#!/bin/sh
loggedInUser=$( ls -l /dev/console | awk '{print $3}' )
sudo -u "$loggedInUser" /usr/bin/osascript -e 'tell application "System Events" to delete login items'
@msardes
Almost right/ Make sure to put the backslashes before the quote marks for one, not after them. Also, quote the entire command you're instructing the script to run as the user. So like this
sudo -iu "$loggedInUser" "/usr/bin/osascript -e 'tell application "System Events" to delete login items'"
Lastly, note I'm using sudo -iu, not sudo -u
script changed to
#!/bin/bash
loggedInUser=$( ls -l /dev/console | awk '{print $3}' )
sudo -iu "$loggedInUser" "/usr/bin/osascript -e 'tell application "System Events" to delete login items'"
following error:
Running script Delete Login Items.sh...
Script exit code: 1
Script result: 0:1: syntax error: A unknown token can’t go here. (-2740)
btw the apple scrips one worked however If I can just do this with a shellscript that would rock.
ok doing this:
Delete Items apple script app in /Users/Shared
this shell script to invoke at login
#!/bin/sh
sleep 1
loggedInUser=$( ls -l /dev/console | awk '{print $3}' )
loggedInPID=$( ps -axj | awk "/^$loggedInUser/ && /Dock.app/ {print $2;exit}" )
/bin/launchctl bsexec "${loggedInPID}" sudo -iu "${loggedInUser}" "open /Users/Shared/DeleteLoginItems.app"
works great, thanks guys and girls.
mm2270 you legend, that works a treat.