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. 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
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
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
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
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