Dealing with strings that contain double-quotes

Hey Guys,

I know that I can use an escape sequence to make a double-quote in a string a literal double-quote (ie “hey “buddy”” = “hey "buddy"”), but what if I have a string that’s so huge that I don’t want to take the time to parse it and insert backslashes?

Specifically, I have log output from a process in a string variable that gets passed to a “do shell script” command. The “do shell script” then reaches the double-quote in the middle of the string and fails.

The string could be 10000+ lines, so I really don’t want to parse it and insert backslashes, but is there any other way?

Thanks,

TIm

Hi scriptim,

Not sure if I got what your asking, but what you can do is set a part of the string as a variable. Then, concatenate with the &. Sometimes that makes the quoting easier to me.

gl,
kel

That’s basically what I’m trying to avoid. Here’s the current solution, but it can be slow at times so I’d like something more elegant. I figure it’s applescript so there’s probably some functionality I’m not using here…

*Edit: this achieves the desired purpose, so when the returned string is fed to the “do shell script” all the " become normal quotes again, and all is well. But again, it’s slow…


on deQuoteString(aString)
	if aString contains "\"" then
		set parsedString to parseLine(aString, "\"")
		set parsedCount to count of parsedString
		set aString to ""
		repeat with i from 1 to parsedCount
			if i < parsedCount then
				set aString to (aString & item i of parsedString as text) & "\\\""
			else
				set aString to aString & item i of parsedString as text
			end if
		end repeat
	end if
	return aString
end deQuoteString

where the parseline handler is


on parseLine(theLine, delimiter)
	-- This came from Nigel Garvey
	
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {delimiter}
	set theTextItems to theLine's text items
	set AppleScript's text item delimiters to astid
	
	repeat with i from 1 to (count theTextItems)
		if (item i of theTextItems is "") then set item i of theTextItems to missing value
	end repeat
	
	return theTextItems's every text
end parseLine

There is no spoon

You don’t have to, put it in a file and read it, done. There is no escape character, it’s only a representation in AppleScript. So when a string in a file contains a double quote, AppleScript will present it with an escape character.

Hi.

set aString to "This is a \"text\" containing \"quotes\""

set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to quote
set aString to text items of aString
set AppleScript's text item delimiters to "\\" & quote
set aString to aString as text
set AppleScript's text item delimiters to astid

aString
--> "This is a \\\"text\\\" containing \\\"quotes\\\""

An alternative, of course, would be to wrap the string in single quotes within the ‘do shell script’ text.

Nigel for the win!

I had only used ASTID for breaking apart strings before, I didn’t know you could replace a character in this fashion. Very cool trick!

Thanks (again),

Tim

Just to be sure; are you using quoted form already? Quoted form makes sure the data is send as a whole. It seems that it was a bit late yesterday and didn’t see the exact problem you’re facing. Here an example what happens with quotes in quoted and not quoted forms in do shell scripts.

set string1 to "this string \"contains\" a double quote"
set string2 to "This string \\\"countains\\\" an escaped double quote"

do shell script "echo " & string1 --removed the quotes
do shell script "echo " & string2 --going one level up in quoting
do shell script "echo " & quoted form of string1 --returns the string as it is send
do shell script "echo " & quoted form of string2 --returns the string as it is send

I wanted to be sure that this isn’t your problem and that you’re not trying to quote text yourself (it’s a no-go). If it’s an quotation problem then use quoted form instead and text will be send the correct way to the do shell script.