check if user has SMB fileserver mounted

Here’s the situation: I’m setting up a Mac lab where users will log in as Guest. They will then mount a server volume via SMB where their coursework files can be stored.

The students are using Eclipse (programming environment) which requires a “workspace” directory. This needs to be individualized for each student. I need a script that does the following:

  1. If user is ‘guest’, check if server is mounted
  2. If not mounted, prompt user to log in, exit script
  3. If server is mounted, write Eclipse config file with “workspace” path pointing to the user’s network share
  4. Launch Eclipse

What I’ve come up with is far from foolproof. Is there some smarter way to check that an SMB share is mounted? If I could find out what user name they’re logged in with that would be even better.

Mac OS has an unfortunate tendency to leave behind an empty mount point folder in “/Volumes/” after disconnecting from a server. This means we can’t rely on listing contents of “/Volumes/” to see if we’re connected to the server. If you connect to “server_share” and folder /Volumes/server_share already exists, the real mount point will be renamed “/Volumes/server_share-1”, which makes this method kind of fudgy.

set theServer to "smb://"
set whoAmI to (do shell script "whoami")

if whoAmI = "guest" then -- check for mounted volumes
	tell application "Finder"
		set theVolumes to ((every disk whose ((local volume = false) and (name ≠ "home" and name ≠ "net")))) as list -- find mounted server volume
		set myHD to (name of startup disk)
	end tell
	set myCount to (count theVolumes) -- 
	if (myCount > 0) then -- share is mounted
		set myNetHome to (name of (item 1 of theVolumes)) -- this *should* be the same as the username, but not foolproof
		set myWorkspace to "/Volumes/" & myNetHome & "/workspace/"
		do shell script "open -a /.hiddenApps/Eclipse/" -- Eclipse is in a hidden directory, because I don't want students opening it directly. This also hides it from Spotlight
		display dialog "Please connect to the server, then launch Eclipse again."
		open location theServer
	end if
	set myWorkspace to "/Users/" & whoAmI & "/Documents/workspace/" -- create workspace & config file in user's local home
	do shell script "open -a /.hiddenApps/Eclipse/"
end if

on createConfig(myWorkspace)
	set {configStart, configEnd} to {"#This configuration file was written by: org.eclipse.equinox.internal.frameworkadmin.equinox.EquinoxFwConfigFileParser
#Thu Feb 19 03:26:41 EST 2015
org.eclipse.update.reconcile=false", "osgi.framework=file\\:plugins/org.eclipse.osgi_3.10.2.v20150203-1939.jar
	set configSpace to "osgi.instance.area.default=" & myWorkspace -- this is the line in the config.ini file that tells Eclipse where the workspace is
	do shell script "touch ~/Documents/config.ini" -- make a new config file in the user's Documents folder
	do shell script "mkdir -p " & myWorkspace -- create the workspace directory
	tell application "Finder"
		set myConfig to (((path to documents folder) as string) & "config.ini")
	end tell
	do shell script "ln -s -f ~/Documents/config.ini /.hiddenApps/Eclipse/configuration/config.ini" -- a symlink seems safer than writing to the actual Eclipse app directory, but maybe not necessary
		close access alias myConfig -- in case it's already open
	end try
	open for access alias myConfig with write permission
	write (configStart & return & configSpace & return & configEnd) to alias myConfig starting at eof
	close access alias myConfig
end createConfig

Browser: Safari 537.36
Operating System: Mac OS X (10.10)