Pick of the Week - Nov 10 [Show all picks]
Path Finder 5 - A feature-laden Finder replacement
Submit Hint Search The Forums LinksStatsPollsFAQHeadlinesRSS
12,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 0.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (0 votes cast)
 
[12,507 views]  

Return the short username in AppleScript | 10 comments | Create New Account
Click here to return to the 'Return the short username in AppleScript' hint
The following comments are owned by whomever posted them. This site is not responsible for what they say.
Return the short username in AppleScript
Authored by: Snaro on Mon, Mar 10 2003 at 11:10AM PST
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 Mon, Mar 10 2003 at 11:31AM PST
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: mactroll on Mon, Mar 10 2003 at 11:51AM PST
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 Mon, Mar 10 2003 at 12:49PM PST
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 | # ]
Return the short username in AppleScript
Authored by: robJ on Mon, Mar 10 2003 at 11:38AM PST
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 Mon, Mar 10 2003 at 1:42PM PST
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 Mon, Mar 10 2003 at 2:50PM PST
This works.

set user to do shell script "whoami"

[ Reply to This | # ]
Return the short username in AppleScript
Authored by: gatorparrots on Mon, Mar 10 2003 at 4:56PM PST
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 Tue, Mar 11 2003 at 8:03AM PST
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 Tue, Mar 25 2003 at 7:08PM PST
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 | # ]