Applescript throws an error when trying to mount a network drive

I have created an applescript that runs at logon, to automatically mount multiple network shares running on a server at my home. Everything works perfectly, except for when I am not connected to my home network. An alert from what seems to be the NetAuth agent comes up, telling me that “the server is unavailable” or similar message, and I have to press OK for the dialog to go away. The script also hangs at this point, (before OK is pressed) and causes it’s resulting application (I made the script an application file) to be come busy and unresponsive until OK is pressed. Once that dialog is dismissed, the script quits without a problem. My goal is to have nothing come up when the server is not available. I have tried some error handling functions within the script, but since there are no errors with the actual script, it doesn’t fix the issue. A copy of the code is below. :slight_smile: Any help would be greatly appreciated. Thanks so much!


tell application "Finder"
	try
		mount volume "smb://mediaserver/automatically add to iTunes/" as user name "Rocco Fiorentino" with password "rocco1020"
		mount volume "smb://mediaserver/shared media" as user name "Rocco Fiorentino" with password "rocco1020"
	end try
	close windows
end tell


Hi,

this is a versatile handler to check the reachability and location
In the first two property lines you have to specify the BSD name of the Ethernet and Wi-Fi interface (e.g. en0)

getLocation returns a list of 4 values:

connected (boolean) - connected to Internet
wired (boolean) - true if connected via Ethernet, false via Wi-Fi
IPAddress (string) - current internal IPv4 address
ssid (string) - name of Wi-Fi network


property primaryEthernetDevice : "en0"
property primaryWiFiDevice : "en1"

set {connected, wired, IPAddress, ssid} to getLocation()

on getLocation()
	set wiredIP to do shell script "/sbin/ifconfig " & primaryEthernetDevice & " | /usr/bin/awk '/inet / {print $2}'"
	if wiredIP is not "" then
		return {true, true, wiredIP, ""}
	else
		set wirelessIP to do shell script "/sbin/ifconfig " & primaryWiFiDevice & " | /usr/bin/awk '/inet / {print $2}'"
		if wirelessIP is not "" then
			set ssid to do shell script "/usr/sbin/networksetup -getairportnetwork " & primaryWiFiDevice & " | sed 's/Current Wi-Fi Network: //g'"
			return {true, false, wirelessIP, ssid}
		else
			return {false, false, "", ""}
		end if
	end if
end getLocation


Hello,

Thank you so much for all of this help, and your quick reply! I think I might be doing something wrong, because when I add this script to the top of my existing script, and run the whole thing, the error dialog still comes up. Is there information or values in the script you gave me that I need to fill in (the network name, etc.)?
Thanks again,
Rocco

Depending on your network environment you have to check the parameters

For example if the computer is required to be connected via Ethernet


set {connected, wired, IPAddress, ssid} to getLocation()
if (connected and wired) then
	-- mount server
end if

or of the connection is restricted to a specific Airport base station


set {connected, wired, IPAddress, ssid} to getLocation()
if (connected and (ssid = "my base station")) then
	-- mount server
end if

Oh! Ok, awesome, thanks so much!! :smiley: Have a great day!

Alternatively you could create the mount point “manually” with mkdir and mount the shared volume with mount_smbfs.
An additional benefit of this method is to mount the volume silently without opening a Finder window

ok, on second thought, when I looked at this script again, I’m still slightly confused, sorry!!

The network “server” is a windows machine, wired to our network. The mac usually connects via wifi. In this script below, what do I need to change/replace so that if the wireless said of “my-said” does not exist, the script will not try to mount the servers?

Thanks
Rocco



property primaryEthernetDevice : "en0"
property primaryWiFiDevice : "en1"

set {connected, wired, IPAddress, ssid} to getLocation()

on getLocation()
	set wiredIP to do shell script "/sbin/ifconfig " & primaryEthernetDevice & " | /usr/bin/awk '/inet / {print $2}'"
	if wiredIP is not "" then
		return {true, true, wiredIP, ""}
	else
		set wirelessIP to do shell script "/sbin/ifconfig " & primaryWiFiDevice & " | /usr/bin/awk '/inet / {print $2}'"
		if wirelessIP is not "" then
			set ssid to do shell script "/usr/sbin/networksetup -getairportnetwork " & primaryWiFiDevice & " | sed 's/Current Wi-Fi Network: //g'"
			return {true, false, wirelessIP, ssid}
		else
			return {false, false, "", ""}
		end if
	end if
end getLocation

tell application "Finder"
	try
		mount volume "smb://mediaserver/automatically add to iTunes/" as user name "Rocco Fiorentino" with password "rocco1020"
		mount volume "smb://mediaserver/shared media" as user name "Rocco Fiorentino" with password "rocco1020"
	end try
	close windows
end tell


and how would I mount the volumes using the mount_smbfs protocol? Would that ileviate the errors when the server doesn’t exist?

the shared volumes will be mounted, if the computer is connected to the Wi-Fi base station “my-said”


property primaryEthernetDevice : "en0"
property primaryWiFiDevice : "en1"

set {connected, wired, IPAddress, ssid} to getLocation()

if (connected and (ssid = "my-said")) then
	tell application "Finder"
		try
			mount volume "smb://mediaserver/automatically add to iTunes/" as user name "Rocco Fiorentino" with password "rocco1020"
			mount volume "smb://mediaserver/shared media" as user name "Rocco Fiorentino" with password "rocco1020"
		end try
		close windows
	end tell
end if

on getLocation()
	set wiredIP to do shell script "/sbin/ifconfig " & primaryEthernetDevice & " | /usr/bin/awk '/inet / {print $2}'"
	if wiredIP is not "" then
		return {true, true, wiredIP, ""}
	else
		set wirelessIP to do shell script "/sbin/ifconfig " & primaryWiFiDevice & " | /usr/bin/awk '/inet / {print $2}'"
		if wirelessIP is not "" then
			set ssid to do shell script "/usr/sbin/networksetup -getairportnetwork " & primaryWiFiDevice & " | sed 's/Current Wi-Fi Network: //g'"
			return {true, false, wirelessIP, ssid}
		else
			return {false, false, "", ""}
		end if
	end if
end getLocation

This is a variation from a “mountAFP” script but I guess it works with SMB as well.
The script checks also whether the volumes is already mounted.
I copied the credentials from your other script


property server : "mediaserver"
property serverVolume : "shared media"
property user : "Rocco Fiorentino"
property pass : "rocco1020"

set isMounted to serverVolume is in (do shell script "/bin/ls /Volumes") or mountSMB(user, pass, server, serverVolume)

if isMounted then
	-- do something
end if

on mountSMB(user_name, pass_word, thehost, theVolume)
	set theAddress to quoted form of ("smb://" & user_name & ":" & pass_word & "@" & thehost & "/" & theVolume)
	set mountpoint to quoted form of ("/Volumes/" & theVolume)
	try
		do shell script "/bin/mkdir " & mountpoint & "; /sbin/mount_smbfs " & theAddress & space & mountpoint
		return true
	on error e
		log e
		do shell script "/bin/rm -r " & mountpoint
		return false
	end try
end mountSMB

I tried your script and it worked great.
What if I wanted to use CIFS?

Model: 2009 27" iMac, 16 GB, Mavericks
AppleScript: 2.3.1
Browser: Safari 537.75.14
Operating System: Mac OS X (10.8)

there is no CIFS equivalent of mount_afp or mount_smbfs, but it seems to be possible with the mount command and the -t switch

That’s awesome!! Thank you so much, I’ve been trying to get rid of that error for about 6 months now, and you did it! :smiley: Thanks again.

you’re welcome