Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

safari - Return Javascript variable to AppleScript

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...