Skip to main content
Jamf Nation, hosted by Jamf, is a dynamic and knowledgeable community of Apple-focused IT admins and Jamf Pro users. Join us in person, in October, for the annual Jamf Nation User Conference (JNUC) to discover new and better ways to manage Apple devices.
16

Script Error help

Posted: 10/24/13 at 10:39 AM by msardes

following script being deployed via casper remote or policy

#!/bin/sh

osascript -e 'tell application "System Events" to delete login items';

and I get the following error: Runs perfect when run through local terminal

Mounting Casper Distribution to /Volumes/casper...
Running script Clear Login Items.sh...
Script exit code: 1
Script result: 43:54: syntax error: A plural class name can’t go after this identifier. (-2740)

Any help would be appreciated.

16
CCT Badge CJA Badge JAMFBadge

Posted: 10/24/13 at 10:56 AM by Andrina

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...

Posted: 10/24/13 at 11:04 AM by msardes

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.

CCA Badge CCE Badge CJA Badge CMA Badge

Posted: 10/24/13 at 11:16 AM by tlarkin

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

Posted: 10/24/13 at 11:45 AM by msardes

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.

CCA Badge CCE Badge CJA Badge CMA Badge

Posted: 10/24/13 at 12:46 PM by tlarkin

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

Posted: 10/24/13 at 12:51 PM by msardes

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

CCA Badge

SOLVED Posted: 10/24/13 at 1:00 PM by mm2270

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.

CCA Badge CCE Badge CJA Badge CMA Badge

Posted: 10/24/13 at 1:10 PM by tlarkin

Could you post both your apple script and the bash script you are using to run it?

Thanks,
Tom

Posted: 10/24/13 at 1:14 PM by msardes

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\'

Posted: 10/24/13 at 1:14 PM by msardes

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\'

Posted: 10/24/13 at 1:19 PM by msardes

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'

CCA Badge

Posted: 10/24/13 at 1:30 PM by mm2270

@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

Posted: 10/24/13 at 1:43 PM by msardes

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)

Posted: 10/24/13 at 1:45 PM by msardes

btw the apple scrips one worked however If I can just do this with a shellscript that would rock.

Posted: 10/24/13 at 2:17 PM by msardes

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.

Posted: 8/5/14 at 3:05 AM by richmac

mm2270 you legend, that works a treat.