Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

error with shutdown timer

Im trying to make a simple app on my desktop so that it displays a dialoge box of 30 60 or 90 . then it will shut down in 30 60 or 90 minutes


display dialog

"How many minutes until shutdown?"

buttons("30", "60", "90")

if result = {button returned:"30"} then

tell application "Finder"

delay 1800


shut down

end tell

end if

if result = {button returned:"60"} then

tell application "Finder"

delay 3600


shut down

end tell

end if

if result = {button returned:"90"} then

tell application "Finder"

delay 5400


shut down

end tell

end if

MacBook Pro (15-inch Mid 2012)

Posted on Nov 26, 2013 1:02 AM

Reply
14 replies

Nov 26, 2013 6:53 AM in response to psych881

A more general solution:


repeat -- until an integer is entered

display dialog "How many minutes until shutdown?" default answer "30" buttons {"Cancel", "OK"} default button 2 with icon 1

try

set theAnswer to text returned of result as integer

exit repeat -- if there's no error

end try

end repeat


delaytheAnswer * 60


tell application "Finder" to shut down

Nov 26, 2013 7:36 AM in response to psych881

This is not the way you want to be doing this. using delay this way will tie up the script editor and slow down the Finder. What you want to do is make a script application and use its idle loop to handle the delay. So copy the following script into the script editor, save it as an application (from the File Format pull down menu near the bottom of the save dialog), then run the application.


global shutdownDelay, timeToDoShutdown


on run

display dialog "How many minutes until shutdown?" buttons {"30", "60", "90"}


if result = {button returned:"30"} then

set shutdownDelay to 1800

else if result = {button returned:"60"} then

set shutdownDelay to 3600

else

set shutdownDelay to 5400

end if


set timeToDoShutdown to false

end run


on idle

if timeToDoShutdown then


-- use system events, not the Finder; Finder shut down is deprecated

tell application "System Events" to shut down

tell me to quit

return 1

else


-- need one pass through idle loop before we can go to shutdown

set timeToDoShutdown to true

return shutdownDelay

end if

end idle

Nov 26, 2013 8:03 AM in response to psych881

Or else


property shutDownTime : ""

property beenHere : false


on run

set shutDownTime to button returned of (display dialog "How many minutes until shutdown?" buttons {"30", "60", "90"})


set shutDownTime to shutDownTime * 60

end run


on idle

if beenHere then

tell application "System Events" to shut down

tell me to quit

end if


set beenHere to true

return shutDownTime


end idle


Nov 26, 2013 10:04 AM in response to Pierre L.

There are three differences:

  • use of globals/properties.
  • creation of a script app.
  • use of an idle handler.

The last two are self-evident with a modicum of thought, the first is a new principle for beginners iff they have never programmed in any language. But even then it's not that difficult a principle to grasp.


And anything they really don't understand is easily explained, no?


However, both Pierre and I forgot one critical detail. When saving the script as an application, you need to check the "Stay open after run handler" checkbox or the idle handler will never get called.


oops. 😀

Nov 26, 2013 7:37 PM in response to Pierre L.

What twtwtw said 😝


Pierre L. I see nothing wrong in pushing the envelope as it were when answering a question here. If the OP is interested in expanding their knowledge then this might push them in that direction. If not and they are just looking for a turnkey solution any of the scripts given here will do the job and they are free to choose to the one they want to use.


I look at this place as a learning/teaching forum (when it comes to programming questions). I would not post here a script that I would not use myself and that does not follow good programming practices.


This is not to imply that any of the scripts posted here do not follow good programming practices only that if I had to solve this problem using AppleScript I would use the solution I posted.


regards

error with shutdown timer

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.