Which user is active?

Hi there

Anyone know if a script can determine if it is being asked to run by (a) the current active user or (b) a user logged in running in the background? I have two user scripts competing to do the same/conflicting thing when a piece of hardware is plugged in and I would like the scripts to cooperate and defer to the current active user.

Thanks

something like this?


tell application "System Events" to get current user

You could use a shell script or the path to the home folder:

tell application "Finder"
	set x to do shell script "whoami"
	set y to path to home folder as text
	set y to word -1 of y
	display dialog x & " " & y
end tell

Thanks for these suggestions. But, so far as I understand matters, neither will allow the script to know if it is being asked to run by the active (ie front) user. It will give different responses according to the user that has called the script, but no-one is any the wiser as to whether the user calling the script is the front/active user or not.

I was hoping that there was a way through “System Events” to know the ‘active’ user, which I could then compare with ‘current’ user. By comparing the two, as the script, I would then know if I was being asked to run by the active user…

I believe this will work:

tell application "Finder"
   set x to do shell script "logname"
end tell

shell scripts don’t require the Finder :wink:

Thanks for this. I am not sure it is working. I have compiled this test script:

repeat with a from 1 to 100000
	tell application "Finder" to set activeUser to do shell script "logname"
	tell application "System Events" to set currentUser to name of current user
	if currentUser is not equal to activeUser then
		break
	end if
	delay 1
end repeat

If I run it as user 1 in script editor, the script loops continually as you would expect. If I then fast user switch to a different user 2 leaving the script running under user 1 and wait a few seconds, then switch back to user 1 - the script is still running, although it should have broken out of the loop because active user and current user would have diverged when I switched to user 2.

Am I missing something here?

Ok, I guess that didn’t work. This seems to ‘sort of’ work though:

set z to "Same user. :("
repeat with a from 1 to 20
	set activeUser to do shell script "last | grep 'still logged in'"
	set activeUser to word 1 of activeUser
	tell application "System Events" to set currentUser to name of current user
	if currentUser is not equal to activeUser then
		set z to "Different user!"
	end if
	delay 1
end repeat
display dialog z

It really only tests to see if another user is logged in. :frowning:

Nice one. As you say, it does not reveal whether the script is running from the ‘active’ user.

Curiously, when the script is run while under one use, activeUser and currentUser are the same (the second user) while when run under the first user (main administrator), activeUser returns as the second user while current user is the main administrator. Unfortunately, the results are the same in both cases whether or not the user is ‘active’ and so does not solve the problem.

Somewhere in the system, Mac OS must know who is active if only to send the keystrokes to the right user’s screen…

Yes, but until you find it, you could have the second user’s script write to a text file in a public folder when it starts, and erase the file when it finishes. Your first user’s script could check that file and act accordingly. It’s a kludge, but it should work. :wink:

Yeah but that won’t sort out the race to use the hardware plugged in, as it might be that the script in the inactive/background user gets running first and thereby squeezes out the user who REALLY needs the hardware!!!

Perhaps someone will have a flash of inspiration to help out over the weekend!!

Thanks for your thoughts. If I work it out, will repost here.

It will if each script tests the file at the beginning. Each script for each user could test to see if there is a user name contained in the file. If there is, and it doesn’t belong to that script, it should wait. If there isn’t, it could insert its user name and continue.

As you have two user scripts which do the same thing,
wouldn’t it be easier to run one script from the main library?

The problem I have is that I have a Fujitsu scanner which uses its own piece of software. If that software is running on one user’s account, when you run the software on another user it will refuse to work telling you that another user has the scanner under control.

I have Peripheral Vision running which detects when the scanner comes on line and auto launches a script which then launches the scanner software.

Only the active user will need to use the scanner.

If I can ensure that background users shut down their copy of the scanner software, I will be fine.

The first (crucial) step in that process is to work out who is the active user. The launch script would then only set the scanner software going if it is running from the active user account.

:slight_smile:

Without having a concrete suggestion, it seems to me that only a script at root level would “know” about the activities of all users, or you’d have to adjust the group permissions on that driver to exclude other users if your account was active.

Agreed.

If someone can point to the magic part of OS X which monitors who is the active user, I will gladly return to this problem. In the meantime, I have this kludge:

tell me to activate
ignoring application responses
	display dialog "Confirm Launch ScanSnap Manager" buttons {"Cancel", "OK"} default button 2 giving up after 30
end ignoring

if button returned of result is "OK" and gave up of result is false then
	try
		tell application "ScanSnap Manager" to launch
	end try
end if

When the scanner is turned on, this script is triggered simultaneously in all users by Peripheral Vision but only the active user can/will click on the confirm button. For the other background users, the display dialog will disappear harmlessly after 30 seconds, without launching ScanSnap Manager.

Seems to work fine, albeit relying on the smallest of user interaction (which I would have preferred to avoid)

It is late now…

I am definitely leaving this for tonight. But found this page:

http://www.radiotope.com/content/os-x-how-perform-action-during-fast-user-switch

…which looks like it may hold the key. How can applescript access/test/use this information?

set usr to do shell script "whoami"
set PIDofmyfinder to do shell script "ps -w -U " & usr & " | awk '/Finder/ {print $1}' | grep -m 1 ''"
set parentidofmyfinder to do shell script "ps -j -p " & PIDofmyfinder

Does this show the word “loginwindow” anywhere in parentidofmyfinder? If so, I may have a solution. It’s not working for me, but that may be my system (10.4.11)

Hi. I get this result:

“USER PID PPID PGID SESS JOBC STAT TT TIME COMMAND
macminia 115 80 115 5a2cea0 1 S ?? 0:54.54 /System/Library/CoreServices/Finder.app/Contents/MacOS/Finder -psn_0_77843”

Seems to me that what we need to isolate is what is called in Apple tech docs the “console user”. See http://developer.apple.com/technotes/tn2005/tn2083.html

Ok. How about with this?

set usr to do shell script "whoami"
set PIDofmyfinder to do shell script "ps -w -U " & usr & " | awk '/Finder/ {print $1}' | grep -m 1 ''"
set parentidofmyfinder to do shell script "ps -j -p " & PIDofmyfinder
set par to word 3 of paragraph 2 of parentidofmyfinder
set idnum to do shell script "ps -j -p " & par

Basically, I’ve noticed that if you look in the Activity Monitor, the parent process of the Finder for the ‘active’ user is the loginwindow. I’m trying to create a script that checks that, but ps doesn’t agree with the Activity Monitor on my system for some reason. :frowning: