Shutdown Script Issue

Hi all,

i made a shutdown script for my mac way back when i had OSX 10.6, now with the advancements of OSX and its auto open feature the script is broke :frowning:

The script works nicely, the machine shuts down in the given minutes supplied, but when the machine restarts the script is then rerun and asks for shutdown time again, so i have to force quit it.

Can anyone suggest how to get this to not reopen on startup?

thanks



property shutdown_time : date

on run
	
	set response to display dialog "Please State Number of minutes until Shutdown?" with title "App" buttons {"Down!"} default answer "60" with icon path to resource "Time.icns" in bundle ("/Users/MAC Apps/Created Mac Apps")
	set timer to (text returned of the response) as number
	set shutdown_time to (current date) + (timer * minutes)
	
end run

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

Try this:

tell application "System Events"
	ignoring application responses
		shut down
	end ignoring
end tell

If that doesn’t work I think there are other ways.

Edited: if that doesn’t work try adding a quit handler. and use the above script in the quit handler.

gl,
kel

hey Kel1

Just tried the below code, but when mac restarted the app opens again :frowning:

Im not sure what you mean by:


property shutdown_time : date

on run
	
	set response to display dialog "Please State Number of minutes until Shutdown?" with title "Down" buttons {"Down!"} default answer "60" with icon path to resource "Time.icns" in bundle ("/Users/MAC Apps/Created Mac Apps")
	set timer to (text returned of the response) as number
	set shutdown_time to (current date) + (timer * minutes)
	
end run

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

Hi JayBird,

I meant something like this otherwise I don’t know:

on idle
	if (shutdown_time < (current date)) then
		quit
	end if
	return 1
end idle

on quit
	tell application "System Events"
		ignoring application responses
			shut down
		end ignoring
	end tell
	continue quit
end quit

I’m just guessing because I’ve never shut down from an idle handler.

gl in your pursuits,
kel

hi Kel1

Nope no luck putting it through like that either:

i assume its because as the application is shutting down the system is saving the applications states. As this application is actioning the shut down its state is saved as open, and thus reopens on restart.

:frowning:


property shutdown_time : date

on run
	
	set response to display dialog "Please State Number of minutes until Shutdown?" with title "Down V2.0" buttons {"
Down!"} default answer "60" with icon path to resource "Time.icns" in bundle ("/Users/MAC Apps/Created Mac Apps")
	set timer to (text returned of the response) as number
	set shutdown_time to (current date) + (timer * minutes)
	
end run
on idle
	if (shutdown_time < (current date)) then
		quit
	end if
	return 1
end idle

on quit
	tell application "System Events"
		ignoring application responses
			shut down
		end ignoring
	end tell
	continue quit
end quit


I’m quite sure that I can shut down my machine with System Events. It might be a matter of setting your System Preferences. I don’t know.

Hope you find your answer,
kel

ps. I’m interested also.

Tried that one as we’ll :frowning:

The shutdown works fine, the only issue is the re-open when you turn machine back on

If i get it to work then ill drop you a pm

**** i wonder if deleting the saved state cache on exit would do the trick…

Hello.

do shell script "osascript -e \"tell application \\\"System Events\\\" to shut down\" 2>/dev/null"

Put the line above into a handler in a separate applet, and call it from inside of the ignoring responses block in your applet above. (Or just tell SystemEvents to shut down from within the other applet.) Good luck!

I tried to make a do shell script that I used to, but either I didn’t get it right, or it is impossible to send something that contains an osascript at least to the background.:confused:

the ignoring responses block is useless because the command shut down does not expect any returning Apple Event.

If you’re running Mavericks I recommend to write the script/app in AppleScriptObjC and implement


current application's NSApplication's sharedApplication()'s disableRelaunchOnLogin()

Actually, the ignoring application block used to work. What it does is it doesn’t wait until the application gets a reply that it is shutting down. That’s what causes the application to stay alive until it gets a return which it never does. If you don;t wait for the reply, that’s what you want to do. I think that used to work. I’ll wait until tomorrow to test it.

Hello

Here is the script which I use because triggering directly the Shutdown menu item from Apple menu often stop due to this or that application.

set myName to my name

tell application "System Events"
	set visible of every process to true
	--delay 0.5
	set theAppsNames to (displayed name of every process whose visible is true and name is not "Finder" and name is not myName)
end tell

repeat with anApp in theAppsNames
	if anApp as text is not in {"Éditeur AppleScript", "Moniteur d'activité", "Keynote", "Numbers", "Pages", "AppleScript Editor", "Activity Monitor"} then
		try
			quit application anApp
		end try
	end if
end repeat
# Now the applications are cleanly closed
# We may delete the Saved Application State datas

repeat
	try
		set p2AS to (path to library folder from user domain as text) & "Saved Application State:"
		exit repeat
	on error
		delay 0.1
	end try
end repeat
tell application "System Events" to tell folder p2AS
	delete every folder
end tell # System Events .

set p2pane to ((path to system preferences as text) & "StartupDisk.prefPane") as alias
# Get four localized strings
set cancelBtn to localized string "CANCEL" in bundle p2pane
set restartBtn to localized string "RESTART" in bundle p2pane
set shutdownBtn to localized string "SHUTDOWN" in bundle p2pane
set prompt to localized string "RESTART?" in bundle p2pane

tell me to display dialog prompt buttons {cancelBtn, restartBtn, shutdownBtn} default button 3 cancel button 1
set |éteindre| to (button returned of result) is shutdownBtn
# Now we may trigger shutdown or restart cleanly, open documents were saved above
tell application "System Events"
	if |éteindre| then
		shut down
	else
		restart
	end if
end tell # System Events

Most of the time, it behaves well but from time to time, it ask me where are AppleScript Editor and Activity Monitor.

Yvan KOENIG (VALLAURIS, France) jeudi 14 novembre 2013 10:55:56