Iterate Through Links on a Safari Page

I apologize if this has already been answered in the forum, I wasn't able to find a solution.

So I am trying to build a macro (that works as part of a much larger macro) that will select each link in a safari window. After clicking the link, the resulting webpage will be processed. Then I will navigate, using the browser shortcut for "Back," select the next link, and repeat until all links have been selected and processed.

Is this possible to do? I know that KM has the capability to see all the links in the front safari window (and the good thing is that there are no links on the page that need to be avoided....they all must be selected and processed), but I do not know how to iterate through them and process. Would javascript get the job done?

I would really appreciate any ideas. Thanks!

Although you are correct that Javascript probably has the tools to do this, I don't think you need Javascript at all. You see, MacOS has a feature that I think makes this entirely possible with the keyboard, which can be controlled by KM.

Take a look at the setting above. On your Mac, the highlighted line is probably disabled. Try turning it on. Then look at how the Tab key works in any browser.

In my opinion, this is the key to getting your job done with KM and not with Javascript. However at this early hour in the day I'm not writing all the code to make it work. But I think this tool is the main trick to make it work with KM. Give it some thought. See how the TAB and RETURN keys work when you turn this on. If you need more hints, let me know.

Sleepy, thanks so much. That makes a lot of sense. I'm going to try to get it to work this evening.

Thanks again.

I'm glad you are happy, at least for now. It still won't be trivial, but I think it will be doable without AppleScript. I'm not against using AppleScript, I'm just pretty poor with it.

This method would be painfully fragile and tedious to use.

Much better to script the process if possible.

Safari ⇢ Harvest Links from the Front Web Page v1.00.kmmacros (7.2 KB)
Keyboard Maestro Export

3 Likes

Awesome. Thanks a ton @ccstone I'm going to see if I can get it working tonight!

1 Like