macosapplescriptdrive-mapping

Apple Script Else/If


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

Solution

  • 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