Shutdown Script

Hey Guys,

I found this script to shutdown my mac when i choose a certain time, it is not my script but i understand how it works:

property shutdown_time : date

on run
set response to display dialog “Number of minutes until Shutdown?” default answer “60”
set timer to (text returned of the response) as number
set shutdown_time to (current date) + (timer * minutes)
display dialog "This computer will shut down at: " & shutdown_time as string
end run

on idle
if (shutdown_time < (current date)) then
tell application “Finder” to activate
tell application “System Events”
tell process “Finder”
click menu item “Shut Down.” of menu 1 of menu bar 1
end tell
end tell
tell me to quit
end if
return 0
end idle

However, i cannot get it to run, the dialog box takes the number of minutes created, it then sets the time fine, and the time string is returned in the next dialog box. I Click ok and wait the 1 minute entered, but the system does not shut down…

Has anyone got any ideas? Cheers

Hi,

try the shut down command of System Events, GUI scripting is not needed


on idle
	if (shutdown_time < (current date)) then
		tell application "System Events" to shut down
		quit
	end if
	return 1
end idle

You did save it as a STAY-OPEN application? Did you?:rolleyes:

Cheers Both, A Both Replies have been implemented and now the application is working

I do have a question though. If i were to run that and leave say safari on with 2 / 3 pages open, the system waits until the user clicks close

is there a kill all = true string i could use to close all programs down without a prompt, so that the the shutdown doesn’t get interrupted

Once again guys thanks for the help

The shell command shutdown does this, however it requires the admin password

do shell script "/sbin/shutdown -h now" with administrator privileges

You could implement this somewhere:

DO NOT USE THE APPLESCRIPT BELOW

tell application "System Events"
	set allProcesses to name of every application process whose name is not NAMEOFYOURAPPLICATIONPROCESS
	
	repeat with i in allProcesses
		try
			do shell script "killall " & i
		end try
	end repeat
	
	shut down
end tell

Though, I think it isn’t save. I, personally wouldn’t, use it, because it also force quits for example the processes loginwindow, mdworker, SystemUIServer, etc. But. no password is needed!

Indeed, it’s strongly recommended not to do that!

You could filter the visible processes ( = all applications with a GUI)


.
set allProcesses to name of every application process whose visible is true and name is not NAMEOFYOURAPPLICATIONPROCESS
.

cheers both, i will play around with a few things, and once happy, i will post script up, thanks again :slight_smile:

Hey Guys,

I Am Playing with simple commands at present, but cannot understand what is going on here:

property shutdown_time : date

on run
	
	set response to display dialog "Please State Number of minutes until Shutdown?" with title "Shutdown" buttons {"Shutdown", "Cancel"} default button 2 default answer "60" with icon path to resource "Time.icns" in bundle ("/Users/jbird/Documents/MAC Apps/Created Mac Apps")
	set timer to (text returned of the response) as number
	set shutdown_time to (current date) + (timer * minutes)
	display dialog "This computer will Shutdown at: " & shutdown_time as string with title "Shutdown" buttons {"Close"} default button 1 with icon path to resource "Time.icns" in bundle ("/Users/jbird/Documents/MAC Apps/Created Mac Apps")
	
end run

on idle
	if (shutdown_time < (current date)) then
		tell application "System Events" to shut down
		tell application "iTunes" to quit
		tell application "Safari" to quit
		tell application "QuickTime Player" to quit
		tell application "Thunderbird" to quit
		tell application "UnRarX" to quit
		tell application "TextEdit" to quit
		tell application "iGetter" to quit
		tell application "Preview" to quit
		tell application "VLC" to quit
		quit
	end if
	return 1
end idle

It keeps returning : Cannot Make Date into Type number, date or text.

Hi,

assign missing value to the property in the first line.
date is a class and needs a parameter