The «runningUserName» ...


I have an IDLE handler running on user A. How can I get the name of user A with a shell script even if it is not the frontmost user (i.e. even if it is in background) ?

Thanks in advance.

Robert Lespérance
Québec, Canada

If you have the UID of user A you can get the name with /usr/bin/dscl

Hi StefanK,

I have really no experience neither knowledge of shell scripting. Basic AS scripting is still a great universe to look into and I understand nothing to all those SS symbols and vocabulary. The best I can do would be:

set userName to (do shell script "/usr/bin/dscl") of "505"

… but I know that it is not the solution.



set UID to 505
set userName to word 1 of (do shell script "/usr/bin/dscl . -list /Users  uid | /usr/bin/grep " & UID)

Would have taken eternity to find that one … Maybe when AS will be better mastered I will try SS, but for now, time is of the essence.

Thanks to you I can have dinner with my family tonight :^) :^):^):^):^):^):^):^):^):^):^):^):^):^) !

… and if I have the userName is there a trick to get the userID ?

thanks, but Canada is a bit far away :wink:

set theUser to "test"
set UID to last word of (do shell script "/usr/bin/dscl . -read /Users/" & theUser & " UniqueID")

I know you said you wanted a shell script way, but since 10.4 there is a simple applescript way to get this information. Try this to see everything you can get.

set sysInfo to system info

Here’s how to get the short user name.

set shortUsername to short user name of (get system info)

Whenever you pass by …

To add to anybody’s routine library. For non shell scripting adept, here are 2 little and trivial handlers that are easier to remind:

-- Get the name of a user by it's number
on GetUserName(userID)
	set GetUserName to word 1 of (do shell script "/usr/bin/dscl . -list /Users  uid | /usr/bin/grep " & userID)
end GetUserName
-- Get the number of a user by it's name
on GetUserID(userName)
	set UID to last word of (do shell script "/usr/bin/dscl . -read /Users/" & userName & " UniqueID")
end GetUserID

Hope this can help somebody else …



Hi Regulus,

I was looking for a way to get to userID and userName of «any» user on my computer. Thanks for helping.


system info returns always information about the current user,
while dscl can determine names and UIDs of all users

Not to belabor the point but if user A was running the script it would return information for user A regardless of whether he is the current user. User A may be in the background but it would still return his info if the script is running in his user space.

I certainly see the benefit of the shell script but the original question did state the script was running in the user space of the user he wanted the information on.

I understand. Thank you for the point … So running your script would give the information of the user running the script. That was something else I was looking for.

I am trying to distinguish these concepts and find a way to get them easily by a shell script or an AppleScript:

¢ who is the «currentUser», meaning the user running a specific script (even if it is in background): you gave me your way (I will have to test it further …)
¢ who is the «activeUser», meaning the frontmost user: the shell script below is supposed to give me that, but my tests are giving me mixed result (I will also have to test it further …)

set activeUserName to (do shell script "stat -f '%Su' /dev/console")