Which user is active?

I get the following answer:

"USER       PID  PPID  PGID   SESS JOBC STAT   TT       TIME COMMAND
macminia    80     1    80 5a2cea0    0 Ss     ??    0:02.10 /sbin/launchd"

Does that help?

Maybe, but I’ve got a better solution! I can’t take credit for it, as I got it here:
http://forums.macosxhints.com/showthread.php?p=480457

set mylist to {}
repeat with i from 1 to 3
	set x to do shell script "stat -f  '%Su' /dev/console"
	set mylist to mylist & x
	delay 10
end repeat
mylist

To test this, get the script ready to run, bring up the login screen for another user and type the password, then click run and quickly finish the login. Wait at least ten seconds and switch back. The results (mylist) should both users.

Pretty clever :slight_smile:

This looks like the bizzo. Will incorporate into my script and repost when fully tested.

Many thanks:)

With the rather tricky bit of the jigsaw now solved with your help, the script works impeccably:

tell application "Finder"
	get name of every process
end tell

if result contains "ScanSnap Manager" then
	set runningTest to true
else
	set runningTest to false
end if

set {activeUser, currentUser} to {do shell script "stat -f '%Su' /dev/console", do shell script "logname"}
if activeUser is equal to currentUser then
	if not runningTest then
		delay 2
		try
			tell application "ScanSnap Manager" to launch
		end try
	end if
else
	try
		tell application "ScanSnap Manager" to quit
	end try
end if


Allowing a script to know whether it is being run by the active user could have a number of other uses, I am sure.

Many many thanks

Good software can be installed globally and can also handle multiple users.
Shame on Fujitsu :wink:

Hi kiwilegal,

Eureka … I was working on this same problem for years ! I experimented the exact same problem with my HP OfficeJet G85 printer. This printer is not supported anymore by HP and since it works perfectly I tried to find a workaround. I found out that HP runs in background in every logged in user a little piece of software to manage the printer. It was crashing my computer when left open or when my computer went to sleep for a long time. It also created conflicts when trying to print from one user and if the little app was also running in another user. That piece of software is “HP Communications”.

I found a partial solution 2 years ago. My solution was to quit the app when the screen saver engine was running. That solved the problem perfectly for that user and everything was OK until I created another user that I did not use very often. I found another temporary solution, but now that we work in both users more often I have to find a solution that would trigger whenever I would switch from one user to another.

This thread gave me the solution to my problem. Thanks to all of you and specialy to kiwilegal for having kept faith and have never given up !!

But there is still one more thing … this script:

do shell script "stat -f '%Su' /dev/console"

gives the log name of the «active user». I want to do the same thing but by identifying the user by it’s userID. How can I script that ? I tried the script below, but it does not work:

do shell script "/usr/bin/id -u"

Thansk all for all … Regards.

Robert

Hi,

Do I understand that:

¢ «activeUser» is the frontmost logged in user
¢ «currentUser» is the user running the script

Thanks in advance.

Robert Lespérance

I would like to help, but my shell scripting skills are very limited.

Re Rob’s question:

The answer is that Rob’s understanding is correct.

Cheers

Thank you.

Hi kiwilegal,

Running this line of your script:

set {activeUser, currentUser} to {do shell script "stat -f '%Su' /dev/console", do shell script "logname"}

returns variably “root” and not only the user on which a specific app or script is running. Could that mean that do shell script “logname” returns the owner of the process running at the execution of the script ?

Regards.

Robert

Hi Rob

The do shell script “logname” will return the name of the user who has triggered the script which contains the do shell script “logname” code.

To take another example, if you manually run the do shell script “logname” in Script Editor, you should get the name of the user you are logged in as. Manually running do shell script “stat -f ‘%Su’ /dev/console” in Script Editor will invariably produce the same user name as do shell script “logname”, because the current user and active user will necessarily coincide.

But the result should not be root. Not sure why this is happening for you, unless you are somehow running things as root user (sudo?).

The script I posted earlier in this thread has worked flawlessly for me with multiple users using my (clearly not multiple user designed) scanning driver software.

From the problems you have, I would have thought that my script would have worked for you. Do you use Lingon? If you had the script polling every ten seconds or so per user, that should sort you out. Alternatively have the script triggered as you switch between users.

Cheers

If we use your convention of:

«currentUser» is the user where the script is running (I call it «runningUser»)
«activeUser» is the frontmost user (I call it «frontmostUser»)

Running the script above from the ScriptEditor, by pressing many times the run button and looking to see any changes in the result, returns variably “activeUser” or “root”. No Lingon nor sudo commands.

I made a test with these 2 scripts:

delay 60
set {frontmostUser, runningUser} to {do shell script "stat -f '%Su' /dev/console", do shell script "logname"}
delay 60
tell application "Finder" to set frontmostUser to do shell script "stat -f '%Su' /dev/console"
tell application "System Events" to set runningUser to name of current user
get {frontmostUser, runningUser}

I placed each script in a distinct ScriptEditor window. I pressed the run button and switched back to my other user (it would become the «activeUser». After the script have completed, I switched back to the user where the scripts were triggered to see what would be the result in the bottom of each script window. This is the result of my test:

Script1: {“activeUser”,“root”}
Script2: {“activeUser”, “currentUser”}

On my computer Script2 seems to return invariably the result I am lokking for. As for the “root” problem, I don’t know what is the problem.

Can I asked you to try the test I did ?

Thanks for helping.

Robert

Your script could still be working because «root» is different from «activeUser» …

Hi there

I just ran the two scripts and they returned the same results. Both were as I would expect, ie {“activeUser”, “currentUser”}. If script version 2 works for you, perhaps go with that. But it is curious that I get the same result with both scripts but you do not.

I am running Leopard v 10.5.5 on an intel Mac Mini.

Cheers

I run Tiger 10.4.11 … Maybe that’s it !

Good you got it to work, anyway

Cheers

Thanks for your thread and your help … !