2

I'm trying to automate the shutdown of my mac, I've tried the scheduled shutdown in energy saver and I wanna sleep but these don;t seem to work. VLC player runnign seems to prevent the shutdown. I think I need a script to forcefully shutdown the mac regardless of of what errors may thrown to screen by various programs running.

Thanks

Ok,

This is the applescript code im using to shutdown may mac. I've added it as an iCal event thats runs nightly.

tell application "System Events" to set the visible of every process to true

set white_list to {"Finder"}

try
    tell application "Finder"
        set process_list to the name of every process whose visible is true
    end tell
    repeat with i from 1 to (number of items in process_list)
        set this_process to item i of the process_list
        if this_process is not in white_list then
            do shell script "killall \"" & this_process & "\""
        end if
    end repeat
on error
    tell the current application to display dialog "An error has occurred!" & return & "This script will now quit" buttons {"Quit"} default button 1 with icon 0
end try



tell application "System Events"
    shut down
end tell
blue-sky
  • 51,962
  • 152
  • 427
  • 752
  • possible duplicate of [How to Safely Force Shutdown of Mac](http://stackoverflow.com/questions/1738070/how-to-safely-force-shutdown-of-mac) – bummi Sep 13 '14 at 13:19

4 Answers4

8

Could you try a simple applescript, which goes something like this...

tell application "System Events"
shut down
end tell

See if it works, and then you can make it run through Automator at certain time, etc.

notthetup
  • 1,129
  • 9
  • 17
  • Or wait.. is the problem that such a call gets cancelled due to all the other programs which cancel the sleep? – notthetup Apr 06 '11 at 11:40
  • Yes, thats the problem. The call seems to get cancelled because of other programs runnning, causing the shutdown to cancel. None of the above worked. Thanks for your comments. – blue-sky Apr 06 '11 at 23:45
  • OK. Can you try to see if running this script before doing the shut down helps to go around issue with apps canceling shutdown.. http://stackoverflow.com/questions/495323/quit-all-applications-using-applescript – notthetup Apr 07 '11 at 09:28
3

my solution (somwhat late). Just a bash script with apple in it:


#!/bin/bash
# OK, just shutdown all ... applications after n minutes
sudo shutdown -h +2 &
# Try normal shutdown in the meantime
osascript -e 'tell application "System Events" to shut down'

I also edited the /etc/sudoers (and /private/etc/sudoers) file(s) and added the line: ALL=NOPASSWD: /sbin/shutdown

Always worked for me for an assured shutdown (knock knock ;-) )

job
  • 31
  • 1
1

This should do:

do shell script "shutdown" with administrator privileges

If you want to pass the admin password from key chain, with no prompt:

do shell script "shutdown" with administrator privileges password "password here"

But do not store the admin password in clear anywhere. Instead use the keychain access.


Alternatively you could kill all user processes, via:

do shell script "kill -9 -1"

This however would also kill your own Applescript process, preventing it from requesting the shutdown/restart afterwards.

Either way you're playing with fire, when using sudo or kill.

Regexident
  • 29,441
  • 10
  • 93
  • 100
0

do what linux users do. use a bash script. if u dont know how to create one just go ahead and download ANY bash script u find using your internet search and open it with text edit app and paste the following:
( be careful if many people use the pc , then this method is not recommended, cause they can learn your user login password from inside this script )

#!/bin/bash

echo -n "Enter a number > "
read x
echo [your password] | sudo -S shutdown -h +$x

it will work the same way it works in linux. the terminal will pop up a message and ask you to enter a number. if we choose for exaple 50 , then the pc ( niresh ) or mac will shutdown in 50 minutes.