Open & WebScrape Chrome Tab in Background [Example] Macro (v9.2)

Use Case

  • WebScrape a URL in the Background

Demo of Entire Process

  • Note that the new Tab never appears
  • The Entire process is run in the Background
  • If Google Chrome was hidden before the macro was triggered, it would continue to be hidden.

MACRO:   Open & WebScrape Chrome Tab in Background [Example]

UPDATED: 2021-05-30 16:30 GMT-5

  • Ver 2.1 script was rewritten to ensure the new tab never appears.
  • Now the entire process is run in the background.
  • See above demo.

--- VER: 2.1    2021-05-30 ---
Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

Open & WebScrape Chrome Tab in Background [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.
Macro was uploaded with a trigger of ⌃F1. Change to whatever you want.


Author.@JMichaelTX -- based on script by Chris @ccstone


  • Open & WebScrape Chrome Tab in Background [Example]


  1. First, make sure you have followed instructions in the Macro Setup below.
  2. See the below "How to Use" Comment Action
  3. This macro is just an example written in response to a user's request. You will need to use as an example and/or change to meet your workflow automation needs.


  • Carefully review the Release Notes and the Macro Actions
    • Make sure you understand what the Macro will do.
    • You are responsible for running the Macro, not me. ??
      Make These Changes to this Macro
  1. Assign a Trigger to this Macro .
  2. Move this macro to a Macro Group that is only Active when you need this Macro.
  3. ENABLE this Macro, and the Macro Group it is in.
    • For more info, see KM Wiki article on Macro Activation
    (all shown in the magenta color)
    • CHANGE to URL that you want to open and webscrape
      • DESC
    • CHANGE to JavaScript to WebScrape Above Web Page
      • AppleScript uses a 2 sec pause after URL is opened.


  1. KM 9.0+ (may work in KM 8.2+ in some cases)
  2. macOS 10.12.6 (Sierra)+

TAGS: @Example @WebScrape @Backgound @Chrome

1 Like

Thanks Michael.

Based on my limited understand of the AppleScript, what the Macro does is: open a new tab as active tab (foreground), do the web scrape, then switch active tab to the original active tab. So in essence, the web scrape part is still in active tab, not in the background.

Hey Henry,

Nyet. Scraping takes place in the new tab, and this is documented in the script:

tell newTab
   --- Set URL of New Tab ---
   set jsResults to execute javascript jsCmdOpenURLStr
   delay 2.0 -- would be better to do a pause util web page element is loaded
   --- WebScrape This New Web Page ---
   set jsResults to execute javascript jsCmdWebScrapeStr
end tell

Did you run the macro?

Did it not work correctly? It worked as expected for me.


1 Like

Yes I did run the Macro, the scraping takes place in the new tab, but it is scraping when the new tab is the active tab. The restore statement is after scraping is done.

By "background", what I meant is the new tab should never be the active tab. From my understanding, KM runs javascript at the active tab of the front window. If this is the case, then I think I have to look for something else for my use case (like headless chrome etc).


Just uploaded a new version.

I think this new version should meet your needs.
The entire process is run in the background.
See the animated GIF demo above in my OP.

1 Like

Yes this worked!

"tell newTab" did the trick to have the javascript executed on on newly tab which is not the active tab


1 Like

Actually, what did the trick was changing how the URL was set for the new tab:

set newTab to make new tab with properties {URL:urlStr}

I was always using tell new tab to execute the JavaScript.

1 Like

This statement actually opens new tab as active tab. I put 'delay 5' after this statement and it does stay in the new tab for 5 seconds then switches back to origTab. Without delay statement, Chrome just switched back to the origTab so quick that our eyes don't even notice it.

I looked into the " Open & WebScrape Chrome Tab in Background [Example] Macro (v9.2)" and it appears to save the current front browser tab, then moves the tab that you want to execute the JS in to front, executes the JS then restores the original tab to the front. It is supposed to happen so fast you don't notice it. On my machine the tab switch is very noticeable so this approach will not work for me. Maybe I need a faster machine.

I'll keep looking for another solution.

1 Like


Google Chrome will not allow you to create windows or tabs in the background for security reasons.

When you create a new window or tab Chrome automatically goes to it, and there's no way I know of using either AppleScript or JavaScript to get around this.

I have been using AppleScript to execute a Javascript in a Safari background tab and it has been working well for me. I created a generic example macro of this that scrapes the new topic count from the KBM forum page at: Keyboard Maestro Discourse

In order this macro to work, you need to have a tab at this URL up, but it does not need to be the frontmost window. When you run the macro it will display the number of new forum messages.

Extract KBM Forum New Count Macro (v10.2)

Extract KBM Forum New Count.kmmacros (4.5 KB)

In my application I use this method in several areas so I packaged the Applescript into a subroutine and then pass the Javascript in as a variable and return the JS results so I can parse the web scrape result.

I should have thought to mention that you could simply keep a singleton document or a tab in a document open in the background and use it at will without switching to it.