skiaddict1
skiaddict1

Reputation: 525

Applescript: how to "tell" an app in the background?

I'm using Chrome to get the HTML of a webpage which is generated by javascript. The applescript which does all this needs to run every 2 minutes. Everything is working perfectly, except that I obviously need Chrome to do this completely in the background. My script contains the following uses of Chrome (as well as a block to set theTab, which doesn't seem to ever cause Chrome to come to the front):

set URL of theTab to theURL
set isLoading to (loading of theTab)
execute front window's active tab javascript javascriptLocation
set theSource to execute front window's active tab javascript "document.documentElement.outerHTML"

Putting this line:

tell application "Finder" to set visible of process "Google Chrome" to false

after each of the above lines either produces no hiding at all, or at best Chrome flashes onscreen and then goes away. I find this very distracting.

Is there any way to have an application run reliably and permanently in the backround? Or, failing this, is there an invisible way to get javascript executed server-side so an applescript can get hold of its generated source?

Chrome 66.0.3359.181 running on Mac OS 10.11.6, Applescript 2.5.

Upvotes: 1

Views: 1378

Answers (1)

skiaddict1
skiaddict1

Reputation: 525

The following is not an answer to the question I posted, but it is an answer to the problem I was trying to solve which is why I posted the question.

As stated in my question, I need to get hold of the HTML which some javascript generates on a site (which is not under my control). I can't do a client-side scrape because of CORS restrictions on the site. I tried the cross-domain tools listed here and couldn't get them to work.

So I was using Chrome's applescript command, execute, to first execute the javascript (to produce the HTML), and then a second time to grab hold of the HTML with document.documentElement.outerHTML. But having Chrome flash onto the screen every 2 minutes throughout the day was doing my head in.

Turns out Chrome can also run in headless mode, from the command line, and just happens to have an option to run javascript and return the generated HTML!

So my code got a whole lot simpler and I don't have to have Chrome in my apps list all the time. Happy coder am I :-)

Here's the one line that gets me the HTML generated on the site I need:

set theSource to (do shell script ((quoted form of POSIX path of googlePath) & " --headless --dump-dom " & theURL))

Thanks, once again, @matt. I'd never heard of headless mode and would never have found this without your suggestion of PhantomJS!

Upvotes: 2

Related Questions