Click Links on a Safari Web Page


A webpage contains links to images, as seen on the screenshot each photo has a unique id and is generated randomly in every page.

The question is how to locate a link after opening a page, then open it in a new tab without using mouse coordinates / find images on screen.

Opening every link which ends with .jpeg / .jpg / .png would be more than okay.

Thanks in advance.

There's probably a way to do this with JavaScript, but my solution would be to use a shell script and lynx

The macro would need to get to the URL field (keystroke: ⌘ L) and then copy the URL to the clipboard (keystroke: ⌘ C) and then have this script run:

#!/bin/zsh -f 

if [[ -e "$HOME/.path" ]]
	source "$HOME/.path"

cd ~/Downloads/

lynx -listonly -dump -nomargins -nonumbers "`pbpaste`" \
| egrep -i '\.(png|jpg|jpeg)$' \
| while read line 

	curl --remote-name -sfL "$line"


exit 0 

That will download all of the linked png / jpg / jpeg files on the current page to ~/Downloads/

Of course you'd also have to have lynx installed, which isn't a default part of OS X (I assume for some sort of license reason) but you can get it easily via Homebrew.

But someone else will probably post a better JavaScript alternative that doesn't require Unix tools.

@tjluoma Thank you for the suggestion, i see that @ccstone has an answer in another topic to return links containing specified format in GC and it does the work!

I need your help to modify the script to return image links in Safari
Many many thanks in advance!

If all of the images's URLs end with "_govermnet-id.jpeg", which I'm inferring they do from your examples, then this sequence of actions ought to retrieve all of the images source URLs containing that portion of text in them, store them in a variable, one per line, then iterate through the lines and open each URL in a Safari tab.

JavaScript Code
Array.from(document.images, x => x.src)
	 .filter(e => e.indexOf("_government-id.jpeg") != -1)

I obviously cannot test it with your specific website, but I did a trial run on this page, looking for image URLs that contain the word "letter" and, indeed, I got all the images of "K" opening up in their own tabs (which are the letter_avatar_proxy images used for site users with no avatar uploaded), anyway, it worked.

1 Like

Hey Kirill,

If you want the images themselves then @CJK's excellent JavaScript should work for you.

But – I feel like you don't really want the images themselves.

You want what what's linked to the images – yes?

If that is the case then this is a place to start:

Extract Link Information.kmmacros (4.6 KB)

Don't try to click the link – try to extract the link.

If you can extract the link you can do whatever you want with it.

Of course this method doesn't always work, because there may be a click-event attached rather than an href.


@CJK's method works great with almost every page but it doesn't work with the target page :frowning:

@JMichaelTX's method to extract links with Xpath solves my question.

Thanks to everyone!