Applescript - check if connected to a specific network, then run (else fail silently)

Discussion in 'macOS' started by mixvio, Sep 20, 2013.

  1. mixvio macrumors 6502

    Joined:
    Apr 12, 2009
    Location:
    Sydney, Australia
    #1
    First off apologies if this is in the wrong place, I wasn't sure if Applescript requests belong here or under the Programming forum.

    I have a Time Capsule which has an external USB drive connected to it and made available over the network. What I'd like to do is set up a script that automatically mounts that share whenever I boot or wake from sleep, or fail silently if the drive isn't available (IE, because I'm not at home or what).

    I've got everything working except for the "fail silently" part. The script runs appropriately at login and when waking from sleep, provided that I'm on my home network. When I'm not, I get an error and it all seems to go kinda crazy.

    The script in question is here:

    Code:
    delay 10
    
    property DRIVENAME : "afp://Time Capsule._afpovertcp._tcp.local/DRIVENAME/"
    mount volume ARCHON
    I was trying to figure out if there are conditionals, if/then etc, but I can't seem to work out how to get it to first check for the existence of the Time Capsule before trying to mount the share. My MacBook is connected over LAN at home, not wifi, so I don't know if searching for the network SSID will work as Airport is off when I'm at my desk.

    Alternatively if there's just a simple way to have it fail gracefully if the share isn't found that would be excellent too.

    Any suggestions?
     
  2. mixvio thread starter macrumors 6502

    Joined:
    Apr 12, 2009
    Location:
    Sydney, Australia
    #2
    Since this got no replies, in case anyone is searching for something similar I managed to get it working using a version of this script from zachmb:

    Code:
    delay 10
    
    -- Check IP address is reachable
    
    set IP_address to "192.168.0.xxx" (note, I have a non-standard IP set for my router which was how this worked)
    
    set IP_Valid to true
    
    try
    	do shell script ("ping -c 2 " & IP_address)
    on error
    	set IP_Valid to false
    end try
    
    -- PREP SSID DETECTION
    
    set SSID to do shell script "/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I | awk '/ SSID: / {print $2}'"
    
    
    
    -- IF NETWORK IS HOME
    
    if SSID is "AIRPORTTIMECAPSULE" or IP_Valid then
    	
    	-- MOUNT DRIVE
    	
    	mount volume "afp://AIRPORTTIMECAPSULE._afpovertcp._tcp.local/EXTERNALDRIVE/"
    	
    	-- SET RESULT VAR TO "WIN"
    	
    	set endresult to "win"
    	
    	-- ELSE
    	
    else
    	
    	-- DO NOTHING
    	
    	-- SET RESULT VAR TO "FAIL"
    	
    	set endresult to "fail"
    	
    	-- END
    	
    end if
    I cut out the Growl stuff since I don't need it sending me notifications. Change the drive and Airport name accordingly for your own needs. This works for me and properly mounts the drive if I'm on home wifi or ethernet, and silently fails if it can't be found.
     
  3. Weaselboy Moderator

    Weaselboy

    Staff Member

    Joined:
    Jan 23, 2005
    Location:
    California
    #3
    You might have better results with the free app Control Plane. You can have it mount/unmount based on what network you are on.
     
  4. xjames macrumors member

    Joined:
    Feb 12, 2008
    Location:
    Brazil
    #4
    Wow. this app was perfect for this. I had the same need, to mount a NAS but only when at home.
     
  5. steve23094 macrumors 68000

    steve23094

    Joined:
    Apr 23, 2013
    #5
    Just stumbled across this thread trying to do something similar. ControlPlane is very useful.
     

Share This Page