Google Chrome Scripting Question (Trying to Get Text via AppleScript)

Hello, Everyone

Got a silly Applescript question. I am trying to grab the text (not source code) out of an open Chrome tab so I can parse out some information in it. Chrome is the best browser IMO, but Safari has support for "get text of document one, like this

tell application "Safari"
   set theText to text of document 1
end tell

and brute-force things from there.

But I can't find an easy way to do this in Applescript in Chrome. Does anyone have any ideas? My workaround is for KM to select all text and copy, which feels inelegant.

Another way, I know, is to figure out how to get the info using difficult scripts as outlined by @JMichaelTX here:

Get Text of Element Using XPath Contains Text [Example]

But as I'm an inexperienced programmer this is probably beyond me. The source code of the code in question seems pretty encrypted, and I can't find clean any kind of hooks like in his example of how to get the data that way.

So if there's any "best" way to get the text from an open website using Chrome either using Applescript or KM, that'd be great to know.

Thanks!

1 Like

Very easy -- no AppleScript required:

image

%ChromeJavaScript%document.body.innerText%

2 Likes

Thanks! There's always a simpler way to do things directly with KM...

That is very helpful

Can you share where/how you looked this up or figured it out - I am pondering what other tricks are out there I can discover with those resources.

Sure. I started with a search in the KM Editor App for inserting a Token, and I entered "chrome" and got this:

The very first item got my attention, so I click on the "Help" link with it selected.

That takes you to this KM Wiki page:
https://wiki.keyboardmaestro.com/token/ChromeJavaScript

with this:

The %ChromeJavaScript%document.forms[0].innerHTML% token returns the result of some JavaScript executed in the current Google Chrome tab.

I know enough HTML and JavaScript to realise that returns the HTML code for a form.
But we want the page text, which is ".innerText" from the document.body

So I just quickly tested:
%ChromeJavaScript%document.body.innerText%

and found that it worked!

I did not previously know about that KM Token.
I usually use Execute a JavaScript in Browser actions which would also work. But since we need ONLY ONE line of JavaScript, this KM Token was perfect.

1 Like

Thank you immensely @JMichaelTX - that puts a lot of things into perspective and shows how KM can be even more powerful by combining it with Javascript - this is excellent motivation for some self-learning on Javascript.

1 Like

Trying this out myself now - any time I enter a token referencing Chrome, while in the editor KM appears to immediately try to fetch the content from Chrome and I get a spinning pinwheel from which I can escape only through a Forced Quit.

What do I have wrong to cause this? (I know I am missing the trailing % - I got the spinning pinwheel before I could complete it. I also get the same response if I enter similar Javascript related tokens directly from the KM Editor menus.)

image

Yep, that's a "feature" of KM that I don't particularly like.
While you are in the KM Editor, KM will try to evaluate each Action, while you are still editing the Macro. So, in this case, it is trying to get the innerText of the front Chrome page.

The easiest workaround is to open Chrome to a short page so KM has something to find.

I thought it might be a problem is Chrome was not running, but I just tested this with Safari, and it works fine, indicating that Safari is "Not Running":

image

So, KM should NOT be throwing up a spinning beachball, or be locked up.
Try quitting Chrome and/or KM Editor and reopen KM and see if you get the same problem.
If so, then I'd report it as an issue to @peternlewis.

On Safari I got an alert reminding me to enable "Allow Javascript from Apple Events" - once I did that it worked perfectly.

On Chrome I similarly enabled the same option. Now the variable populates in KM (so that is progress) but I still get the spinning pinwheel and need to do a Force Quit in KM.

@peternlewis - any thoughts?

BTW @JMichaelTX and @peternlewis - It does work fine when using Execute Javascript to perform the equivalent command

image

The Chrome tokens access Chrome via AppleScript.

If Chrome is not running, they will simply display Not Running.

If the AppleScript locks up, well, unfortunately, that is AppleScript - AppleScript does not run asynchronously, which is why Keyboard Maestro uses a sub-tool for the job, but that is not practical for token which should always respond in a timely manner.

I suggest restarting or otherwise figuring out why the simply AppleScript request to Chrome is locking up.

Any idea why essentially the same function works in "Execute Javascript in Google Chrome" but does not work with "Set Variable" when choosing a Chrome Javascript variable?

Execute JavaScript uses an external process to handle the AppleScript. No matter what happens with the AppleScript, it wont lock the engine up.

The Chrome tokens use AppleScript within Keyboard Maestro itself. If AppleScript misbehaves, the engine will lock up.

1 Like

Thanks... last question

When it locked up, I could not even get back in by starting the editor without the engine - that seems odd. Why would that be? In retrospect, I should have tried starting it after quitting Chrome; I did not think of that at the time and instead restored an earlier version of my KM macros from time machine.

In any event - what an amazing application this is. Increasingly I am thinking of KM as basically a programming environment or mini IDE rather than simply a keyboard macro program.

The editor is probably displaying the same tokens, hence executing the same AppleScript. It sounds like Chrome was locked up (or the AppleScript subsystem in Chrome or the system was locked up).

1 Like

Where is this screen come up? Thanks.

KM Editor menu Edit > Insert Token > By Name...

1 Like