Passing text data from Applescript to Javascript

I have a series of Applescripts that run in Photoshop CS5 that set the contents of text objects with variable data read from a plist. In addition to editing the normal text objects in text layers I need to be able to edit the contents of a text object in a smart object. To accomplish this I believe I need to use the listener and use javascript. I plan to run the Javascript from within the Applescript using do javascript command. My dilemna is how do I pass the text variable from the Applescript to the Javascript?

As you can see I am a novice scripter!

This first script works fine as the variable myVar is inside the Javascript.


tell application "Adobe Photoshop CS5"
	activate
	set theJavaCode to "{	
myVar = 'Get Shorty'
var originalUnit = preferences.rulerUnits
preferences.rulerUnits = Units.MM
var docRef = app.documents.add( 100, 150 )
var artLayerRef = docRef.artLayers.add()
artLayerRef.kind = LayerKind.TEXT
var textItemRef = artLayerRef.textItem

textItemRef.contents = myVar

docRef = null
artLayerRef = null
textItemRef = null
app.preferences.rulerUnits = originalUnit
 }"
	do javascript theJavaCode
end tell

This second script does not work as I cannot figure out how to pass the text data from the Applescript to the variable contained inside the Javascript.


tell application "Adobe Photoshop CS5"
	activate	
	set myVar to "Get Shorty"	
	set theJavaCode to "{		
var originalUnit = preferences.rulerUnits
preferences.rulerUnits = Units.MM
var docRef = app.documents.add( 100, 150 )
var artLayerRef = docRef.artLayers.add()
artLayerRef.kind = LayerKind.TEXT
var textItemRef = artLayerRef.textItem

textItemRef.contents = myVar

docRef = null
artLayerRef = null
textItemRef = null
app.preferences.rulerUnits = originalUnit
 }"	
	do javascript theJavaCode
end tell

Hi,

as both the javascript code and the variable value are plain text, just split the literal text and insert the variable name


tell application "Adobe Photoshop CS5"
	activate
	set myVar to "Get Shorty"
	set theJavaCode to "{        
var originalUnit = preferences.rulerUnits
preferences.rulerUnits = Units.MM
var docRef = app.documents.add( 100, 150 )
var artLayerRef = docRef.artLayers.add()
artLayerRef.kind = LayerKind.TEXT
var textItemRef = artLayerRef.textItem

textItemRef.contents = " & myVar & "

docRef = null
artLayerRef = null
textItemRef = null
app.preferences.rulerUnits = originalUnit
}"
	do javascript theJavaCode
end tell

Hi Stefan,

Thanks for the reply, I copied your code immediately but keep getting an error message. I must be doing something completely foolish.

Here’s the error message I get.
http://imaginationpartners.co.uk/public/error.jpg

Cheers.

probably the single quotes from the first example are missing before the first and after the second double quote


textItemRef.contents = '" & myVar & "'

Good evening Stefan,

Thank you for your response. It does indeed work perfectly now. This is the second time you have helped me and I am very grateful for your generosity.

Kindest regards
Martin Eaglen