Applescript and Javascript and single quote (apostrophe)

Hi,

I’m producing several hundred Photoshop images that need to have dialogue placed into them. I’m using a Javascript placed into an Applescript to set the text… it all works fine until I need to include a single quote in the text.

For example on one image I need the phrase “I’m on my way round to Dave’s place”

If I set the variable in Applescript to:

set myVar to "I'm on my way round to Dave's place"

and then in the Javascript I call the text as:

putString( idTxt, '" & myVar  &   "' );

everything grinds to a halt on the first single quote in the word “I’m”

The speech is mainly made up of London slang and nearly every image calls for an apostrophe. Is there a way that I can use a Unicode character?

Thanks in anticipation (and desperation)!

Hi there,

Have you tried ‘escaping’ the apostrophe? If not with a single backslash then a double backslash?
Not sure what version of Photoshop you’re using, you may need to amend the code below accordingly, but give this a try.


tell application "Adobe Photoshop CC 2014"
	
	do javascript "alert('Hell\\'o World!');"
	
end tell

This works for me, I get a dialog with the apostrophe included.

HTH

Here an example code how you can work with strings and how to escape them when using single or souble quote strings in JavaScript. This is Safari but the same escaping is required for PhotoShop.

-- escape the double quotes when using a double quote string
set myVar to "I'm on my way \\\"round\\\" to Dave's place"
tell application "Safari"
	activate
	tell document 1
		do JavaScript "alert(\"" & myVar & "\")"
	end tell
end tell

-- OR

-- escape the single quotes when using a single quote string
set myVar to "I\\'m on my way \"round\" to Dave\\'s place"

tell application "Safari"
	activate
	tell document 1
		do JavaScript "alert('" & myVar & "')"
	end tell
end tell

When creating javascripts for Photoshop I find it helpful to use the ExtendScript Toolkit.
Once I’m happy the javascript is working ok I put that into an applescript.
The same goes for InDesign javascripts.