How Would I Get the Line Number of Selected Text in a Variable?

Very helpful in seeing if our listing is in the SERPs of different google searches, thank you.
How would I get the line number of the text I'm looking for in the variable. GS__LinkList
ie. I'm looking for house-cleaning within the URL's that are returned, if that text is in one of the URL's I'd like to know what number line its on.
This way I can programmatically track the results monthly.
Thank you in advance.

Apologies, I just realized I replied to your 'Macro Library' post.
Can you move this to a new post titled
Get line numbers of variable or something like that?

I've revised my macro from the other topic to provide an example of what you are now asking for.

Example Output



MACRO:   Get Selected URL and Line Number from List [Example]

--- VER: 2.0    2019-08-23 ---
Requires: KM 9+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

Get Selected URL and Line Number from List [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


Author.@JMichaelTX based on script by @ccstone


  • Get List of URLs (or MD Links) from Google Search Results
  • Works for FrontMost Browser


  1. Do a Google Search in either Safari, Chrome, or Chrome-based Browser
  2. Trigger this macro


  1. Scans the Google results, and returns one link per result
    • Each Line contains LineNumber«Link
  2. User is then prompted to select one line from results
  3. The Line# and Link are displayed


  1. Review the Actions in magena color, and change as desired
  • Local__Max_Links
  • Local__Return_Data
  1. IF Local__Return_Data is set to "MD", then it returns Markdown links
  • Default: URL
  • Markdown Format: [Link Text](URL)
  1. The max number of links returned is set by the Variable Local__Max_Links
  • Default: 10
  1. ADD the Actions you want to process the results (list of URLs)

TAGS: @XPath @Google @Chrome @Search @Links @JavaScript

• Any Action in magenta color is designed to be changed by end-user



• To facilitate the reading, customizing, and maintenance of this macro,
key Actions are colored as follows:
• GREEN -- Key Comments designed to highlight main sections of macro
• MAGENTA -- Actions designed to be customized by user
• YELLOW -- Primary Actions (usually the main purpose of the macro)
• ORANGE -- Actions that permanently destroy Varibles or Clipboards


(1) Keyboard Maestro Ver 7.2.1+
(2) Yosemite (10.10.5)+
(3) Safari or Google Chrome

1 Like

Thank you @JMichaelTX for your time.
I do get an error when running the macro. Although I have worked a bit revising your initial macro to create what is a good solution for me.
I periodically check our SERPs.
We have 2 different websites that I check for.
This macro will check the current results and show if either of those 2 sites are listed and their # position.
I know my macro could be done cleaner, and it's probably a bit rough around the edges but for now, it does exactly what I need it to do.
Could have never ever gotten to this point without your expert help and initial macro, so as always, many thank you's to you @JMichaelTX

[WEB] Get URLs from Google Search Results TK.kmmacros (36 KB)

Can you please post a screenshot of the error.

You're welcome. Glad you got it working.

I looked as best I could at your macro, not sure if I need to do anything to make it work.

at this point I don't need it, but making it work would as always, help me learn.

That error means that the Execute JavaScript did NOT find any results of the Google Search.
Did you first do a Google Search, and with that as the frontmost window, trigger the macro?

If you still can't resolve the error, change the Execute JavaScript Action to "display results in a window" so you can see what it returns. Copy/Screenshot that an post here.


ok, I'm obviously missing the entire boat here, I apologize.
I downloaded a fresh macro from above.
I did a google search in safari.
I ran the script - it error'd out.
I changed the JavaScript action from 'save results to a variable' to 'display results in a window and it still error's.
As I said, I don't need this particular macro any longer, it was the complete basis for the macro I altered and now works for me.
But I don't need to make this work.
I appreciate your help.

If you want to debug/fix my macro, then I'll need for you to show a screenshot of the JavaScript window when the error occurs.

Search google for Yonkers House Cleaning
Run the macro - it does not display the results in a window - it returns nothing.
2019-08-26 12:47:16 Prompt With List failed with no result Macro “Get Selected URL and Line Number from List [Example]” cancelled (while executing Prompt With List “” from Variable “Local__URL_List”).