Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

An AppleScript to generate a timed shutdown System
I searched on a lot of Apple sites to find an easy shutdown script. shutdown -h -t xx works fine in the Terminal, but it's too clumsy. So I managed to make a (very) little AppleScript (my first one, BTW) which works.

(* how many seconds before we remove the notice? *)
property dialog_timeout : 3

repeat
  try
    display dialog "In how many minutes must the computer be shutdown?" 
    default answer ""
    set the time_amount to the text returned of the result as integer
    if the time_amount is greater than 0 then exit repeat
    on error number error_number
    if the error_number is -128 then return "user cancelled"
  end try
end repeat

if the time_amount is 1 then
  set the my_minuten to "minute"
else
  set the my_minuten to "minutes"
end if

display dialog "The computer will be shutdown in " & time_amount & " " & 
my_minuten & "!" buttons {"Ok"} default button 1 giving up after dialog_timeout

delay time_amount * 60
tell application "Finder" to shut down
There is no canceling as I know, that would a nice addition...

[robg adds: 10.3 includes a scheduled shutdown in the Energy Saver prefs, though it might be easier to run this script as opposed to accessing the prefs each time...]
    •    
  • Currently 1.67 / 5
  You rated: 1 / 5 (6 votes cast)
 
[22,267 views]  

An AppleScript to generate a timed shutdown | 10 comments | Create New Account
Click here to return to the 'An AppleScript to generate a timed shutdown' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
An AppleScript to generate a timed shutdown
Authored by: ranker on Feb 17, '04 11:53:26AM
When you need to pause an applescript for long periods of time it is not advisable to use "delay". It causes the script to hang, eating up processing time. Instead, you should use the Unix "sleep" command.

do shell script ("sleep " & number_of_seconds)

[ Reply to This | # ]

An AppleScript to generate a timed shutdown
Authored by: hello1234 on Feb 17, '04 04:21:55PM
Here's a way to give the user a chance to cancel shutting down:

display dialog "The computer will be shutdown in " & time_amount & " " & 
my_minuten & "!" buttons {"Ok"} default button 1 giving up after dialog_timeout

delay time_amount * 60

display dialog "The computer is about to shutdown." buttons {"Ok","Cancel"} default button 1 giving up after 10

tell application "Finder" to shut down


[ Reply to This | # ]
another way
Authored by: SOX on Feb 17, '04 04:31:42PM

leave 2:45 && osascript "tell finder to shutdown"



[ Reply to This | # ]
An AppleScript to generate a timed shutdown
Authored by: cougar718 on Feb 17, '04 05:14:51PM
Hey all, Not bad Steve. I decided to clean up your code a bit. It now uses a different method than using delay or sleep which is... Sets the scheduled date to the current date + Number entered * 60 (Converts the number entered into minutes) Then on idle, tests if the current date is greater than or equal to the scheduled date. Like I said, good job and thanks for the contribution. Hey all if you want to use this version, simply paste it into Script Editor and save as an application making sure Stay Open is checked since this script utilizes the on idle event. Cheers,

--Created by StevenT 
--Modified by cougar

-- How many seconds before we remove the shutdown notice? 
property pIntTimeout : 3
property pIntTimeAmount : ""
property pDateGoal : ""

on run
	set pDateGoal to ""
	set blnValid to false
	repeat until blnValid
		set recDialog to display dialog "Enter in how many minutes until the computer shuts down?" default answer "" buttons {"Cancel", "Ok"} default button 2
		try
			set pIntTimeAmount to text returned of recDialog as integer
			if (pIntTimeAmount > 0) then
				set blnValid to true
			else
				error
			end if
		on error
			display dialog "Invalid Number - Numbers must be integers and greater than 0" buttons {"Ok"} default button 1
		end try
	end repeat
	
	set strMinute to " minute"
	if (pIntTimeAmount > 1) then set strMinute to strMinute & "s"
	display dialog "The computer will shutdown in " & pIntTimeAmount & strMinute & "!" buttons {"Ok"} default button 1 giving up after pIntTimeout
	set pDateGoal to (current date) + (pIntTimeAmount * 60)
end run

on idle
	try
		if ((current date) ? pDateGoal) then
			tell application "Finder" to shut down
		end if
	end try
end idle

---
Rick alias cougar

[ Reply to This | # ]

Have it remember your last shutdown delay
Authored by: ngb on Feb 18, '04 02:14:49AM
In the display dialog line, change default answer "" to display dialog pIntTimeAmount, and the script will display your last timeout as the default. Whenever the script changes the value of a property, it remembers the new value next time it is launched. The complete line is below.

set recDialog to display dialog "Enter in how many minutes until the computer shuts down?" 
     default answer pIntTimeAmount buttons {"Cancel", "Ok"} default button 2


[ Reply to This | # ]
An AppleScript to generate a timed shutdown
Authored by: cougar718 on Mar 08, '04 05:29:26PM
Hey all, Seems that the >= character was not carried over correctly. Should be...

on idle
	try
		if ((current date) >= pDateGoal) then
			tell application "Finder" to shut down
		end if
	end try
end idle

---
Rick alias cougar

[ Reply to This | # ]

An AppleScript to generate a timed shutdown [Final Version]
Authored by: cougar718 on Apr 15, '04 02:34:24PM
Here is the final version of this script. It was brought to my attention that there was a critical component of the code missing it is fixed now. Simply compile and Save as an Application and make sure "Stay Open" is checked.

--Created by StevenT 
--Modified by cougar

-- How many seconds before we remove the shutdown notice? 
property pIntTimeout : 3
property pIntTimeAmount : ""
property pDateGoal : ""

on run
	set pDateGoal to ""
	set blnValid to false
	repeat until blnValid
		set recDialog to display dialog "Enter in how many minutes until the computer shuts down?" default answer pIntTimeAmount buttons {"Cancel", "Ok"} default button 2
		try
			set pIntTimeAmount to text returned of recDialog as integer
			if (pIntTimeAmount > 0) then
				set blnValid to true
			else
				error
			end if
		on error
			display dialog "Invalid Number - Numbers must be integers and greater than 0" buttons {"Ok"} default button 1
		end try
	end repeat
	
	set strMinute to " minute"
	if (pIntTimeAmount > 1) then set strMinute to strMinute & "s"
	display dialog "The computer will shutdown in " & pIntTimeAmount & strMinute & "!" buttons {"Ok"} default button 1 giving up after pIntTimeout
	set pDateGoal to (current date) + (pIntTimeAmount * 60)
end run

on idle
	try
		if ((current date) >= pDateGoal) then
			tell application "Finder" to shut down
		end if
	end try
	return 1
end idle

---
Rick alias cougar

[ Reply to This | # ]

An AppleScript to generate a timed shutdown
Authored by: pgk on Oct 03, '05 10:33:48PM

For some reason, this script isn't working for me. The dialog boxes display as they should and the fields seem to accept entries okay. But my computer is still not shutting down. Any ideas? Btw, if it matters, I'm running OSX 10.4.2 on a 15" Titanium PB. Thanks!



[ Reply to This | # ]
An AppleScript to generate a timed shutdown (Final Version)
Authored by: pgk on Oct 03, '05 10:36:17PM

To clarify, I'm running the "Final Version" of the script. Thanks.



[ Reply to This | # ]
An AppleScript to generate a timed shutdown (Final Version)
Authored by: pgk on Oct 03, '05 11:51:47PM

Nevermind. It's working now. (I may have failed to select the "Stay Open" option when I first saved the script.)



[ Reply to This | # ]