Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've configured a local user account on an iMac (Lion) to immediately launch Firefox to a specific page after logging in – the page contains a survey to be completed by the user. This works fine.

What I want to do next but can't seem to figure out how is to automatically log off the user after they've completed the survey and they exit Firefox.

I don't want them to have to click the Apple icon at the top and then logout – when they finish the survey and exit Firefox, I'd like a script to execute that logs them out and takes the Mac back to the login screen for the next user.

Is it possible to associate a script and have it execute upon exiting an application?

share|improve this question

Save a property list like this as ~/Library/LaunchAgents/com.superuser.457047.plist (or whatever you change the label to), and load it with launchctl load ~/Library/LaunchAgents/com.superuser.457047.plist or by logging out and back in.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd>
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.superuser.457047</string>
    <key>ProgramArguments</key>
    <array>     
        <string>osascript</string>
        <string>-e</string>
        <string>tell application "System Events" to if (not (exists process "firefox")) or number of windows of process "firefox" is 0 then tell process "Finder" to click last menu item of menu 1 of menu bar 1</string>
    </array>
    <key>StartInterval</key>
    <integer>5</integer> <!-- run every 5 seconds -->
</dict>
</plist>

There's probably some cases where clicking the last menu item wouldn't work. keystroke "q" using {option down, shift down, command down} wouldn't work if the user was holding control.

tell application "System Events" to log out would show a confirmation dialog.

/System/Library/CoreServices/Menu\ Extras/user.menu/Contents/Resources/CGSession -suspend doesn't show a confirmation dialog but keeps the user logged in and shows the fast user switching screen.

share|improve this answer

Keyboard Maestro can be configured to execute scripts when an application closes.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .