Search code examples
bashmacosapplescriptautomator

Automator wifi specific backup


I am wanting to automate the running of a weekly backup of certain folders on my Mac to the cloud when I am connected to my home network.

Scenario:

Check wifi network name

If 'home' then perform backup

else exit

I have created a calendar event to trigger an automator script.

I currently have two bash scripts

  1. to determine the wifi network that i am connected to
  2. to perform the backup (using rclone)

The part i am struggling with is the If, Then, else part.

Bash script 1:

/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I | awk -F: '/ SSID/{print $2}'

Attempt at applescript:

on run {input, parameters}
    
    if input is not ("home") then error number -128
    
    return input
    
end run

Bash Script that should run on detection of home network:

/usr/local/Cellar/rclone/1.52.1/bin/rclone copy "Source" Remote:folder

Both bash scripts work but i am stuck on the conditional part.... any help gratefully received.


Solution

  • Running your command, I get the following

    ❯ /System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I | awk -F: '/ SSID/{print $2}'
     Home
    

    You will notice there is a tiiiiiiiiny space prior to Home. The text is actually <space>Home.

    Your issue is in the awk command.

    When you split the text SSID: Home by the :, $1 becomes SSID and $2 becomes Home. (again, note the spaces).

    Your input to the applescript is looking for Home. Home does not match Home.

    Be mindful of both Spacing AND Capitalization.


    Secondly, input is a list, not a string. So input is not ("home") will always run because you are comparing it to a string.

    You must typecast input as a string if you wish to compare it to a string such as home.

    on run {input, parameters}
        if (input as string) is " home" then
            tell application "System Events" to display dialog "You are home."
        end if
        return input
    end run
    

    enter image description here