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

Pass variables back and forth between Applescript and Javascript Web Browsers
I've been making use of the do JavaScript command in AppleScript, but I was frustrated that there is no way of getting information back from the script. The command always returns true, regardless of whether the JavaScript succeeds or not.

Getting AppleScript values into Javascript is very simple, but not documented on macosxhints yet, so here it is...

Example:
tell application "Safari"
  set myVar to "This is my variable"
  do JavaScript "
  var myVar = '" & myVar & "';
  alert(myVar);
  // do as you wish with myVar
  /**/" in document 1
end tell
The above won't return the proper values. So here's the workaround for getting values back again:
tell application "Safari"
  set oldName to (name of window 1) as string
  do JavaScript "
  var myJSvar = 'This is my variable';
  document.title = myJSvar;
  /**/" in document 1
  set myJSvar to ((name of window 1) as string)
  set name of window 1 to oldName
  -- do as you wish with myJSvar
end tell
This way, you can use the return values to tell if a JavaScript command was successful or not.
    •    
  • Currently 2.00 / 5
  You rated: 2 / 5 (6 votes cast)
 
[21,863 views]  

Pass variables back and forth between Applescript and Javascript | 3 comments | Create New Account
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 | # ]
"do javascript" command
Authored by: ngb on Dec 23, '05 01:07:39AM
Actually, Photoshop also supports a do javascript command, and values can be returned from it the same way as the original responder pointed out.

[ Reply to This | # ]