I just tried the pizza string with one of the scripts and it worked fine. The emojis have issues but that is unrelated. However, if you have an email string that contains double quotes, we can test with that.
Please note that the âknightâ strings I provided were these below. The first case is for when you provide the string within the script (eg set var to âaâ), the second when itâs brought in from outside the script, for example with the read command or using the clipboard. The second one is probably best suited for your case, so you need to insert a backslash before each double quote.
king's \\\"knight\\\"
and
king's \"knight\"
It was not this:
king's 'knight'
You are correct about what âquoted form ofâ does to single quotes, which is why this doesnât provide a general solution. @jambomac has the right idea, which is to use the quote constant, which is what Iâve done although I only apply this to the string as the javascript command on its own is fine.
So, the issue at this point, is how to add a backslash before the double quotes within your text. You should be able to do a simple search and replace using delimiters.
Testing
I created an html page with the source code below. It has a âtextareaâ in it. You can save this as an html file and open it in safari as a test page.
Here is an example script. Create a text file on the desktop containing the string queen's "knight" with the name ârawâ. The script will place a single backslash before each of the two double quotes and then replace whatever is in the web pageâs textarea with the original text. If it works, put your own string in the text file and try again.
This way, you can see exactly what the script does with a generic textarea.
Script
-- contents of example text file
--> queenâs "knight"
-- read text file
set fid to ((path to desktop as text) & "raw")
set redd to read file fid as «class utf8»
-- split text on "
set text item delimiters to quote
set tim to text items of redd
-- rejoin text around \"
set text item delimiters to "\\\""
set mys to tim as text
-- queenâs "knight" becomes "queenâs \"knight\""
-- wrap entire string in quotes
set mys to quote & mys & quote
-- set value of textarea
tell application "Safari"
activate
set JSCommand to "document.getElementsByTagName('textarea')[0].value = " & mys & " ;"
log JSCommand
do JavaScript JSCommand in document 1
end tell
HTML
<!DOCTYPE html>
<head>
<title>Test for 'textarea'</title>
<meta charset="utf-8">
<style>
h2 {font-size:1.8em; text-align:center; color:#249eb1;}
h2 {max-width:20em;line-height:1.3em; padding-left:6px;}
p {max-width:30em; font-size:1.2em; line-height:1.4em;}
</style>
</head>
<body>
<h2>Leave a comment:</h2>
<p>textarea below đ</p>
<textarea rows="8" cols="60" placeholder="Write your comment hereâŠ"></textarea>
</body>
</html>