gadgetGuy
gadgetGuy

Reputation: 45

Pass applescript variable to Javascript

I cannot get an applescript variable to be recognized within a do Javascript block. Previous versions of Script Editor seem to recognize the variable, but not Yosemite.

set myVar to 123
tell application "Safari" 
do Javascript " document.getElementById('filter1FieldValueId').value= dsid ;" in front document  
end tell

This is one of the various tasks I need to accomplish, and if anyone can suggest how to give JavaScript a variable outside of the DO JAVASCRIPT, that would be so helpful..

Upvotes: 3

Views: 1874

Answers (1)

Alexander O'Mara
Alexander O'Mara

Reputation: 60597

You need to concatenate the string together into a valid JavaScript string using the concatenate & operator. AppleScript will not interpret variables in a string.

set myVar to 123
tell application "Safari" 
    do Javascript " document.getElementById('filter1FieldValueId').value= " & myVar & " ;" in front document  
end tell

Upvotes: 4

Related Questions