How to Run an AppleScript On Wake Up

You can use Power Manager to run an AppleScript when you wake up your Mac.

Running an AppleScript when your Mac wakes up can be used to automate repetitive configuration tasks, such as mounting disks, or establishing a connection to a particular wireless network.

We are going to use Power Manager's trigger power on. This trigger fires whenever your Mac powers on from being asleep or from hibernation. This means you can use this event with MacBooks that are set to deep sleep. This trigger provides consistent behaviour when waking from sleep and deep sleep.

Create the Wake Triggered Event

  1. Launch AppleScript Editor: Applications > Utilities > AppleScript Editor.
  2. Copy and paste the following AppleScript into a new document:
    set myPathToTheApplicationToRun to "/Users/Shared/MyScript.app"
    
    tell application "/Library/Application Support/Power Manager/Agents/Power Manager Scripting.app"
    
        tell workshop
    
            set myEvent to make new event with properties {trigger ID:"myWakeEvent", name:"Run AppleScript on wake up"}
    
            -- Add the on-demand behaviour to aid testing
            set behaviours of myEvent to [can perform on demand]
    
            -- Create a power on trigger
            make new trigger power on at front of triggers of myEvent
    
            -- Create an launch application action
            set myApplication to make new application description with properties {path:myPathToTheApplicationToRun}
            set myAction to make new action launch application at front of actions of myEvent with properties {application description:myApplication}
    
            -- Add a slight delay before launching the application
            make new action await relative date at front of actions of myEvent with properties {seconds:3, quiet:yes}
    
        end tell
    
        -- Deploy the event
        tell Event Store to store these events myEvent
    
        -- Clean up
        tell the workshop to empty
    
    end tell
    
    An AppleScript for creating a wake trigger event in Power Manager.
  3. Save the script: File > Save.
  4. Run the script: Script > Run.

When run, this AppleScript will create a new event in Power Manager. The event will contain one trigger and two actions.

When your Mac powers on, the trigger will fire. The event will start performing its actions. The first action asks the event to wait three seconds. This time is needed to ensure your Mac is fully woken up and ready to launch new applications.

After three seconds, the second action is performed. It is this action that launches the application. By default your application will be launched as the front most user. If no-one is logged in, the application can not be launched and nothing will happen.

The action launches the application stored at /Users/Shared/MyScript.app. For this event, you should save your AppleScript as an application in this location.

If you need to run a process regardless of whether a user is logged in, consider using a shell script and Power Manager's action execute external.

A schedule wake event in Power Manager's System Preference.

Once the above AppleScript is run, the event will be created and scheduled. Thanks to the on-demand behaviour, you can test this event without needing to put your Mac to sleep. Test this event manually by using the system menu bar or your using Power Manager Remote as a trigger.

For the curious, you may like to see what happens when you set the await relative date's quiet property to no.

This article was posted in AppleScript, PM4, PMPRO4, Power Manager, and Recipe and tagged AppleScript, dssw, Power Manager, power on, recipe, and wake up on 2011-02-01.