Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Pass variables back and forth between Applescript and Javascript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Pass variables back and forth between Applescript and Javascript
Authored by: jonn8n on Dec 22, '05 09:12:19AM
Um, all you have to do is to include a return value in your JavaScript command:

--run this a few times without recompiling...
property myVar : missing value
tell application "Safari"
	if myVar = missing value then set myVar to 1
	set myVar to do JavaScript "
var myVar = " & myVar & ";
myVar = myVar + 1;
return myVar;
" in document 1
end tell
display dialog "myVar is now: " & myVar buttons {"OK"} default button 1 with icon 1 giving up after 5

Jon

PS For the record, "do JavaScript" is a Safari-specific AppleScript command, not a generic AppleScript command. Some people might be confused from the original post.

[ Reply to This | # ]
Pass variables back and forth between Applescript and Javascript
Authored by: jonn8n on Dec 22, '05 09:18:57AM
Oh, and it works equally well for other types of returned values (NB: let AppleScript escape your text using the 'quoted form of ' command; manually adding quotes can lead to lots of problems):

tell application "Safari"
	set myVar to "My word:"
	set myVar to do JavaScript "
var myVar = " & (quoted form of myVar) & ";
myVar = myVar + ' cheese';
return myVar;
" in document 1
end tell
display dialog "myVar is now: " & myVar buttons {"OK"} default button 1 with icon 1 giving up after 5

Jon

[ Reply to This | # ]