开发者

why doesn't 'do javascript' call in Applescript execute when the same code typed into browser does?

开发者 https://www.devze.com 2023-03-17 11:15 出处:网络
I\'m trying to figure out why my Applescript does nothing when the same javascript code typed into a Safari location bar works.

I'm trying to figure out why my Applescript does nothing when the same javascript code typed into a Safari location bar works.

Go to a search results page, such as: http://www.google.com/search?q=test . For the correct behavior, type this into the location bar and hit enter:

javascript: document.getElementsByClassName('vspib')[0].click();

You'll see that it selects the magnifier for the first search result.

This is what I want to make happen 开发者_运维百科via javascript. So I typed up the following:

tell application "Safari"
    activate
    do JavaScript "document.getElementsByClassName('vspib')[0].click();" in document 1
end tell

However, it does nothing. Any ideas?


The problem is that do JavaScript has to correctly address a tab in a Safari window.

The following script works for me, if the search results page is the current tab in the frontmost Safari window:

tell application "Safari"
    activate
    set theScript to "document.getElementsByClassName('vspib')[0].click();"
    do JavaScript theScript in current tab of first window
end tell
0

精彩评论

暂无评论...
验证码 换一张
取 消