Get image from URL

If you have an address for an image ( such as- l.yimg.com/a/i/us/we/52/26.gif ), is there a way to load the image itself into a variable that I could then insert into a rich text file? I’m stumped!

See, for example, the script at Scripts > Download > Images of Page. It doesn’t do exactly what you want, but it explains the method.

Thanks korm, but there’s no file named ‘Images of Page’ in the Scripts>Download folder on my computer. There are 4 or 5 other scripts that deal with downloading images using links, but nothing that helps me actually get the image itself. Maybe this can’t be done at present but I’ll keep looking! :astonished:

The script’s file name is “Add Images” – which is called “Get Images of Page” in the menu.

But, you might need something different – like using Automator (this example is still valid in Mavericks).

It’s easy enough to use ‘curl -o ‘ to create an image file or I can use a pipe to redirect the output to the clipboard. Unfortunately, what I get on the clipboard is just the image data. I would have thought there would be some reasonable way to grab an image off of a website once you’ve gotten the image’s URL, but apparently there isn’t. I’m not giving up though!

Here’s the generic non-DEVONthink way with curl. (Random image snarfed from Google images.) The image has to be sent to a file of the same type as the source. You could import, index, or tell DEVONthink to get the file from, for example, a temp location:


set theURL to quoted form of ("http://eofdreams.com/data_images/dreams/image/image-01.jpg")
set theDestination to quoted form of POSIX path of ((path to desktop as text) & "myImage.jpg")
set theScript to "curl " & theURL & " -o " & theDestination

do shell script theScript

Right, I can save the image to a file with “curl -o “ & targetPath & “ “ & picURL or I can save it to the clipboard with “curl “ picURL & “ | pbcopy”. But, in the first case, I end up with an image saved to disk and in the second case I get the data from the gif image on the clipboard. Neither of these helps me to insert an image into my rtf in DTPO. At least not in a way that I’ve figured out yet. :question:

With some very helpful advice from Criss, this example will create an RTFD with some text and an image grabbed from an image address. The content addition and keystroking could be refined to get you where you want to be.

tell application id "DNtp"
	
	set theURL to quoted form of ("http://eofdreams.com/data_images/dreams/image/image-01.jpg")
	set theDestination to POSIX path of ((path to desktop as text) & "myImage.jpg")
	set theDestination_q to quoted form of theDestination
	set theScript to "curl " & theURL & " -o " & theDestination_q
	
	do shell script theScript
	
	set the clipboard to (read theDestination as TIFF picture)
	
	set theRecord to create record with {name:"My File", content:"blah blah", type:rtfd} in display group selector
	set thewindow to open window for record theRecord
	activate
	tell application "System Events"
		keystroke "v" using {command down}
		delay 0.5
	end tell
	close thewindow with saving
		
end tell


Thanks for taking the time korm! Believe it or not, I figured out how to do it that way ( with some help looking through Macscripters! ) but it seemed very clumsy to me. I just figured there would be a more elegant way to accomplish grabbing the picture. Maybe that would be a good feature request!?

korm, I think I’ve found a better way!! Instead of creating a file and then reading it into the clipboard, this seems to work… 8)

***EDIT!! Unfortunately the code below only reads in the top portion of the image. Back to the drawing board…

set theCurlCommand to ("curl " & picURL)
set the clipboard to (do shell script theCurlCommand as TIFF picture)

Good approach. In some cases (URLs with internal spaces, e.g.) you might want

set picURL to quoted form of "[the url]"
set theCurlCommand to ("curl " & picURL)
set the clipboard to (do shell script theCurlCommand as TIFF picture)

Unfortunately, I spoke too soon. Feeding the clipboard straight from the ‘curl’ gets only the top portion of the image. I’m back to creating a file and reading the image in from there. :frowning: