Pass variables back and forth between Applescript and Javascript
Dec 22, '05 05:27:00AM • Contributed by: leenoble_uk
Dec 22, '05 05:27:00AM • Contributed by: leenoble_uk
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:
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.
•
[21,975 views]
