escaping backslash?

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

to be passed to the shell? Many thanks.

Does this helps?

quoted form of "\\( -path ~Work/Data \\)"

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.

Hello.

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.

Hello.

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

The result from the log window:

"find . -name \\*.c -exec grep -l more {} \\; "
Edit

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! :slight_smile:

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: :slight_smile:

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.

-- http://macscripter.net/viewtopic.php?pid=163300#p163300
-- This script shows the escaped commandline after you have copied a 
-- commandline from a terminal window to the clipboard. Or just shows you what something will look
-- like when it is escaped like in a do shell script.
-- © 2013 McUsr Icon © Apple Inc. Upgraded 2013.10.29 to work under all locales
-- With thanks to Yvan Koenig.
script EscapeCommandLine
	property ScriptTitle : "Escaped Terminal Commandline"
	property osascriptButton : "Osa script inside a do shell script"
	property doshellscriptButton : "Do shell script"
	local clipIcon, clipCnts, errStr, btn
	set clipIcon to a reference to file ¬
		((path to library folder from system domain as text) & "CoreServices:CoreTypes.bundle:Contents:Resources:ClippingText.icns")
	set clipCnts to the clipboard
	if clipCnts is "" then
		tell application (path to frontmost application as text)
			display alert "Nothing on Clipboard"
			error number -128
		end tell
	else
		tell application (path to frontmost application as text)
			set btn to button returned of (display dialog "Choose if you need to escape the command-line once or twice!" with title ¬
				ScriptTitle buttons {"Cancel", osascriptButton, doshellscriptButton} ¬
				cancel button 1 default button 3 with icon clipIcon)
		end tell
		
	end if
	try
		text 0 of clipCnts
		-- forces an error
	on error errStr
		set startIx to offset of "\"" in errStr
		set meatOfErrStr to text startIx thru -2 of errStr
	end try
	if btn = osascriptButton then
		try
			text 0 of meatOfErrStr
			-- forces an error
		on error errStr
			set startIx to offset of "\"" in errStr
			set meatOfErrStr to text startIx thru -2 of errStr
		end try
	end if
	local btn, theText
	tell application (path to frontmost application as text)
		set {btn, theText} to {button returned, text returned} of (display dialog "The escaped commandline, ready to be  be pasted in...
" default answer meatOfErrStr with title ¬
			ScriptTitle buttons {"__________Cancel__________", "________Clipboard________", "____________Ok____________"} ¬
			cancel button 1 default button 3 with icon clipIcon)
		if btn is "________Clipboard________" then
			set the clipboard to theText
		end if
	end tell
end script
tell EscapeCommandLine to run

The buttons looks a bit ugly, but I had to take liberties to “stretch” out the input box, and those hard spaces, can be pesky…

Are the parentheses really needed?

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:

http://www.cyberciti.biz/faq/linux-unix-osx-bsd-find-command-exclude-directories/

of course, there may be other ways which don’t involve parentheses (or even which don’t involve find at all). But it works for my purposes.

this syntax for find in AppleScript usually works


do shell script "/usr/bin/find /path/to/folder -type d \\( -path '*.app*' \\)"

Hello.

I fixed the script in post #8, so it tests for the right button name, when pasting the “parsed” result back to the clipboard.

Hello.

I removed everything pertaining to TextEdit, in post #8 as it wasn’t necessary anymore.

Hello.

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.

Hello.

I have updated the script in post #8 for escaping an osascript inside a do shell script.