How to Shut Down Your Mac Using AppleScript

You can automatically shut down your Mac using AppleScript. The following AppleScript snippet shows the simplest method:

 tell application "Finder"

   shut down

 end tell

Short and simple but prone to problems

The above AppleScript asks the Finder to begin the shut down process. Sadly this short and simple method is not perfect.

  • The AppleScript requires a user to be logged into the computer.
  • The Finder's shut down can be stopped by running application asking questions.
  • The AppleScript provides no warning to the active user.

Power Manager supports AppleScript and provides a robust shut down action. We will use this action to ensure the Mac is shut down.

To be nice to anyone using our Mac, we want to provide a little notification before shutting down. Let's create an event that will shut down your Mac in ten minutes time. To do this, we can use AppleScript to create a scheduled shut down event.

Our event will:

  • Provide a warning to any active users.
  • Allow administrators to cancel the shut down.
  • Shut down even if no-one is logged in.
  • Shut down even if an application attempts to block the process.
  • Wake up a sleeping Mac when the shut down is due.

How to Create a Shut Down Event in Power Manager

  1. Launch AppleScript Editor: Applications > Utilities > Script Editor.
  2. Copy and paste the AppleScript that follows these steps into a new document. Automatically shut down your Mac in ten minutes with Power Manager
  3. Save the script: File > Save.
  4. Run the script: Script > Run.

The AppleScript to copy and paste into Script

 tell application "Power Manager"

   tell workshop

     set myEvent to make new event with properties {id:"myEvent", name:"Shut down soon"}

     -- Create a warn action before shutting down
     make new action warn at front of actions of myEvent

     -- Create a shut down action after the warning
     make new action shut down at end of actions of myEvent

     -- Create a once trigger in ten minutes from now to perform the actions
     make new trigger once at front of triggers of myEvent with properties {date:((current date) + (60 * 10))}

   end tell

   -- Deploy the event
   tell Event Store to store these events myEvent

   -- Clean up
   tell the workshop to empty

 end tell

The AppleScript works by creating a new Power Manager event in the workshop. Two actions are added to the event; a warn action and a shut down action.

The warn action provides a final warning to anyone looking at the Mac as the event is being performed. The warning provides observers with a visual hint that the shut down is planned and is not a problem.

The shut down action begins Power Manager's shut down process; this process is slightly different from the Finder's equivalent. Power Manager performs extra steps to ensure nothing stops the shut down from occurring.

To complete the event a once trigger is added. The once trigger will trigger the event once on a specific time and date. In this case, the current date plus ten minutes is used.

Finally, the finished event is deployed from the workshop to Power Manager for scheduling.

When run the script will cause Power Manager to show a notification to active users, and a separate notification on the login window to warn prospective users. The notification counts down until it is time to shut down.

Power Manager provides a notification before shutting down

Just before shutting down a large shut down image is displayed and moments later the shut down begins. Within a few seconds of the event being triggered, your Mac will have shut down. All this is achieved with a short AppleScript.

This article was posted in , , , and and tagged , , , , and .

Published by Graham Miln on