Retrieve current user ID

Hi,

I found how to get the current user name. From there how can I get the current user ID number ? Nothing in this forum …

Thanks in advance.

Robert Lespérance

I’m NO Unix Wiz Kid by any means
So some nice scripter on here who is might be kind enough to add some
other fancy unix’ie stuff to this to single out just what your after, but i know you can use “id” to get the User info:

do shell script "id"
display dialog result

Thank you Pidge …

With a little tweak I got it. Probably not a pro script … but this is a start:

get text 5 thru 7 of (do shell script "id")

Regards.

Robert Lespérance

Try something like this:

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

Alternatively:

user ID of (system info)

Thank you Bruce,

Can I also get the ID of the other logged in users ?

Regards.

Robert Lespérance

You can get the UIDs only (AFAIK) with Remote Apple Events


property user_name : "user"
property pass_word : "¢¢¢¢"

set thisComputer to host name of (system info)
-- checks all user IDs between 500 and 599
set allUsers to words of (do shell script "/usr/bin/dscl . -list /Users  uid | /usr/bin/grep \"5[0-9][0-9]\"")

set loggedinUsers to {}
set eppc to "eppc://" & user_name & ":" & pass_word & "@" & thisComputer & "/Finder?uid="
repeat with i from 1 to (count allUsers) by 2
	set m to eppc & item (i + 1) of allUsers
	using terms from application "Finder"
		try
			tell application m to get (desktop as string)
			set end of loggedinUsers to {item i of allUsers, item (i + 1) of allUsers}
		end try
	end using terms from
end repeat

loggedinUsers contains {{user1, uid1}, {user2, uid2}.}

For reference:

do shell script "/usr/bin/dscl . -list /Users uid" & ¬
	" | /usr/bin/awk '{ print $2, $1 }'" & ¬
	" | /usr/bin/grep -e '^[5-9][0-9]\\{2\\} ' -e '^[1-9][0-9]\\{3,\\} '" & ¬
	" | /usr/bin/sort --numeric-sort" & ¬
	" | /usr/bin/awk '{ print $1; print $2 }'"
set example to paragraphs of result
--> Example: {"501", "bruce", "502", "_privoxy"}

Hi StefanK and Bruce,

Thank you for your help … StefanK, your script works perfectly. As for Bruce’s script, it does not give any result other than «{“”}». Maybe I don’t understand it’s purpose.

Regards.

Robert

Hi Bruce,

I need a script that would return 2 types of userID:

  1. the frontmost logged in userID (logged in but in foreground)
  2. the specific userID of the user where an app or script is running even if it is in background (logged in but either in background or foreground)

What script would return those 2 userID ?

I thought that your script: do shell script “/usr/bin/id -u” would give me the first. Am I wrong ?

Said in other terms, considering this handler:

on idle
set currentUser to (do shell script "/usr/bin/id -u")
get currentUser
end idle

Let’s say I have 2 logged in users. If “508” is in background and 505 in foreground and if I have the above «Idle» handler running on each user, should I expect that both handlers would return “505” ?

Regards.

Robert

I