Weird single quote escaping by applescript

Hello,

Given :

set a to " \" "
display dialog a
display dialog quoted form of a

set a to " ' "
display dialog a
display dialog quoted form of a

The first set behaves as expected. You get :

tell application "AppleScript Editor"
	display dialog " \" "
		--> {button returned:"OK"}
	display dialog "' \" '"
		--> {button returned:"OK"}
end tell

Although the second set behaves strangely. You get :

tell application 'AppleScript Editor"
	display dialog " ' "
		--> {button returned:"OK"}
	display dialog "' '\\'' '" --> What happened here ?
		--> {button returned:"OK"}
end tell

It seems to be adding \" after the single quote for some reason.

Thoughts?

Using 10.7.2.

I don’t know what you see in Lion but in Snow Leopard it seems like expected: ’ ‘'’ ’

The initial string is a single quote => ’

I was expecting to see for the quoted form => ’ ' ’
A single escaped quote with a single quote on each side.

Not => ’ ’ ' ’ ’
A single escaped quote with two single quotes on each side.

Spaces added for clarity.

Well I don’t get it, sorry. A single quote is in AS not a special character unlike the backslash and double quote “ they have special meanings in AppleScript which means they need to be escaped if they need to behave like a normal character. Escaped characters are:

\ =
" = "
\n = newline
\r = cariage return
\t = tab

In the AppleScript environment special characters need to be escaped. When you write data to a file or show a string on the screen (outside the AppleScript environment) the escaped characters will be replaced with it’s value.

So your string “’ ‘\’’ '” is actualy “’ ‘'’ '”. You have escaped the backslash and not the single quote behind it. Like I said a single quote doesn’t have a special meaning like in PHP, Javascript or shell scripts. It’s more like a C string

ok, I understand all of that.

quoted form of aString

adds a single quote at the beginning and end of aString ?

If aString is a single quote => set aString to " ’ "

Shouldn’t the quoted form of aString be three single quotes when displayed? => ’ ’ ’

It would be ’ ’ \ ’ ’ ’ on my machine (not the spaces). Because with quoted form “ a string supposed to be used for the shell and there a single quote has a special meaning “ you’re escaping the quote in the string by saying it has no special meaning. How would the shell know if it is the end of string or a meaningless character? The quoted string form in the shell differs a little bit. You can escape a special character or quote the whole string (same as quoted form of). the first command of example below is the same as quoted form of command and the second command is an escaped string

do shell script "echo  'x'\\''x'" --result: "x'x"
do shell script "echo x\\'x" --result: "x'x"

With some experimenting I think I figured out where I was confused.

tell application "Terminal" to do script "echo " & quoted form of " x\"x " --> echo ' x"x ' --> x"x 
tell application "Terminal" to do script "echo " & quoted form of " x'x " --> echo ' x' \' 'x ' --> x'x 

My initial question was why is there a different way of parsing how it’s send to the command line.
When double quotes are used it’s sent as a single statement, but when single quotes are used it’s sent as a triple statement.

double => ’ x"x ’
single => ’ x’ and ' and 'x ’

It does this since ’ x ' x ’ does not work. Why? Good question?

Now using double quotes you can write it like above => " x " x " and that does work.
You can also write it in three parts and it still works => " x" and " and "x "
In the actual command you take out the and

I think that explains it, although it still seems rather confusing… Spaces are for clarity.

Not really, you send every command seperated by a return or ; to the interpreter. The inpterpreter receives a string and start parsing it. While parsing it you can indicate with quotes which parts belongs to eachother. Normally values on the command line are separated by characters in the input field separator, a variable named IFS. By default it contains a newline, tab and a space.

--the command line interpreter sends hello and world as two seperate values to the process
set theFormat to "'arg1:%s" & return & "arg2:%s'"
do shell script "printf " & theFormat & " hello world"

I’ve used in the example above printf because echo isn’t able to show us the parameters. Now if we want hello world as a single argument to an application we need to escape it:

–the double backslash is for AppleScript needed, when it is send to the command line it’s a single backslash.

set theFormat to "'arg1:%s" & return & "arg2:%s'"
do shell script "printf " & theFormat & " hello\\ world"

The example above is also named a single character quotation. You told the interpreter that the next character is a space and not a delimiter/separator. There is also a strong quotation for the shell where hello world can be notated in many different ways.


set theFormat to "'arg1:%s" & return & "arg2:%s'"

do shell script "printf " & theFormat & " 'hello world'"
do shell script "printf " & theFormat & " \"hello world\""
do shell script "printf " & theFormat & " hello' 'world"
do shell script "printf " & theFormat & " hello\" \"world"
do shell script "printf " & theFormat & " 'hello'' 'world"
do shell script "printf " & theFormat & " \"hello\"\" \"world"
do shell script "printf " & theFormat & " 'hello'' ''world'"
do shell script "printf " & theFormat & " \"hello\"\" \"\"world\""
do shell script "printf " & theFormat & " hello' ''world'"
do shell script "printf " & theFormat & " hello\" \"\"world\""
--you can even mix single and double quotes in a string
do shell script "printf " & theFormat & " hello\" \"'world'"
do shell script "printf " & theFormat & " hello' '\"world\""
--I think this would be enough examples :D

Maybe a little to many examples but, as you can see, strong quotations does not indicate the beginning of a value nor does it indicate the end of a value. It turns on and off if the following characters are quoted or not. Then there is the quote in string

do shell script "echo x\\'x" --"x'x"
do shell script "echo \"x'x\"" --"x'x"
do shell script "echo 'x'\\''x'" --"x'x";same as quoted form of
do shell script "echo 'x'\"'\"'x'" --"x'x"
do shell script "echo 'x'\\'\"x\"" --"x'x"; the most odd one

But after all this and knowing how quotations works on the command line, quoted form of has never let me down. It has been working perfectly even when there are quoted form of quoted forms involved (like MySQL on the command line). Quoted form of is not only a solid command but also faster than inventing the wheel again with AppleScript code.