How to Log Out Users With AppleScript

Being able to log out a user on Mac OS X with AppleScript is useful. A common reason for needing to log out a user is to ensure a shared Mac is freed up for other users. The simplest way of logging out an active user is shown in the AppleScript snippet below. The script asks the System Events process to begin the log out process.

Being able to log out a user on Mac OS X with AppleScript is useful. A common reason for needing to log out a user is to ensure a shared Mac is freed up for other users.

The simplest way of logging out an active user is shown in the AppleScript snippet below. The script asks the System Events process to begin logging out.

tell application "System Events"

    log out

end tell

Fragile AppleScript for logging out

This script, though simple, is not perfect and is not robust enough to use in situations where the user might cancel the log out, or where a rogue application may block the log out.

Over the years we have become experts at automating the log out process. Thankfully all that experience is encapsulated in Power Manager for you to call upon as needed.

Let’s look at how to use AppleScript with Power Manager to force all users logged into a Mac to log out - whether they want to or not. Our solution can claim the following improvements over the AppleScript snippet above:

  • Active users will be warned of the pending log out.
  • Users logged in but Fast User Switched will also be logged out.
  • Administrators will have the opportunity to cancel or adjust the log out.
  • The script will work even if the login window is front most.
  • The log out can not be stopped by standard users.
  • The log out will not be stopped by application dialog boxes.
  • The log out will not be stopped by stalled or rogue applications.
  • The script will wake up the Mac when the log out is due.

How to Create a Log Out Event in Power Manager

  1. Launch AppleScript Editor: Applications > Utilities > AppleScript Editor.

  2. Copy and paste the following AppleScript into a new document:

    tell application "Power Manager Scripting"
    
        tell workshop
    
            set myEvent to make new event with properties {trigger ID:"myLogOutEvent", name:"Log out"}
    
            -- Create short notification delay before logging out
            make new action await relative date with properties {seconds:60, availability:wake up} at front of actions of myEvent
    
            -- Create a warn action before logging out
            make new action warn at end of actions of myEvent
    
            -- Create a log out action after the warning
            make new action log out at end of actions of myEvent
    
            -- Mark the event has having on-demand behaviour
            set behaviours of myEvent to [can perform on demand]
    
        end tell
    
        -- Deploy the event
        tell Event Store to store these events myEvent
    
        -- Clean up
        tell the workshop to empty
    
    end tell
    

    Robust AppleScript for automatically logging users out of Mac OS X

  3. Save the script: File > Save.

  4. Run the script: Script > Run.

When you ran the script, you created a new event in Power Manager. This event has no triggers, so it will not be automatically scheduled. Instead this event has on-demand behaviour. Your new ‘Log out’ event will immediately appear in Power Manager’s interface.

A one minute notification alerts active users

An on-demand event can be triggered using one of the following methods:

Trigger the log out from your iPhone with Power Manager Remote

If you want to schedule this event to occur at a specific date and time, or on specific days, go back and edit the AppleScript to add one or more triggers. The AppleScript included in the shut down recipe includes a trigger for its event.