Hi all, I’m trying to get a simple script going that’s triggered by iCal to turn off my computer after a certain time if I don’t cancel the shutdown. So far, what I have is this:
tell application "System Events"
display dialog "Shut down?" buttons {"Cancel", "OK"} default button 2
repeat
with timeout of 120 seconds
if buttonReturned("Cancel") then
exit repeat
else if buttonReturned("OK") then
shut down
end if
end timeout
shut down
end repeat
end tell
When I run the script, I get an error message if I click on the “OK” button:
I’ve also tried removing the timeout clause.
Any ideas? Thanks in advance.
Hi,
the problem is, you have defined buttonReturned as a handler call using the syntax buttonReturned() and System Events doesn’t know handlers.
The return value of display dialog must be assigned explicitly to a variable.
The timeout block is not needed, use giving up instead
tell application "System Events"
set {button returned:buttonReturned, gave up:gaveUp} to display dialog "Shut down?" buttons {"Cancel", "OK"} default button 2 giving up after 120
if buttonReturned is "OK" or gaveUp then shut down
end tell
the script will be aborted anyway, when the user presses Cancel
Thanks! Works like a charm. Only question: when scheduled from iCal, when I cancel out, I get an error message:
Not a big deal… just curious as to whether there’s a way to script it that you don’t get the error message.
pressing Cancel in display dialog throws error -128, maybe this causes the error message.
The try block ignores the error
tell application "System Events"
try
set {button returned:buttonReturned, gave up:gaveUp} to display dialog "Shut down?" buttons {"Cancel", "OK"} default button 2 giving up after 120
if buttonReturned is "OK" or gaveUp then shut down
end try
end tell