AppleScript Error - No user interaction allowed

pairjal
New Contributor II

I do a policy that will run script for user to shutdown/restart their laptop if the uptime is more than 7days.
The script is like this:

set question to display dialog "The machine has not been shut down or restarted for more than a week. Please shut down or restart the machine by clicking on Shut Down or Restart." with title "System Event by Andres Cheah, MCM" with icon stop buttons {"Shut Down", "Restart", "Cancel"}
set answer to button returned of question
if answer is equal to "Shut Down" then tell application "System Events" shut down end tell
end if
if answer is equal to "Restart" then tell application "System Events" restart end tell
end if
if answer is equal to "Cancel" then return
end if

I test in the machine it is working fine, but when i put into the policy; there an error below:

Script exit code: 1
Script result: 2013-09-23 10:15:47.576 osascript[2866:707] ApplePersistence=NO /Library/Application Support/JAMF/tmp/System event-AppleScript Applet.scpt: execution error: No user interaction allowed. (-1713)

Anyone can advise?

1 ACCEPTED SOLUTION

aurica
New Contributor III

Give this a try:

#!/bin/bash
VARIABLE=$(/usr/bin/osascript <<-EOF
tell application "System Events"
    activate
    set question to display dialog "The machine has not been shut down or restarted for more than a week. Please shut down or restart the machine by clicking on Shut Down or Restart." with title "System Event by Andres Cheah, MCM" buttons {"Shut Down", "Restart", "Cancel"} with icon caution
    set answer to button returned of question
    if answer is equal to "Shut Down" then
        tell application "System Events"
            shut down
        end tell
    end if
    if answer is equal to "Restart" then
        tell application "System Events"
            restart
        end tell
    end if
    if answer is equal to "Cancel" then
        return
    end if
end tell
EOF)

$VARIABLE
exit 0

View solution in original post

14 REPLIES 14

bentoms
Esteemed Contributor
Esteemed Contributor

It's because Casper runs as root & not the logged in user.

Try wrapping it in a tell block..

Like "tell Finder..... end tell"

bentoms
Esteemed Contributor
Esteemed Contributor

Also, I've had luck when using AppleScript from Bash scripts... Like: http://macmule.com/2013/07/13/change-screenshot-location-from-self-service-with-gui-prompt/#more-595

pairjal
New Contributor II

hi, thanks for the reply,

i already put "end tell" , same error..

perrycj
Contributor III

I had a similar problem having to run a script to prompt to change AD passwords so what I did was store the script on every system within a directory that you know exists on all macs. I used the /Users/Shared directory and then I used this script to call it to run and to make it run as the user logged in and not root:

#!/bin/bash

user=/usr/bin/last -1 -t console | awk '{print $1}'
su -l $user -c "open /Users/Shared/filenamecontainingscript"

I also used an applescript as the stored script in the /Users/Shared folder. Hope this helps.

aurica
New Contributor III

Give this a try:

#!/bin/bash
VARIABLE=$(/usr/bin/osascript <<-EOF
tell application "System Events"
    activate
    set question to display dialog "The machine has not been shut down or restarted for more than a week. Please shut down or restart the machine by clicking on Shut Down or Restart." with title "System Event by Andres Cheah, MCM" buttons {"Shut Down", "Restart", "Cancel"} with icon caution
    set answer to button returned of question
    if answer is equal to "Shut Down" then
        tell application "System Events"
            shut down
        end tell
    end if
    if answer is equal to "Restart" then
        tell application "System Events"
            restart
        end tell
    end if
    if answer is equal to "Cancel" then
        return
    end if
end tell
EOF)

$VARIABLE
exit 0

pairjal
New Contributor II

Hi aurica,

I try the script, work fine. But when upload it and run using Casper, this error:

Executing Policy Never Shutdown 7days...
Mounting my-jss01 to /Volumes/CasperShare...
Running script SystemEvent.sh...
Script exit code: 0
Script result: 449:453: syntax error: Expected end of line but found application constant or consideration. (-2741)

aurica
New Contributor III

I had success with my sample. I uploaded it into my 8.62 environment as pairjal_test.sh and set up a manually triggered policy.

Log from user cancelation, which registers as a policy failure:

/usr/sbin/jamf is version 8.62
Executing Policy pairjal test...
Creating directory structure for /Library/Application Support/JAMF/Downloads/
Downloading http://FQDN:80/ShareName/Scripts/pairjal_test.sh...
Running script pairjal_test.sh...
Script exit code: 0
Script result: 58:329: execution error: System Events got an error: User canceled. (-128)

Log from successful restart and shutdown:

/usr/sbin/jamf is version 8.62
Executing Policy pairjal test...
Creating directory structure for /Library/Application Support/JAMF/Downloads/
Downloading http://FQDN:80/ShareName/Scripts/pairjal_test.sh...
Running script pairjal_test.sh...
Script exit code: 0
Script result:

The syntax error in your last post makes me suspect that you might have dropped a ( in the copy/paste?

tlarkin
Honored Contributor

Hey Everyone,

I had this problem moving forward with 10.7 and 10.8. Where the Applescript, when ran as root, did not like not having a user environment. To get around this, I would save my scripts as apps in Applescript then use the `sudo -u` command to run as root as the current user.

So, I would toss my Applescript app in the Scripts folder on my CasperShare, then have another script call it like this:

#!/bin/bash

# execute applescript as end user
currentUser=$(ls -l /dev/console | awk '{ print $3 }')

sudo -u ${currentUser} open /Volumes/CasperShare/Scripts/myapplescript.app

exit 0

The CasperShare will always mount to /Volumes, and I just had scripts call the app from there with the open command.

I hope this may help you out.

Tom

Alert
New Contributor

+1

pairjal
New Contributor II

Hi Aurica

thanks for the script. i have modified and working fine now.

AKuzenkov
New Contributor III

For me, "tell application "System Events"" does not work, however tell application "Finder" does.

AKuzenkov
New Contributor III

Issue seems to be resolved with 9.24

geoffrey-obrien
New Contributor

So, you can now launch that applescript from a bash script? Im trying to make this happen and its just not going.

AVmcclint
Honored Contributor

I would love to use a script like this. However, I can't figure out how to set the threshold for when to trigger it to happen. I'd like for our computers to be rebooted if their uptime is greater than 20 days. And if a user clicks cancel, how much time can elapse before it is triggered again?