How to Copy an Image from a Web Page to the Clipboard or a File?

I have an image that I need to routinely copy/save. It is on a web page (specifically an extension, but essentially it's HTML) and it only shows when a dialog is shown.

I have created a script that opens the dialog, then I need to right-click and copy that image to clipboard). I have tried initiating a right click on the image by finding an element that will always follow the image, then move up by 50 pixels and fo the right click. This brings up the browser's context men, where I then do a Found image macro to find the 'Copy image' menu item.

It is VERY hit and miss. Barely ever works.

My preference would be to do this:

Open the dialog (this has to be done as the image is not in the DOM until the dialog is shown, dynamically injected)
Get the src attribute, and somehow save that image from there without having to bring up context menus, use Found IMage macro etc etc

What might be the best options for doing this?

Thanks :slight_smile:

Hey Lloyd,

Open your dialog and then run this and see if it works.

-Chris


Front Browser Window Source v1.00.kmmacros (2.0 KB)
Keyboard Maestro Export

This does run, but just so you know I am very comfortable with running scripts in browser (web developer in the past), and I use those a lot for DOM manipulation.

Now, this script takes the text content, but what I need is to copy one specific image. Using the browser context menu is flaky. I can easily identify the image src attribute, is I wondered if I can retrieve that src for the image, can I feed that to something else to then download/save the image file, perhaps?

Okay, since you can get the source URL for the image you should be able to use the Get a URL action to retrieve it.

image

Strangely the action doesn't have the ability to save to a file, but you can use the clipboard and then dump that to a file if needed.

If for some reason this doesn't work then there's always curl and wget.

This worked perfectly, thank you! No drama to save it to a named clipboard. I had a feeling that my approach was way more complex (and brittle) than it needed to be!

@peternlewis – please consider adding “Save to File” to the GetURL action.

1 Like

Noted.

3 Likes

This is kinda related ... so I will bolt this one on too! My reason for saving the image in question is to record a snapshot of something, and at the same time I'd like to grab a snapshot of the related page's HTML. The Get URL macros has an option to save the web page HTML as text, and this is handy, but is there a way to get a web archive or 'Save as Web Page Complete' that does not involve opening a browser window, but instead to do it silently like with this Get URL?

I did try the web archive, but it did not save an archive as such (it was literally the source showing in the web page, as if the HTML were rendered inside

...
)

Not exactly – it can save the code it is able to obtain as text – but it's not an actual browser that can load dynamic content created by scripts when a web page renders.

There's a Unix executable webarchiver – but I haven't tested it for some time.

Also available from MacPorts and Homebrew

And there's an AppleScript solution – if it still works. Again – I haven't tested this in some time.

Done for the next version. Boy did this open a can of worms, but I packed them all back in the can.

3 Likes

Sorry 'bout dem worms. I'm sure they will be tasty worms, though.