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
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.
Very easy -- no AppleScript required:
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:
But we want the page text, which is "
.innerText" from the
So I just quickly tested:
and found that it worked!
I did not previously know about that KM Token.
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.
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":
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 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?
The Chrome tokens access Chrome via AppleScript.
If Chrome is not running, they will simply display
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.
The Chrome tokens use AppleScript within Keyboard Maestro itself. If AppleScript misbehaves, the engine will lock up.
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).
Where is this screen come up? Thanks.
KM Editor menu Edit > Insert Token > By Name...