Simple question about applescripting a terminal command

Discussion in 'Mac Programming' started by sitryd, Sep 19, 2007.

  1. sitryd macrumors member

    Joined:
    Sep 19, 2006
    #1
    Hey all,

    I'm assuming this is a simple question, but I can't find any good documentation online on it.... I'm trying to write an applescript to automatically back up a copy of my notes for classes to a flash drive whenever its plugged in. The problem is that the directory path for the source file has a space in it, and Applescript is bouncing back an error "Expected “"” but found unknown token."

    Here's the relevant portion of the script:

    ignoring application responses
    tell application "Terminal"
    activate
    do shell script "rsync -a /Users/Sitryd/Documents/07-08 2L/\ Constitutional\ Law/Constitutional\ Law\ Notes.doc /Volumes/Backups/"
    end tell
    end ignoring

    It's hiccuping right after the \ in the "07-08 2L/\" sequence (i.e. at the first space), but I can't figure out how to have applescript just pass that sequence normally to terminal.

    Thanks!
     
  2. MongoTheGeek macrumors 68040

    MongoTheGeek

    Joined:
    Sep 13, 2003
    Location:
    Its not so much where you are as when you are.
    #2
    anytime you want to put quotes inside an applescript string you have to put a backslash (\") first. If you want to put an actual backslash in the string you have to put a backslash in front of it. (\\)

    other useful ones

    \r --> return
    \t --> tab
    \n --> newline
     
  3. sitryd thread starter macrumors member

    Joined:
    Sep 19, 2006
    #3
    Hmm think i wasnt very clear with my question. I dont actually want to have the \ or a quote in the script. From what I've read the " marks are necessary for the rsync command (i.e. send "rsyc -a /Source /Destination" to the terminal).

    The actual source is /Users/Sitryd/Documents/07-08 2L/Constitutional Law/Constitutional Law Notes.doc . To tell the terminal to preserve the spaces in the sourcepath, I had to put the "\"s in, leaving /Users/Sitryd/Documents/07-08 2L/\ Constitutional\ Law/Constitutional\ Law\ Notes.doc /Volumes/Backups/.

    This leaves the script as:

    ignoring application responses
    tell application "Terminal"
    activate
    do script "rsync -a /Users/Sitryd/Documents/07-08\ 2L/\ Constitutional\ Law/Constitutional\ Law\ Notes.doc /Volumes/Backups/"
    end tell
    end ignoring


    Yet rather than treating the portion within the quotation marks as a single command to send to the terminal, applescript is hitting the first space and giving an error. I seem to need the quotes, since without them it comes back with an "expected end of line" error.

    Thanks in advance!
     
  4. MongoTheGeek macrumors 68040

    MongoTheGeek

    Joined:
    Sep 13, 2003
    Location:
    Its not so much where you are as when you are.
    #4
    Like having to put the \ in front of the space to get terminal to preserve the space in the file name you have to put a \ in front of the \ to make Applescript realize that you want to have a \ in the string that the shell needs to see the file.

    what you need is
    Code:
    ignoring application responses
        tell application "Terminal"
            activate
            do script "rsync -a /Users/Sitryd/Documents/07-08\\ 2L/\\ Constitutional\\ Law/Constitutional\\ Law\\ Notes.doc /Volumes/Backups/"
        end tell
    end ignoring
    
    or

    Code:
    ignoring application responses
        tell application "Terminal"
            activate
            do script "rsync -a \"/Users/Sitryd/Documents/07-08 2L/ Constitutional Law/Constitutional Law Notes.doc\" /Volumes/Backups/"
        end tell
    end ignoring
    
    or perhaps most simply

    Code:
    ignoring application responses
        tell application "Terminal"
            activate
            do script "rsync -a '/Users/Sitryd/Documents/07-08 2L/ Constitutional Law/Constitutional Law Notes.doc' /Volumes/Backups/"
        end tell
    end ignoring
    
    You should also look into do shell script in the standard additions.
     
  5. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #5
    Shouldn't that be "do shell script ...". The rest looks fine. Your original post looked like it didn't have the "\ " between 08 and 2L, which would have been giving you issues. I'd try this out, but I don't have those specific folders of course.
     
  6. pepeleuepe macrumors 6502

    Joined:
    Oct 27, 2002
    Location:
    Los Angeles, California
    #6
    I just setup rsync last night and had a similar problem. I ended up using the "quoted form of" command in applescript to solve my specific problem. I think it might work for you as well.

    Check out this little tutorial: http://www.oreillynet.com/pub/a/mac/2005/07/22/backup.html?page=2

    or give this a shot:

    Code:
    do shell script "rsync -a " & quoted form of "/Users/Sitryd/Documents/07-08 2L/ Constitutional Law/Constitutional Law Notes.doc" & " /Volumes/Backups/"
    
     

Share This Page