Copy Text From a Web Table -- IF Column A Contains a Given String Copy Column B

Hi :wave: awesome people!

I am super new to KM.

I have a table in a website, it usually has 4 rows but sometimes 1 of them is missing and then has only 3 rows, or may have only 1 row… then the JS path changes and my macro does not work.

If column A reads “ HAWAII COUNTY” i want to copy column B (Sales) and paste it into another website. This is the macro I created which works perfectly when all 4 rows (counties) are present, but when one of then is missing the JavaScript path changes and then it does not work.

My Macro

Table when it has all 4 counties:

If all 4 counties are present the JS for HAWAII Sales is always the same: document.querySelector("#expected-wholesale-locals > tbody > tr:nth-child(3) > td.wholesale-sales.text-right > span")

But if there's only 1 or 2 counties it changes to :

document.querySelector("#expected-wholesale-locals > tbody > tr:nth-child(1) > td.wholesale-sales.text-right > span") or

document.querySelector("#expected-wholesale-locals > tbody > tr:nth-child(2) > td.wholesale-sales.text-right > span")

Depending on what rows if falls... How can I fix this? Is there a way to say instead copy B if A reads "HAWAII"?

Some HTML coding:


I'd suggest basically you need to read the left column of each successive row until you find one that is HAWAII COUNTY and then read the right column.

You could either do the looping in JavaScript, or in Keyboard Maestro, depending on which you are more familiar with. Either way, you basically want to do something like:

  • For each n in number rang 1…10
    • Read innerText of the n’th row’s first column
    • If the innerText is “HAWAII COUNTY” then
      • Read the innerText of the n’th row’s fourth column, and break from the for each loop


Thank you so much!!!

Unfortunately I am super new to both KM and JavaScript! Could you please show the steps in KM so I can get a better idea?


Hey Yisbel,

Can you give us the URL to the actual web page where the table resides?


Hi Chris!!

The website is not public, you need a login info to access. But I found this website with a table that may serve the same purpose!!!

I would need a macro that copies column C if Column A value is Blount, but JS path is not always the same, it varies depending on how many counties there are listed before Blount...

How do I create a macro that says if Column A reads Blount go to Column C and copy that value to the system clipboard?

I appreciate your help!!!!

Hey Yisbel,

You can make this task pretty complex, or you can boil it down into something reasonably simple.

The first macro below extracts the innerText of the entire table and then uses Keyboard Maestro's Search Variable action to parse for a row that starts with "Blount".

The working regular expression looks for:


If that's found the entire line will be returned.

Only the first instance is searched for in this macro – other instances will be ignored.

You can easily parse the rest from there.

If you need to get all lines that start with "Blount" then you have to jump through another couple of hoops using native Keyboard Maestro actions, but it's not overly difficult.

Extract Text From Web Table v1.00.kmmacros (8.0 KB)


Keyboard Maestro Export

This macro uses JavaScript for all the heavy lifting and will return ALL lines matching the regular expression:

Extract Text From Web Table (JavaScript) v1.00 .kmmacros (6.8 KB)


Keyboard Maestro Export

You have to know a bit more JavaScript to get this working than the first macro.

I reckon you could also loop through the lines of the table-object and use a regex filter to find any relevant rows, but I don't want to fool with that right now.

This general methodology keeps you from having to use a hard path to the table row you're looking for.

It should also be possible to use XPath and a specifier to look for a row beginning with certain content in a certain table – but that's beyond me at the moment, and I don't want to spend time figuring it out.

@ComplexPoint ought to be able to pull this out of his hat though...


1 Like