Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Return the short username in AppleScript Apps
I don't recall ever seeing anything like this. I find it to be a handy addition to AppleScripts in OS X:
set the_user to the second word of (characters (offset of "Users" in path to 
  preferences as string) through (length of (path to preferences as string)) of 
  (path to preferences as string) as string)
display dialog "The current user is " & the_user
A simple little thing that might be of benefit.
    •    
  • Currently 2.00 / 5
  You rated: 3 / 5 (6 votes cast)
 
[26,580 views]  

Return the short username in AppleScript | 12 comments | Create New Account
Click here to return to the 'Return the short username in AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Return the short username in AppleScript
Authored by: Snaro on Mar 10, '03 11:10:01AM
You could also use this way:
system attribute "USER"
There are other variables available with system attribute:
"HOME": posix path to users home directory
"SHELL": posix path to users shell
"USER": users short name
"LANG": user system language as two-letter code e.g. "en_US"
"PATH": content of the PATH variable
"__CF_USER_TEXT_ENCODING": Don't know the meaning of this


[ Reply to This | # ]
Return the short username in AppleScript
Authored by: rbest on Mar 10, '03 11:31:53AM

I like the idea of user the 'system attribute' but forgive my ignorance, how do I USE it? (an example would be great)
Thanks!



[ Reply to This | # ]
Return the short username in AppleScript
Authored by: Anonymous on Mar 10, '03 11:51:46AM

Another option that I use a lot:

set myname to do shell script ("whoami")

Now myname is your shortname.

For other information, like path, you can pull that from the environment, but for username it's probably simpler to just use whoami.

Joel



[ Reply to This | # ]
Return the short username in AppleScript
Authored by: Eravau on Mar 10, '03 12:49:51PM
How to use it:
   set short_name to system attribute "USER"
Now the variable short_name will contain your short user name.

[ Reply to This | # ]
The AppleScript way
Authored by: Rainy Day on Mar 30, '12 02:02:34PM
Or:

set userName to short user name of (system info)


[ Reply to This | # ]
Return the short username in AppleScript
Authored by: robJ on Mar 10, '03 11:38:22AM
This works for me in OS X 10.2.4:

display dialog "The current user is " & last word of (path to current user folder as text) & "."


[ Reply to This | # ]
Return the short username in AppleScript
Authored by: cougar718 on Mar 10, '03 01:42:52PM
Hello all, I would use this instead...

-- Get the current user of the machine
set theuser to (do shell script "echo $USER")
display dialog "Current User of Machine Is : " & theuser buttons {"Ok"} default button 1 giving up after 5 with icon note

---
Rick Bargerhuff alias cougar
Programmer / Developer
Apple Technician / Specialist / Web Designer
Personal Email: cougar718@comcast.net

[ Reply to This | # ]

Return the short username in AppleScript
Authored by: stevanreese on Mar 10, '03 02:50:20PM

This works.

set user to do shell script "whoami"



[ Reply to This | # ]
Return the short username in AppleScript
Authored by: gatorparrots on Mar 10, '03 04:56:24PM
That may not work for scripts using sudo (or sudo -s) or the 'with administrator privileges' syntax in AppleScript (the same problem goes for any of these possiblities: $USER, 'users' and 'logname' -- there are situations where these facilites will return 'root' in response. To get the currently logged-in GUI username, a more reliable method would be this: who | grep console | awk '{print $1}' To call it from an AppleScript: set theuser to (do shell script "who | grep console | awk '{print $1}'")

[ Reply to This | # ]
Return the short username in AppleScript
Authored by: cyberelf on Mar 11, '03 08:03:42AM

This only works if you're using the default home directory.
However, if your home directory is something else (I prefer to use a directory named as my full name bicapitalized without spaces on a separate volume), this script won't work at all.
Using 'do shell script "whoami"' is much more reliable.



[ Reply to This | # ]
Return the short username in AppleScript
Authored by: snourse on Mar 25, '03 07:08:14PM

What would be nice in a school web setting is to display the username as a watermark over the desktop picture, or perhaps in the menu bar.

It would be nice to know who is logged in- it can be a problem with students logging in as another student.

It seems like it wouldn't be that difficult- Applescript Studio perhaps? Does it allow one to define window opacity, application visibility (no dock), and text opacity?



[ Reply to This | # ]
Return the short username in AppleScript
Authored by: Zilliquister on Oct 11, '11 08:57:14AM

Wouldn't it be easier to just do this instead?

set myname to path to home folder as string
set my_name to third word of (myname)

display dialog "My name is " & my_name



[ Reply to This | # ]