0

I'm currently trying to have drives be automatically mounted when I'm connected to my network. Only problem is that when I'm not connected to my network an error shows, while connected to the network no errors show. How would I disable this?

Here's my current script:

tell application "Finder"
    mount volume "smb://(IP)/(Drive)" as user name "(user)"
end tell
Erich
  • 3
  • 4
  • check if the network's connected before trying to mount? probably should check that the CORRECT network is connected as well. – Marc B Dec 21 '15 at 16:26
  • `mount volume` is a part of `Standard Additions`. The Finder is not needed. – vadian Dec 21 '15 at 16:28
  • How would I execute it so it checks if it's connected to the right network before it executes. – Erich Dec 21 '15 at 16:38

1 Answers1

0

This is a simple handler to check the network connection. It works with both Ethernet and WiFi connections.

Adjust the values in the first two lines if necessary.

property primaryEthernetDevice : "en0"
property primaryWiFiDevice : "en2"

property myIPAddress : "192.168.1.10"
property mySSID : "mySSID"

set {isConnected, IPAddress, ssid} to checkConnection()
if isConnected and ssid is mySSID then
    mount volume "smb://(IP)/(Drive)" as user name "(user)"
end if

on checkConnection()
    set wiredIP to do shell script "/sbin/ifconfig " & primaryEthernetDevice & " | /usr/bin/awk '/inet / {print $2}'"
    if wiredIP is not "" then return {true, wiredIP, ""}
    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, wirelessIP, ssid}
    else
        return {false, "", ""}
    end if
end checkConnection
vadian
  • 274,689
  • 30
  • 353
  • 361
  • This works good for when I'm not connected. but when I'm connected to a different network it returns an error code -5014 and tells me the drive can't be mounted. – Erich Dec 21 '15 at 17:08
  • You would have to find a way to distinguish the networks. If it's wireless you could identify it by the SSID. Or if the IP addresses are different, use that. A third solution is to wrap the `mount volume` line in a `try - end try` block, that suppresses the errors. – vadian Dec 21 '15 at 17:17
  • I'm not very good with AppleScript, as this is my first time actually using it. On top of that it's not my Mac so I'm trying to get this setup so volumes mount only while connected to a certain network (Which I am currently on). Using what you described how would I implement that into the script you gave me? – Erich Dec 21 '15 at 17:20
  • I updated the answer to check also the IP(v4) address. – vadian Dec 21 '15 at 17:27
  • Thank you, at this point I don't know if what I'm trying to do is possible. Simply because the IPv4 would change because it's a laptop being brought from location to location. Is there a way to check the MAC address of the router? – Erich Dec 21 '15 at 17:31
  • Not easily with vanilla AppleScript. It's possible with help of AppleScriptObjC. But if you're using only WIFI you could check the SSID. – vadian Dec 21 '15 at 17:35
  • Sorry to comment so many times but I wasn't able to get the script working that I tried to implement the SSID detection in. Think you could provide an example of how I would use that, thank you. – Erich Dec 21 '15 at 19:15