Hi. I have an rsync shell command that works perfectly, and which I want to include in an AppleScript. The command includes this to exclude a particular directory
which AS doesn’t seem to like: I get the message
Assuming that the problem was with escaping the backslash, I did this
No syntax error, but the script fails: according to the log, the command is being passed to the shell as
which of course the shell doesn’t like. I’m getting a bit lost now: could anyone suggest an AS sequence which would allow
thanks for the suggestion. unfortunately that’s giving me “Expected end of line but found unknown token.”, highlighting the first quote mark in your suggestion.
Maybe you should start out with the whole rsync commandline within single ticks, and only “getting out” of the single ticks the places you want to have a variable. That should be a far eaiser approach, when it comes to get it right. You must still escape, but at least the commandline interpret won’t try to invoke a subshell.
Edit
I really meant "put the whole argument list for rsync within single ticks, and only open it up if you have to pass in a variable, like a path. (But see if you can get it right with a hardcoded path first). This will simplify the escaping a wee bit, as you then only have to deal with Applescript’s do shell script command and rsync, and not the shell on top of it.
Hi. Thanks for the tip. Although my rsync command works fine in the terminal you’re right that it is going to need more serious modification to use in an AS. I’ll bear in mind your advice as I try to do that.
Here is a little trick I believe kel reminded me about in a post.
You copy your commandline from the Terminal window into an empty TextEdit document, then you run the script below, so you can see how Applescript would have needed it as a command string for the do shell script.
Example commandline:find . -name \*.c -exec grep -l more {} \;
The applescript to parse it with when it is in the TextEdit document.
tell application "TextEdit"
set a to first paragraph of text of its front document
end tell
A lazier version of the script, you just copy the commandline from the Terminal window to the clipboard, before you run it. (you have that commandline in the Terminal history anyway.)
tell application "TextEdit"
make new document at front
set text of front document to the clipboard
set a to first paragraph of text of front document
log a
close front document saving no
end tell
a
thanks for that. doing that suggested the right way to escape the sequence, but also that there were issues elsewhere in the command. but then it occurred to me that I could just call the (working) shellscript from inside the AppleScript. Presumably not the best way to do it, but it works for my purposes.
i do that all the time for a new of reasons, as long as I am not to share it, most of them really being practical sense, and good software-practice. Then you maintain the command in just one place, and you can use it from the Terminal window, a script, an Automator action, that can get a short cut, and if you save the script as an applet, then you can get at it from the Spotlight!
Anyways, I made a script for showing an escaped commandline and to store an escaped commandline onto the clipboard, ready to be pasted right into a script, as the command-argument of a do shell script:
Edit
Upgraded 2013.10.29 to work regardless of locale. Thanks to Yvan Koenig.
Upgraded 2013.11.15 to implement a choice for double escaping for an osascript inside a do shell script.
as far as I can tell, yes. Actually, I got things are bit muddled in the first message: it’s an rsync command, but it locates files using find. To exclude certain directories I was using the bit in parentheses, as per here:
Just for the record; you’ll have to change the text item delimiter to your country’s counterpart, if you are on a machine that doesn’t have English as default locale.