将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屋!