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://fqdn.of.my.server"
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/"
		createConfig(myWorkspace)
		do shell script "open -a /.hiddenApps/Eclipse/Eclipse.app" -- Eclipse is in a hidden directory, because I don't want students opening it directly. This also hides it from Spotlight
	else
		display dialog "Please connect to the server, then launch Eclipse again."
		open location theServer
	end if
else
	set myWorkspace to "/Users/" & whoAmI & "/Documents/workspace/" -- create workspace & config file in user's local home
	createConfig(myWorkspace)
	do shell script "open -a /.hiddenApps/Eclipse/Eclipse.app"
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
eclipse.p2.profile=epp.package.java", "osgi.framework=file\\:plugins/org.eclipse.osgi_3.10.2.v20150203-1939.jar
equinox.use.ds=true
eclipse.buildId=4.4.2.M20150204-1700
osgi.bundles=reference\\:file\\:org.eclipse.equinox.simpleconfigurator_1.1.0.v20131217-1203.jar@1\\:start
org.eclipse.equinox.simpleconfigurator.configUrl=file\\:org.eclipse.equinox.simpleconfigurator/bundles.info
eclipse.product=org.eclipse.platform.ide
osgi.splashPath=platform\\:/base/plugins/org.eclipse.platform
osgi.framework.extensions=reference\\:file\\:org.eclipse.osgi.compatibility.state_1.0.1.v20140709-1414.jar
eclipse.application=org.eclipse.ui.ide.workbench
eclipse.p2.data.area=@config.dir/../p2
osgi.bundles.defaultStartLevel=4
osgi.locking=none"}
	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
	--try
	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
	try
		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
	return
end createConfig

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