Determining remote home directory

Hello all,

I created a small program for the school I work for (high school) that will allow students on unmanaged laptops to connect to their network storage spaces without having to know any of the server details. We are running into a slight problem right now in that every year, the folder structure changes. We currently have two storage servers with two grades on each. Each year, those change (folders are renamed, seniors removed, freshman created). So some years we have a Freshman/Senior server, other Freshman/Sophomore, etc.

The program I made has two radio buttons which will, when selected, set the IP address of the machine to mount. What I’m wondering is, is it possible to have the code read the ‘Home’ attribute of the student’s profile so it can dynamically figure out where to connect them to?

-- Student Volume Mounter
-- Version 1.0
on clicked theObject
	if title of theObject = "Cancel" then
		quit
	else if title of theObject = "Log On" then
		
		set theWindow to window of theObject
		tell theWindow
			set passwd to contents of text field "passwd"
			set student_id to contents of text field "student_id"

-- I want to change these to something dynamic

			set server1 to "10.10.0.231"
			set server2 to "10.10.0.232"
-- End 
			if student_id is "" then
				display alert "You must supply a Student ID" attached to theWindow as critical
			else if passwd is "" then
				display alert "You must supply a password" attached to theWindow as critical
			else
				tell matrix 1 of theWindow
					set temp_var to (name of current cell) as string
					if temp_var is "Senior/Freshman" then
						set student_server to server2
					else if temp_var is "Junior/Sophomore" then
						set student_server to server1
					end if
				end tell
			end if
			
			-- First a check to see if we already are mounted.
			-- If so then unmount and mount with our new data.
			try
				do shell script "test -w /Volumes/" & student_id
				set MOUNTEXISTS to "true"
				
			on error
				set MOUNTEXISTS to "false"
			end try
			if MOUNTEXISTS is "true" then
				try
					do shell script "umount -f /Volumes/" & student_id
				on error
					try
						do shell script "rm -rf /Volumes/" & student_id
					end try
				end try
			end if
			
			-- MOUNTING THE SERVER
			-- the -o nobrowse option means that this mount will not be visible in the Finder. 
			-- delete this and it will be visible in the finder.
			
			try
				do shell script "mkdir -p /Volumes/" & student_id & " > /dev/null 2>&1 &"
				do shell script "mount_afp afp://" & student_id & ":" & passwd & "@" & student_server & "/" & student_id & " /Volumes/" & student_id
			on error
				display alert "There was a problem authenticating. Please make sure you have the correct username, password and class selected. 
				
If the problem persists, please contact your school's technical support" attached to theWindow as critical
			end try
			quit
		end tell
	end if
end clicked

Once the user is logged in you should be able to grab the $HOME environment variable with something like

set homedir to do shell script "set | grep HOME"
display dialog homedir

You will get some extra “garbage” but you should be able to get the home dir out of the string

This may not help with the original query (no directory services in my environment, sorry!), but I can point out the following more efficient way of getting the value of environment variables.

You can get the value of the environment variable directly from “AppleScript” (really the StandardAdditions extension):

system attribute "HOME"

If you really need to use the shell, this is a cleaner way of getting the value (no extra “garbage”):

do shell script "echo -nE \"$HOME\"" without altering line endings

Usually there should be no effective difference in the values returned by these methods. Theoretically, the latter invocation allows the shell to run an initialization file that might modify the values of environment variables, but this is unlikely.

Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari Version 3.1.2 (4525.22)
Operating System: Mac OS X (10.4)

I might be totally wrong, but the following code also returns the home directory:


POSIX path of (path to home folder)

I may be reading this original post the wrong way so forgive me.
If you’re just trying to determine what the network home path for a user is, why not reference the directory service?
If you’re using Active Directory or Open Directory, or some other LDAP variant, you can perform this lookup with a simple ldapsearch command.

If you’re students are logging in with their shortnames as defined by your directory service, you can take that value and pass it via the ldapsearch command to search on and return the network home path.

This is essentially what I am doing with a simple app I put together to search our Active Directory for user information.
Part of the return is their network home path which I then convert from the windows format to a string for OS X that can be copied and pasted in the “Connect to Server”

Sorry for the delay in posting back, things have been nutty on my end here.

I’m going to try all the suggestions and I’ll let you guys know which works well for us.

Thanks!

O.k., finally got some time to work on this, and here is what I did:

-- Student Volume Mounter
-- Version 2.0

on clicked theObject
	if title of theObject = "Cancel" then
		quit
	else if title of theObject = "Log On" then
		
		set theWindow to window of theObject
		tell theWindow
			set passwd to contents of text field "passwd"
			set student_id to contents of text field "student_id"
			
			if student_id is "" then
				display alert "You must supply a Student ID" attached to theWindow as critical
			else if passwd is "" then
				display alert "You must supply a password" attached to theWindow as critical
			end if
			
			-- First a check to see if we already are mounted.
			-- If so then unmount and mount with our new data.
			try
				do shell script "test -d /Volumes/" & student_id
				set MOUNTEXISTS to "true"
				
			on error
				set MOUNTEXISTS to "false"
			end try
			
			if MOUNTEXISTS is "true" then
				try
					do shell script "umount -f /Volumes/" & student_id
				on error
					try
						do shell script "rm -rf /Volumes/" & student_id
					end try
				end try
			end if
			
			-- MOUNTING THE SERVER
			-- the -o nobrowse option means that this mount will not be visible in the Finder. 
			-- delete this and it will be visible in the finder.
			
			try
				set theServer to do shell script "ldapsearch -h sbhslogin -u -x -b \"dc=sbhslogin,dc=sbhs,dc=org\" uid=" & student_id & " | grep homeDirectory | awk '{print substr($2,18,9)}'"
				set theGrade to do shell script "ldapsearch -h sbhslogin -u -x -b \"dc=sbhslogin,dc=sbhs,dc=org\" uid=" & student_id & " | grep description | awk '{gsub(\"description:\",\"\"); print}'"
				
				if theServer = "" or theGrade = "" then
					display alert "There was a problem authenticating. Please make sure you have the correct student number and password.

If the problem persists, please contact your school's technical support" attached to theWindow as critical
					quit
				end if
				
				do shell script "mkdir -p /Volumes/" & student_id & " > /dev/null 2>&1 &"
				do shell script "mount_afp afp://" & student_id & ":" & passwd & "@" & theServer & "/" & student_id & " /Volumes/" & student_id
				
			on error
				display alert "There was a problem authenticating. Please make sure you have the correct username, password and class selected. 
				
If the problem persists, please contact your school's technical support" attached to theWindow as critical
			end try
			quit
		end tell
	end if
end clicked

So now, instead of mounting first, it tries to find their OpenDirectory information, grabs the server name from the home directory field (which wraps and is really annoying that Apple’s implementation of AWK doesn’t use the -T switch to ignore line wrapping) and then also grab the description field, which is the grade level, and combine it all into the mounting command.

I think the only other thing I would like to do is add a dialog that has a progress bar or spinning wheel while it is doing all of this stuff, but I haven’t been able to figure out how to have the dialog open while letting the script run in the background…I found some examples of making a new window in IB and then hiding it, then tell it to show on click and stuff, but it got real complicated (still a newbie at this stuff) so I don’t know if there is an easier way.

Thanks everyone for helping out with this, the person this is for is going to be very happy.