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

Shutdown Applescript Issue

Hi All,


I have an applescript that worked perfectly until 10.7 brought in application state when shutting down. Now my script shuts down the mac, but when i turn it back on i am prompted with the dialog to add the shut down time again. This is a pain as i have to force quit it. Has anyone got any ideas on how to not retain application state (open) when shutting down?


any help is greatly appreciated.


property shutdown_time : date


on run


set response to display dialog "Please State Number of minutes until Shutdown?" with title "LockDown V2.0" buttons {"LockDown!"} 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)


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

Posted on Nov 13, 2013 1:06 PM

Reply
10 replies

Nov 13, 2013 2:17 PM in response to JayBird2009

It looks like the system is set to reopen applications that were running when it was shut down. I'm not sure why it was running differently in Lion, but your application will always prompt with the dialog when it starts up because that is the way it is written (the idle handler will be run after the run handler finishes).


Properties are persistent and saved with the script, so when it is first started you can use them to check the date or application status to see if it needs to put up the dialog.

Nov 13, 2013 2:33 PM in response to JayBird2009

You can do the same kind of check that you are doing in your idle handler - if the shutdown time hasn't been set yet or is expired, put up the dialog, otherwise continue, for example:

property shutdown_time : missing value on run   if shutdown_time is missing value or shutdown_time ≤ (current date) then     set response to display dialog "Please State Number of minutes until Shutdown?" with title "LockDown V2.0" buttons {"LockDown!"} 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)   end if end run on idle   if (shutdown_time < (current date)) then     set shutdown_time to missing value -- reset for next time     tell application "System Events"       ignoring application responses         shut down       end ignoring     end tell     quit   end if   return 1 -- the number of seconds to check again end idle

Nov 13, 2013 3:33 PM in response to JayBird2009

Your application is being run at startup because reopening the current applications is the preference setting when shutting down (note that your application is still running when the system is shut down), or it is in your user's login items. The application is what will be putting up the dialog, so it will need to check to see if that is appropriate.


If you are going to be restarting the current applications the next time you start up, your application will need to check to see what its current status is. When do you want to see the dialog - are you wanting to have to manually restart the application after it performs a shutdown?

Nov 14, 2013 3:57 AM in response to JayBird2009

Hello


Try something like the following script. The point would be not to have the stay-open applet be running when the shutdown sequence begins. The script below employs osascript(1) in background in order to initiate shutdown AFTER the applet quits.


Not tested, for I don't use any 10.7 or later systems.


Good luck,

H


property shutdown_time : missing value

repeat
    display dialog "Enter number of minutes until shutdown" default answer "60"
    try
        set n to text returned of result as number
        exit repeat
    end try
end repeat
set shutdown_time to (current date) + (n * minutes)

on idle
    if (shutdown_time < (current date)) then
        do shell script "osascript <<'EOF' &>/dev/null &
delay 5
ignoring application responses
    tell application \"System Events\"
        shut down
    end tell
end ignoring
EOF"
        quit
    end if
    return 1
end idle

Nov 14, 2013 5:43 AM in response to JayBird2009

I'm confused. The Terminal shutdown command takes a time value. No need to be doing any of this.


If you need a wrapper around shutdown either do it as a shell script or make an Applescript that just calls shutdown.


something like this to start:

set response to display dialog "Please State Number of minutes until Shutdown?" with title "LockDown V2.0" buttons {"LockDown!"} default answer "60"


set timer to (text returned of the response) as number


do shell script "shutdown -h " & "+" & timeruser name "me" password "mypassword" with administrator privileges

Nov 14, 2013 10:09 AM in response to JayBird2009

Well, if your posted code is the entire code of your shutdown applet, then you may just replace your idle handler with mine. Otherwise you may put additional code before the do shell script statement such as:


on idle
    if (shutdown_time < (current date)) then
    
        -- # here you may put any additional code to be executed before shutdown call
    
        do shell script "osascript <<'EOF' &>/dev/null &
delay 5
ignoring application responses
    tell application \"System Events\"
        shut down
    end tell
end ignoring
EOF"
        quit
    end if
    return 1
end idle


Regards,

H

Shutdown Applescript Issue

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