Luna
Luna

Reputation: 23

Apple script opening webpage on chrome and copy text

Well this is my problem, Im trying to write a code in AppleScript in order to open a webpage and copy all the text, then save it to a variable, and use that variable later, the problem is that I already did this for safari and it worked well, but for chrome I dont know how to do it, I stared today learning this language so any help will be appreciated. This is what I have so far

    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

Upvotes: 2

Views: 6005

Answers (1)

Lri
Lri

Reputation: 27613

You can get a plain text version of the clipboard with 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

You might also use document.body.innerText:

tell application "Google Chrome" to tell tab 1 of window 1
    set t to execute javascript "document.body.innerText"
end tell

Upvotes: 5

Related Questions