1

Is there a way, via the command line, to check if the current user has the desktop? That is, if I run a shell script in a window, I want to be able to tell (in the script) if the user has switched to another account via Fast User Switching. The shell script is still running in that command window, but since the user has switched to another account, the Desktop doesn't show that command window.

I share my Mac with my wife, and we each have our own accounts. When she sits down at the computer, she switches to her account. I want to write a Python program that runs 24/7 on my account. I want that program to know when my wife is at the computer and has switched to her account.

I hope I'm explaining this clearly. I searched on Google for 20 minutes before giving up, as none of the search results were even close to what I was trying to find. I don't even know how to phrase the question properly.

0

You can check who has the "console" with command:

ps aux | grep console | grep -v grep | awk '{ print $1 }'
  • 1
    This doesn't work. I wrote this script: while [ 1 ]; do ps aux | grep console | grep -v grep | awk '{ print $1 }'; sleep 1; done and it always prints my login name, even when I switch to another user. – John Smith Nov 13 '14 at 1:42
0

You can if you create a "login hook." A login hook tells Mac OS X to execute a certain script when a user logs in. Unlike Startup Items that open when a user logs in, a login hook is a script that executes as root.

With a login hook:

  • The script specified as a login hook must be executable.
  • The login hook will be run as root.
  • In the login hook script, the variable $1 returns the short name of the user who is logging in.
  • Other login actions wait until the hook has completely executed.

How to set up a login hook

  1. Open Terminal (Applications/Utilities).
  2. In the Terminal window, type:

    sudo defaults write com.apple.loginwindow LoginHook /path/to/script

    (where /path/to/script is the full path to the script that you want to execute when a user logs in—it doesn't have to be in the user's Home directory).

    This modifies the /var/root/Library/Preferences/com.apple.loginwindow file.

  3. Type your password at the prompt, then press Return.

Full article on Apple Support Page: Mac OS X: Creating a login hook

  • 1
    Does login hook work with Fast User Switching? – Mateusz Szlosek Nov 12 '14 at 14:51
  • I tested it some time ago and it worked. – jherran Nov 12 '14 at 14:58
  • 1
    This doesn't appear to work either. I wrote this script: $ cat /usr/local/bin/loginhook #!/bin/sh echo $1 >> /tmp/login.txt but it doesn't create /tmp/login.txt when I fast-switch to another user. And yes, /usr/local/bin/loginhook is executable. – John Smith Nov 13 '14 at 1:48
0

Ok, I think I figured it out, but I don't know how it works. The following Python function returns one value when I'm the active login, and another when I'm not:

from Quartz import CGMainDisplayID
print CGMainDisplayID()

All I need to do is store the value of CGMainDisplayID() when the script first runs, and then call it again whenever I want to see if I'm switched away. If it returns a different value, then I know I'm switched away.

You must log in to answer this question.

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