How to Run a Script Within Every User Session

Ever wanted to run a script within every user session on Mac OS X? The chances are you have not. The need tends to be rare, but when needed it is essential. Until Power Manager Professional 4, being able to run a script within every user session was beyond the reach of most administrators.

Having the ability to run scripts and applications within a specific user session is important on Mac OS X. It is important because the script, or application, being run needs to reside within the correct session in order to have access to the windowing system of the associated user. It is possible run a script as a specific user, but if the script is not within the user session it will not to be able to display windows or interact with the user.

Power Manager lets you run a script, launch an application, or open documents within every user session. This is achieved surprisingly easily thanks to a special short user name provided: PME:logged in.

How to Modify an Event to Run Within Every User Session

  1. Create an event that launches an application.
    Create an event to launch an application using Power Manager Professional
  2. Display the Application section's optional fields: Action cog > Optional Fields
    Show the Application's optional fields in Power Manager Professional
  3. Select or type: PME:logged in for the User field.
    Select PME:logged in for the optional User field
  4. Save and deploy your event.

When performed, your selected application will be launched in each graphical user session. If no-one is logged in, the application will not be launched.

You can test this behaviour using a small AppleScript saved as an application. The following AppleScript displays a dialog box containing the current user's short name:

tell application "Finder"

    set shortName to do shell script "whoami"
    display dialog "Hello " & shortName

end tell
AppleScript to show a dialog containing the user's short name

Use AppleScript Editor to save this script as an application.

Create an event to run this AppleScript; remember to set the user to PME:logged in; this tells Power Manager to perform the event once per logged in user.

Next use Fast User Switching to log into two separate accounts on a single Mac. With two accounts logged in, one active and the other in the background, the computer has two graphical user sessions.

Trigger your event and note the dialog appears with the front most user's short name.

Fast User Switch to the background user and you will discover the second user also has a waiting dialog. This second dialog shows the name of the second user; proving each instance of AppleScript application is separate and running within the correct user session.

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

Published by Graham Miln on