2

I have a Folder Action on a /Volumes folder. I'm checking when a specific disk is mounted (folder in /Volumes created), checking ssh to remote computer. If all present I run rsync there.

It was OK when number of files was low. Now only rsync's "building file list" takes 40 minutes. I want this action to ask before rsync for my decision. Better with osascript.

source="/Volumes/MyDisk/FolderToSync"

user="username"
host="myhost"
port="22"
path="/RemoteFolder/"

log="$HOME/RSync_$(date +%Y.%m.%d_%H-%M-%S).log"
rm $HOME/RSync_$(date -v-1m +%Y.%m.)*.log

if [ -d "$source" ]; then

    nc -z $host $port > /dev/null   

    if [ $? -eq 0 ]; then
        touch $log;

        osascript -e 'display notification "Do you want to RSync now?" with title "ssh"'

        rsync -czvaEP --delete --log-file=$log "$source" -e "ssh -p$port" $user@$host:$path;

    fi
fi
0

Using display notification will be of no use since its result is none, use a display dialog instead.

The follow is an example of how I'd do it:

result="$(osascript -e '(display dialog "Do you want to Rsync now?" buttons {"No", "Yes"} default button 2 with title "ssh")')"
if [ "$result" == "button returned:Yes" ]; then
    rsync -czvaEP --delete --log-file=$log "$source" -e "ssh -p$port" $user@$host:$path;
fi

Shown within the relevant code posted in the OP:

source="/Volumes/MyDisk/FolderToSync"

user="username"
host="myhost"
port="22"
path="/RemoteFolder/"

log="$HOME/RSync_$(date +%Y.%m.%d_%H-%M-%S).log"
rm $HOME/RSync_$(date -v-1m +%Y.%m.)*.log

if [ -d "$source" ]; then

    nc -z $host $port > /dev/null   

    if [ $? -eq 0 ]; then
        touch $log;
        result="$(osascript -e '(display dialog "Do you want to Rsync now?" buttons {"No", "Yes"} default button 2 with title "ssh")')"
        if [ "$result" == "button returned:Yes" ]; then
            rsync -czvaEP --delete --log-file=$log "$source" -e "ssh -p$port" $user@$host:$path;
        fi
    fi
fi
  • Tried as you described. Three comments 1) notifications do have buttons, such as "Update to High Sierra" has. 2) Moved touch $log inside the if clause, so blank files won't get created each time we can sync, but choose not to 3) button returned:Yes do not work in this syntax - never start sync no matter the reply:( – Pavel Jan 15 '18 at 18:32
  • @Pavel, Looking at the Commands Reference in the AppleScript Language Guide for, display notification, it has no way of defining buttons. For Result it shows "None.". It also states under Discussion, "Exactly how the notification is presented is controlled by the “Notifications” preferences in System Preferences. Users may opt to display a reduced form of notification, turn off the sound, or even not display them at all." – user3439894 Jan 16 '18 at 3:44
  • @Pavel, It has been my experience that even when set to Alert style, which shows buttons, in Notifications preferences in System Preferences, they are not user definable nor are they trappable in standard AppleScript. Also, individual applications created in Automator do not show up in that preference pane, so there's no way to change it to Alert style. Which is moot, if it's not trappable to begin with. Sans your rsync command, my example code does in fact work, otherwise I wouldn't have posted it! I tested with my own rsync command because obviously I couldn't test yours but mine worked! – user3439894 Jan 16 '18 at 3:45
  • @Pavel, So you need to figure out where your code is not working and fix it as I can't fix what I can't test. Beside that, the only purpose of my answer was to show you a way to trap a button from a display dialog using osascript in bash and the code I wrote for that particular event does in fact work! – user3439894 Jan 16 '18 at 3:45
  • The problem was in my "edit" - result = "$(osascript -e '(display instead of result="$(osascript -e '(display... Wanted to make it more readable. Can you spot the difference? After an hour in forums, found out that space matters. Need some goof book on bash shell to learn systax. Sorry! – Pavel Jan 16 '18 at 11:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .