Posted on 09-22-2013 07:55 PM
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?
Solved! Go to Solution.
Posted on 09-23-2013 01:50 PM
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
Posted on 09-22-2013 10:16 PM
It's because Casper runs as root & not the logged in user.
Try wrapping it in a tell block..
Like "tell Finder..... end tell"
Posted on 09-22-2013 10:18 PM
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
Posted on 09-22-2013 10:29 PM
hi, thanks for the reply,
i already put "end tell" , same error..
Posted on 09-23-2013 05:33 AM
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.
Posted on 09-23-2013 01:50 PM
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
Posted on 09-23-2013 07:16 PM
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)
Posted on 09-24-2013 08:56 AM
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?
Posted on 09-24-2013 10:07 AM
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
Posted on 09-24-2013 11:32 PM
+1
Posted on 09-24-2013 11:35 PM
Hi Aurica
thanks for the script. i have modified and working fine now.
Posted on 12-31-2013 02:09 PM
For me, "tell application "System Events"" does not work, however tell application "Finder" does.
Posted on 02-26-2014 04:08 PM
Issue seems to be resolved with 9.24
Posted on 04-23-2014 01:21 PM
So, you can now launch that applescript from a bash script? Im trying to make this happen and its just not going.
Posted on 04-06-2015 08:35 AM
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?