最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

safari - Return Javascript variable to AppleScript - Stack Overflow

programmeradmin2浏览0评论

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?

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?

Share Improve this question edited Apr 22, 2014 at 16:33 Darrick Herwehe 3,7521 gold badge24 silver badges33 bronze badges asked Apr 22, 2014 at 16:01 Cornelius QualleyCornelius Qualley 7615 gold badges10 silver badges19 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

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./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./Mimsy/"
    delay 1
    set myVar to do JavaScript "numbervalue=3; var fred=numbervalue+4" in document 1
    return myVar
end tell
发布评论

评论列表(0)

  1. 暂无评论