Return User Name

At this point, this is an academic question. However, out of curiosity and for my own edification, I would love the answer to this question. Any help that anyone can offer would be appreciated.

I just wrote a script that does a backup of a specific folder to a specified server. It looks in a specific directory on a server and checks for a folder with the name based on the user name. If it is not there, it creates it. It then backs a specific folder up to that directory.

I needed to come up with the user name. I found this bit of code on the internet that works.

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)

This seemed to be over kill. I was thinking there has to be a easier way to return the user (account) name. With some playing around, I came up with this segment, that returned the user name:

tell application "System Events"
	set the_user to name of current user as string
end tell

However, when I use this segment of code, I get an error later on in my script, and I can’t figure out why. If I just run this portion of the code, and I ask it to display the contents of the variable “the_user” both pieces of code return the same exact result. However, when I use the code in the actual script, I get an error message later on down the script. (It’s not concatenating the path correctly.)

Here is the full script. You should be able to parse the information you need from this script. Note, login information to the servers and IP addresses fake. Good luck. I hope this helps.

set startup_disk to path to startup disk as string

set NotesData_Path to startup_disk & "Applications:Lotus Notes:Data:"

tell application "Finder" -->Mount backup server
		mount volume "afp://xxx:xxx@"
	end try
end tell

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)
-->return the login name of the computer

set backup_path to "UserData:MCE17:"
-->set variable to the backup path to the server

set user_folder_path to backup_path & the_user & ":"
-->Create path to user backup folder

	alias user_folder_path
	tell application "Finder"
		duplicate folder NotesData_Path to user_folder_path replacing yes
	end tell
	--> file exists, go ahead and copy the data folder to the server. If the folder exists, replace it.
on error --> file doesn't exist, create a folder based on the user account name
	-->copy the the data folder to that folder.
	tell application "Finder"
		make new folder at backup_path with properties {name:the_user}
		delay 3
		duplicate folder NotesData_Path to user_folder_path replacing yes
	end tell
end try

delay 60

tell application "Finder" -->Dismount User Data Volume
	eject disk "UserData"
end tell

If the shortname is enough, use

set ShortName to system attribute "USER"

If you want the long name, use the line above plus:

set FullName to (do shell script "finger " & ShortName & " | grep Login | colrm 1 46")

Addendum: the command “system attribute” by itself will show the available attributes (which include “USER”)

Both these methods are helpful. Thank you. But as I pointed out in my original post, since I already figured a way to get the script working, the question is purely academic. What I’m trying to basically figure out is why both methods I outlined produced the same result in the variable the_user, yet one method causes the script to fail, and the other method works fine.

I am hoping by understanding why the one method fails, it will help me avoid problems in the future.

Thanks so much for your response.

System Events doesn’t work at all, for me. (Mac OS X 10.4.2)

You could also this:

do shell script "whoami"

You can do this with one line:

do shell script "finger `whoami` | grep Name | colrm 1 46"

BTW, I didn’t know about ‘colrm’. Thanks for revealing that to me.

set longName to (do shell script "/usr/bin/nidump passwd . | /usr/bin/grep `whoami` | /usr/bin/awk -F ':' '{print  $8}'")

Sorry, just had to post another way for the heck of it. (I’m an awk junkie, so accept my apologies!)

In all seriousness, though:

Please remember, guys, when calling shell apps in a shell script or AppleScript app, /use/the/whole/path. (You may want to declare the full path as a variable name at the beginning of yer code to make it easier! :-))

I don’t do this when I post here, because it’s shorter, and because some people might have a different file structure. E.g., does this work for anyone?

do shell script "/Library/Ruby/bin/ruby -e 'puts \"Hello, World!\"'"

That said, it is the proper way to do things. However, you should be aware that some commands are built into the shell (such as cd) and don’t have a full path.

Yet another method:

do shell script "echo $USER"

As for declaring the full path, I don’t. Theres no need to imo.

do shell script "echo 'print \"hello world\"' | perl"
do shell script "echo 'print \"hello world\"' | ruby"
do shell script "echo 'print \"hello world\"' | python"

All work fine for me, as well as:

do shell script "sw_vers | sed '2!d' | awk '{print substr($2,4,1)}'"

to get the system version :stuck_out_tongue:

Hi Wexie,

I’m just a newbie but I had a similar problem with concatenating a path. Where you had:

set NotesData_Path to startup_disk & "Applications:Lotus Notes:Data:"

I think it should have been:

set NotesData_Path to startup_disk & ":Applications:Lotus Notes:Data:"

Without the leading “:” the line before this set startup_disk to the name of the boot disk but only the name, thus if the disk was named “General” then the concatenated result is “GeneralApplications:Lotus Notes:Data:” and you will get an error later refering to the GeneralApplications Blah Blah.

Add the “:” and you get NotesData_Path to equal “General:Applications:Lotus Notes:Data:” which should solve the problem.

As a newbie, I hope this helps,

Duke Stewart.

Model: iBook
AppleScript: Version 2.1 (80)
Browser: Safari 412.5
Operating System: Mac OS X (10.4)