2

好吧,这是我的问题,我试图在 AppleScript 中编写代码以打开网页并复制所有文本,然后将其保存到变量中,稍后再使用该变量,问题是我已经为 safari 和它运作良好,但对于 chrome,我不知道该怎么做,我今天盯着学习这种语言,所以任何帮助将不胜感激。这是我到目前为止所拥有的

    tell application "Google Chrome"
activate
set theurl to "http://www.webpage.com"
if (count every window) = 0 then
    make new window
end if

set found to false
set theTabIndex to -1
repeat with theWindow in every window
    set theTabIndex to 0
    repeat with theTab in every tab of theWindow
        set theTabIndex to theTabIndex + 1
        if theTab's URL = theurl then
            set found to true
            exit repeat
        end if
    end repeat

    if found then
        exit repeat
    end if
end repeat

if found then
    tell theTab to reload
    set theWindow's active tab index to theTabIndex
    set index of theWindow to 1
else
    tell window 1 to make new tab with properties {URL:theurl}
end if
    end tell


    tell application "Google Chrome"
tell tab 6 of window 1 to select all
tell tab 6 of window 1 to copy selection
--in here I dont know how to set the variable with the text i just copied 
    end tell
4

1 回答 1

4

您可以使用the clipboard as text.

tell application "Google Chrome" to tell tab 1 of window 1
    select all
    copy selection
end tell

set t to the clipboard as text

您还可以使用document.body.innerText

tell application "Google Chrome" to tell tab 1 of window 1
    set t to execute javascript "document.body.innerText"
end tell
于 2012-07-31T08:11:56.477 回答