Get the current username using Applescript

2008 March 31
by Scott

For various reasons while creating Applescripts, I've had a need for the script to know which computer it was on, since I am writing scripts for several co-workers to use. The easiest way to do so for me was to use each computer's username (the shortname OS X uses for the home directory). It's a pretty easy thing to extract from the home path:

[code lang="applescript"]
set userHome to (text of word -1 of (path to home folder as text))

display dialog userHome
[/code]

This has worked well for me to be able to purge the script's property settings whenever the script gets passed on to someone else (otherwise it seems to keep the property assignments on the new computer as well. Also, it's set up just in case I ever need to fork code for a particular user if their computer setup happens to have something that just won't work with the way the rest of the other computers are set up (hopefully that will not be necessary, but you just never know).

edit:
Well as usual, there's an easier way:

[code lang="applescript"]
set userHome to (short user name of (system info))

display dialog userHome
[/code]

No comments yet

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS