Benches macOS

9

applescript and do shell script

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

i have an applescript that issues a tar command.

the tar command I need is

gnutar czvf "/Volumes/Cold
Storage5/144000-144999/144073.tgz" "/Volumes/COLD STORAGE/144000-144999/144073" --exclude "/Volumes/COLD STORAGE/144000-144999/144073/print drive/*"

when i run that in the terminal .. it works perfect.

when I have applescript run it, it errors out.

I turned on logging and this is what applescript is trying to run.

gnutar czvf "/Volumes/Cold
Storage5/144000-144999/144073.tgz" "/Volumes/COLD STORAGE/144000-144999/144073" --exclude "/Volumes/COLD STORAGE/144000-144999/144073/print drive/*"

you can see that applescript escapes the quotes. if I copy and paste this into the terminal it errors out.

Why does the terminal direct version work, but not the version coming from my applescript? They are exactly the same, unless I am missing something.

Thanks in advance.

Anyone have any ideas?

Thanks,

-P

9 replies

Sign in or join Beachball Help to reply.

  1. 0

    Bang A. Lore

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    If you're not escaping the quotes in your code then it's not very likely to work the way you want it to.

  2. 0

    Pendal

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I'm feeding the command into the applescript via XML.

    when It comes it i looks like the first example.

    Correct me if i'm wrong, but isn't the applescript escaping the quotes itself?

    I'll give it another shot.

  3. 0

    Bang A. Lore

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Pendal wrote:

    I'm feeding the command into the applescript via XML.

    What does that mean?

  4. 0

    Camelot

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Knowing the error message you get would help explain the problem you're encountering.

    I'm guessing it's one of quoting (and the code not compiling), and that's likely because of quoted characters in your shell command.

    If you include quotes (and other certain symbols) in your shell command, then you need to escape those characters when you use that shell command within AppleScript. That's so that AppleScript knows to pass the quote onto the shell command, rather than interpret them itself.

    Consider a simpler example with the shell command:

    doit -param1 "/path/to/file"

    If you put that in AppleScript as-is, you'd see:

    do shell script "doit -param1 "/path/to/file""

    Now you should be able to see the problem - AppleScript is interpreting the first quote around the file path as the
    end of its own string:

    do shell script "doit -param1 "

    along with a trailing:

    /path/to/file""

    This clearly won't work. The quotes in the shell command need to be escaped so that AppleScript passes them to the shell:

    do shell script "doit -param1 "/path/to/file""

    Now the backslashes escape the quotes so they get passed to the shell correctly.

    It's also a good idea to include full paths of the command you want to run to avoid differences in shell environments (such as $PATH).

    In your case, you should end up with something like:

    do shell script "/usr/bin/gnutar czvf "/Volumes/Cold
    Storage5/144000-144999/144073.tgz" "/Volumes/COLD STORAGE/144000-144999/144073" --exclude "/Volumes/COLD STORAGE/144000-144999/144073/print drive/*""

    Now the quotes around the file paths get passed to the shell and should enable your command to work.

  5. 0

    Pendal

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    my applescript parses an xml file that has the shell command in it

    example

    <item><![CDATA[/usr/bin/gnutar cvf "/Volumes/Cold
    Storage5/144000-144999/144073.tar" "/Volumes/COLD STORAGE/144000-144999/144073" --exclude "/Volumes/COLD STORAGE/144000-144999/144073/print drive/*"]]></item>

    that part of the script works perfect, except for commands that use this above example.

    the applescript i have doing this assigns the the <item> to a variable called commandToProcess and then this function is called

    to procCommand(commandToProcess)

    try

    with timeout of 1800 seconds

    do shell script commandToProcess

    end timeout

    on error

    log "Could Not Process Command: " & commandToProcess

    end try

    end procCommand

    when I copy and paste the command to the terminal, exactly how it written above, it works.

    when I process the command through my applescript, it tar's the directory, but does not issues the --exclude parameter

    I understand the whole escaping thing " and so on.

    But this just isn't working.

    Any ideas?

    Message was edited by: Pendal

  6. 0

    Pendal

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Could it have something to do with the two dashes before the exclude command

    ?? --exclude?

    just wondering.

  7. 0

    Hiroto

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hello

    The exclude pattern of tar should be enclosed in quotes (apostrophes) so that the pattern is not expanded by shell before being passed to tar.

    I don't know why your current command does NOT fail in Terminal in the first place.

    cf.

    http://www.gnu.org/software/tar/manual/index.html

    E.g., see p.99 of -

    http://www.gnu.org/software/tar/manual/tar.pdf

    Choosing Files and Names for tar > Excluding Some Files > Problems with Using the exclude Options

    So try something like this:

    --SCRIPT

    set sh to "/usr/bin/gnutar -czvf "/Volumes/Cold
    Storage5/144000-144999/144073.tgz" "/Volumes/COLD STORAGE/144000-144999/144073" --exclude '/Volumes/COLD STORAGE/144000-144999/144073/print drive/*'"

    do shell script sh

    --END OF SCRIPT

    Good luck,

    H

    Message was edited by: Hiroto (fixed typo)

  8. 0

    Pendal

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    using ' ' around the the exclude command did not work at all. Even in the terminal.

    here is my input

    cd "/Volumes/COLD STORAGE/144000-144999/144073"; gnutar czvf "/Volumes/Cold
    Storage5/144000-144999/144073.tgz" * --exclude '"print drive"/*'

    if i remove the ' ' from around the --exclude pattern, it works just fine in the terminal.

    i even tried the following

    1. pass command string to applescript.

    2. write command string to a file

    3. run file with command string in it using do shell script

    the result is exactly the same. the command runs perfectly fine in the terminal, but will not run using applescript.

    this is getting annoying.

  9. 0

    Pendal

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    for anyone interested it was a shell problem.

    i was able to resolve it using this technote from apple

    http://developer.apple.com/technotes/tn2002/tn2065.html