将 Javascript 变量返回到 AppleScript [英] Return Javascript variable to AppleScript
问题描述
所以,我有一个简单的问题,这在 AppleScript 中可能是不可能的.我正在尝试从代码中没有返回的 javascript 返回一个变量.换句话说,我正在尝试获取元素的状态,以便我可以有条件地运行函数.这是我不会得到回报的代码:
So, I have a simple problem which may not be possible in AppleScript. I'm attempting to return a variable from a javascript that doesn't have a return in the code. In other words, I'm trying to get the status of an element so that I can run a function conditionally. Here is the code I have that will not get a return:
tell application "Safari"
activate
open location "http://127.0.0.1:9999"
delay 1
set myVar to do JavaScript "var obj = document.getElementById(87); myVar = obj.value; return myVar;" in document 1
return myVar
end tell
所以,基本上我试图获取 obj
的值并将其返回给 AppleScript,它应该在 0 到 255 之间(它是一个按钮).问题是,我无法控制 javascript 函数,因此无法添加 return obj.value
.这可能吗?
So, basically I'm trying to get the value of obj
and return it to AppleScript, which should be between 0 and 255 (it's a button). The problem is, I don't have any control over the javascript function so I can't add a return obj.value
. Is this possible?
推荐答案
删除return"和var".do JavaScript"将返回最后一个全局值.例如,这应该返回 7.0:
Remove the "return" as well as the "var". "do JavaScript" will return the last global value. This, for example, should return 7.0:
tell application "Safari"
activate
open location "http://www.hoboes.com/Mimsy/"
delay 1
set myVar to do JavaScript "numbervalue=3;fred=numbervalue+4" in document 1
return myVar
end tell
然而,这应该返回 3.0,因为这是最后一个全局值:
This, however, should return 3.0, as that’s the last global value:
tell application "Safari"
activate
open location "http://www.hoboes.com/Mimsy/"
delay 1
set myVar to do JavaScript "numbervalue=3; var fred=numbervalue+4" in document 1
return myVar
end tell
这篇关于将 Javascript 变量返回到 AppleScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!