johniboy1990

Q: rsync apple script syntax error - help please :]

Hi all

 

Im having difficulty with my rsync syntax.

I want to write a script that backs up folders from my 'home' folder.

 

The rsync works perfectly in terminal.

I just cant get it to work in applescript.

 

I get error -  Expected “"” but found unknown token.

Id really really appreciate it if someone could help me out here and show me whats wrong with the syntax.

 

tell application "Finder"

  activate

  do shell script "rsync -ruv --exclude-from=/Volumes/John\'s\ Stuff/john/Rsync/Exclude\ from\ Media\ Storage.txt /Volumes/John\'s\ Stuff/john /Volumes/Media\ Storage\ 1 "

end tell

 

display dialog "Backup complete, Do you want to eject the disk?"

 

if result = {button returned:"Ok"} then

  try

  do shell script "diskutil eject /Volumes/Media*Storage*1"

  display dialog "Successfully ejected the disk." buttons {"Close"} default button "Close"

 

  on error

  display dialog "Unable to eject the disk." buttons {"Close"} default button "Close"

  end try

 

else if result = {button returned:"Cancel"} then

end if

end

OS X Yosemite (10.10.3), null

Posted on Jun 14, 2015 10:23 AM

Close

Q: rsync apple script syntax error - help please :]

  • All replies
  • Helpful answers

  • by Niel,Solvedanswer

    Niel Niel Jun 14, 2015 10:23 AM in response to johniboy1990
    Level 10 (311,923 points)
    Jun 14, 2015 10:23 AM in response to johniboy1990

    You need to put a second backslash next to each existing one to escape them:

     

    do shell script "rsync -ruv --exclude-from=/Volumes/John\\'s\\ Stuff/john/Rsync/Exclude\\ from\\ Media\\ Storage.txt /Volumes/John\\'s\\ Stuff/john /Volumes/Media\\ Storage\\ 1 "

     

    (128759)

  • by johniboy1990,

    johniboy1990 johniboy1990 Jun 14, 2015 10:32 AM in response to Niel
    Level 1 (4 points)
    Mac OS X
    Jun 14, 2015 10:32 AM in response to Niel

    Thanks Niel!!!!

    top guy.

     

    Best regards

    John

  • by Drew Reece,

    Drew Reece Drew Reece Jun 14, 2015 10:41 AM in response to johniboy1990
    Level 5 (7,485 points)
    Notebooks
    Jun 14, 2015 10:41 AM in response to johniboy1990

    I had trouble compiling what you pasted, there appears to be tabs or some other invisible character before the indented lines. I cleared them out & then double escaped the command

    I believe the 'do shell script' command requires you to double escape (the shell expands one backslash, the do shell script expands the other).

     

     

    tell application "Finder"

      activate

      do shell script "rsync -ruv --exclude-from=/Volumes/John\\'s\\ Stuff/john/Rsync/Exclude\\from\\Media\\ Storage.txt /Volumes/John\\'s\\ Stuff/john /Volumes/Media\\ Storage\\ 1"

    end tell

     

    display dialog "Backup complete, Do you want to eject the disk?"

    if result = {button returned:"Ok"} then

      try

      do shell script "diskutil eject /Volumes/Media\\ Storage\\ 1"

      display dialog "Successfully ejected the disk." buttons {"Close"} default button "Close"

      on error

      display dialog "Unable to eject the disk." buttons {"Close"} default button "Close"

      end try

    else if result = {button returned:"Cancel"} then

    end if

     

    I haven't tested it with rsync, so see if it works for you.

    I also made diskutil eject the disk name without the wildcards - you need to double escape that too.

     

     

    Personally I find it easier to convert to quoted variables and then smash them into a string via '&'…

     

    set source to quoted form of POSIX path of "/Volumes/John's Stuff/john"

    set dest to quoted form of POSIX path of "/Volumes/Media Storage 1"

    set exclude to quoted form of POSIX path of "/Volumes/John's Stuff/john/Rsync/Exclude from Media Storage.txt"

     

    tell application "Finder"

      activate

      do shell script "rsync -ruv --exclude-from=" & exclude & " " & source & " " & dest

    end tell

     

    display dialog "Backup complete, Do you want to eject the disk?"

     

    if result = {button returned:"Ok"} then

      try

      do shell script "diskutil eject " & dest

      display dialog "Successfully ejected the disk." buttons {"Close"} default button "Close"

      on error

      display dialog "Unable to eject the disk." buttons {"Close"} default button "Close"

      end try

    else if result = {button returned:"Cancel"} then

    end if


     

    P.S.

    See this if you want to automate fixing the broken indenting that this forum does to Applescripts.

    Bookmarklet to fix AppleScript pasting on discussions.apple.com