Load image from URL

Given the URL of an image file, is it possible to load that image and paste it into a Numbers cell?


I don't see why not. The main problem I see is that web browsers return web pages, not images in the clipboard, which means waiting for the page to load, then finding and copying the image into a clipboard is the step that needs to be considered. Once it's in the clipboard, you would just paste it into Numbers using the KM paste action, I suppose. Is your cursor in Numbers going to be in the right location when you start the macro? In the simplest case, this task could be performed with four simple KM actions. 1. Get URL. 2. Copy image from browser into clipboard. 3. Activate Numbers. 4. Paste clipboard into Numbers.

All these steps are trivial, except perhaps for taking #2. There are different ways to perform #2. I would imagine that doing it with AppleScript is the most reliable. I'm not very good with AppleScript so I'll let someone else chime in first.

It may possibly depend upon the website you are using. Some website return web pages in stages, and if they do that, we may need some code to wait for the web page to fully load.

Thanks for the answer.

I have everything working except the first bit. I'm able to grab the other information I need from the page containing the image then pass all that on to an AppleScript that opens my document, adds a new row to the end of a table and sets the cell values appropriately. If I have manually copied the image into the clipboard, it will also paste the image correctly (still in AppleScript).

In other words, the page is already loaded so that's not a problem. I'm struggling with the "find image" step. The pages I'm working with have a photo that may vary in size and position and is different for each instance of the page.

I think there's a way I can use AS to download the image then read it into the clipboard but I was hoping to avoid messing with the file system.

You need someone who knows AS to help with that part. It would take me an hour or more to figure it out.

That phrase sightly contradicts your opening post which says,

This is a slight contradiction because now you are saying it's a general web page containing one or more images, not a page that consists of a single URL that points to a single image. If you want someone to write an AS for you, you're likely going to have to show the URL, so why not show it now?

I see where I've not been as clear as I should have been. I'm on a company directory page (which means I can't publicly share details). This page has one or more photos. Using JavaScript, I can locate the primary photo's href and put the URL into a variable. That's why I said "Given the URL...".

Sorry for the confusion.

And to be clear, I'm not expecting AS help in this forum. I was just thinking there might be a way through KM itself.


Privacy is understandable and appreciated. Some browsers (you didn't say which browser you are using) may be able to save an image from a web page directly to a file using a menu "Save" command if the image is the only thing on the page. But you said you don't want to save as a file, but you didn't say why. May I ask what's the reason for this restriction? If it's just "efficiency," then my suggestion is don't worry about efficiency until after you get a working macro. Efficiency should always be optional, but if it's mandatory, then you can't eliminate AppleScript which is always the most efficient solution.

Efficiency would be one reason, and the need to clean up afterwards. But as you say, getting something working is always top priority. Elegance can come later. :grinning_face_with_smiling_eyes:

(Though I don't have enough digits to count the number of times I've had to make something "just work", intending to do it the "right way" later, only to never getting around to doing so. Lol)

If there is no right way, then there's no loss doing it inefficiently. If there is a right way, and you don't waste time implementing it, then you have "efficiently" saved yourself time.

It seems to me you've done the hard parts.

Using JavaScript, I can locate the primary photo's href and put the URL into a variable.

If I have manually copied the image into the clipboard, it will also paste the image correctly (still in AppleScript).

Unless I'm misreading things, all you need is the Get a URL action. It will happily download an image from the URL you extracted via JavaScript and put it on the clipboard, where your AppleScript can pop it into a Numbers cell. Here's a minimal demo of how the action works:

Get a URL demo.kmmacros (1.6 KB)

1 Like

Excellent! I had no idea this action (with the ability to save an image from a URL into a clipboard) existed. KM always surprises me with its hidden features.

Works like a charm!

1 Like

Yeah, as a relatively new user to KM, the number of features is still a bit overwhelming. Much to learn - but fun doing so.