I am using a Applescript to use do shell script and curl to post a form. I need to escape any double quotes in a given variable. I have searched and searched on this and not having any luck. Maybe Im just searching wrong. Seems like a lot of people want to do this, but no clear answers. I have no control over the data in the text field and can’t do this manually. There has to be something to do this automagically.
Basically, my curl command includes text for a given field in " quotes ". However, if one of those text variables contains a " then I’d have something like… " quotes " in my text field ".
The middle " as part of the variable is killing me. I need to escape it. It should be something like " quotes " in my text field ". However, I need to apply this feature to the string and escape any " in the string for this to work right.
Im having trouble finding an easy way to escape the " in the string.
When escaping quotes (single and double as well as other reserved characters), use the “quoted form of” command. This documented in the AppleScript documentation and discussed many times in these forums as well as other forums (i.e., Apple’s help boards). Here’s an example:
set the_URL to "http://www.domain.com/poorly \"formed\" URL"
do shell script "curl " & quoted form of the_URL
Thanks, but thats not it. In the above example you show a manual input of the escaped, that is not my issue.
Here is what I have in part…
set dataSource to do shell script "curl --form datafield=\"" & myString1 & "\" [url=http://www.domain.com/hello.html]http://www.domain.com/hello.html"[/url]
The variable “myString1” is a large field which could contain a double qoute inside of it. I need to escape that one string. If I use “quoted form of myString1” it doesn’t do the job. If I do unix_path(myString1) it WORKS, but it escapes out spaces and is not the correct way to do this.
Strange, becuase I can’t get " inside a quote unless I precede it with \
Does the string appear as
“this is a “test” of the string system”
when shown in your result window?
If I try to make the string a variable, the result is “this is a "test" of the string system”
Try to determine what AS thinks this character is:
-YourString is a stored string “this is a “test” of the string system”
set Chars to characters of YourString
repeat with thisChar in YourText
display dialog "\"" & thisChar & "\"" & " is ASCII number " & (ASCII number thisChar) as text
end repeat
When it gets to " it will give you a number to reference the character by.
SC
By the way,
quote " is ASCII 210
and end quote " is ASCII 211
No, the “quoted form of” command is what you want. The fact that I escaped the quotes in my example doesn’t negate that. Try this, in the following code, when prompted, input a string with as many single and double quotes as you want (without escaping them) and then look at the final result. In the example, I entered the following text: This is a “quote”, isn’t it?
set the_input to (display dialog "Enter some text (include some single and double quotes without escape backslashes):" default answer "" buttons {"OK"} default button 1 with icon 1)'s text returned
return quoted form of the_input
-->"'This is a \"quote\", isn'\\''t it?'"
When you pass this to a shell script, it will look like:
No matter how you get your string: from a variable, from a display dialog result, from reading a file, from a shell script, from a text filed or text view in an AppleScript Studio app, whatever, using the “quoted form of” command will work for your needs.
So, in your code, try this (don’t try to quote or escape the string yourself):
set dataSource to do shell script "curl --form datafield=" & quoted form of myString1 & " [url=http://www.domain.com/hello.html]http://www.domain.com/hello.html"[/url]