Mount a Network Drive via AppleScript

I’ve spoken quite a bit on Mac Power Users about how I use an AppleScript (miserably created by me, thankfully re-created by Dorothy Rendon) to mount my Drobo 5N but the same idea should work with any network drive. First, let me explain why I do this. I want to make sure my network volume is available and accessible anytime.

There are a couple ways you can mount a Drobo or any network attached storage device. Perhaps easiest is to simply use the Finder sidebar. You can also add the network volume to your login items. Sometimes, especially when using a laptop, network drives can become disconnected if your machine goes to sleep or your otherwise drop your network connection. So it maybe necessary to re-mount the network drive on a regular basis. That’s where this script comes in.

I use a couple methods to trigger the script to keep my network drive mounted. One method, is I use Keyboard Maestro to fire off the script anytime I connect to my Wi-Fi network. Another method is to use an App like Lingon 3 to run the AppleScript on a regular basis (maybe every hour or so). Finally, you can use a launcher utility like LaunchBar or Alfred to launch the script on demand.

In order to work, the script needs to know the IP address of your NAS on your internal network. Therefore it’s a good idea to create a DHCP reservation on your network so your NAS will always be assigned the same address. You can do this with your router and will need to know the MAC address of your NAS. This is the IP address that you will plug into the script.

So without further delay, here’s the script: Note that you'll need to supply the script with your NAS IP address as well as the name(s) of your volumes.

-- *********************************************************************
-- AppleScript to detect & mount Drobo network drives
--
-- Created by Katie Floyd, April 2011
--
-- Revision History
--  Oct. 8, 2011: add network detection & error recovery, D. Rendon
--
-- Does not handle username/password to Drobo. Assume finder will ask & user can 
-- do his own entry.
-- *********************************************************************
-- If you find the talking too annoying, just comment out all the "say" statements
-- by placing two dashes at the start of the line.

-- Set variables
--
-- Substitute your Drobo IP address here
set DroboIP to "XX.X.X.X"

-- Assume failure
set isDroboReachable to "no"

-- Check if Drobo drives are present on LAN
try
    -- test for Drobo pings
    set pingString to "ping -t 5 -c 1 " & DroboIP
    -- log "Executing string: " & pingString
    set ping_result to (do shell script pingString)
    
    -- log "Ping result: " & ping_result
    if "64 bytes from" is in ping_result then
        set isDroboReachable to "yes"
    end if
    
    -- if ping errors, return no and essentially exit script
on error
    set isDroboReachable to "no"
end try


-- if Drobo responded to ping, go ahead & mount all drives
-- Do a catch on each. If any fail, script will not attempt to mount the rest.
if isDroboReachable is "yes" then
    set mountString to "afp://" & DroboIP
    
    -- Substitute your volume names here, replacing "Data", "DroboTM", & add other volume names as needed
    set mountDisk1 to mountString & "/Data"
    set mountDisk2 to mountString & "/DroboTM"
    
    try
        log "Mount String: " & mountDisk1
        tell application "Finder" to mount volume mountDisk1
        
    end try
    
    try
        log "Mount String: " & mountDisk2
        tell application "Finder" to mount volume mountDisk1
        
    end try
    
    -- Cannot find Drobo on current LAN. So say error and leave
end if