How to create a JPEG record with the downloaded data?

I have a webarchive, I’m getting the links to the linked images:

set this_source to the source of this_webarchive
set these_links to get links of this_source base URL this_URL

After I have filtered the links to find the one I’m interested in (a JPG image), I download it:

set this_picture to (download URL this_link agent my_user_agent)

I’m getting the picture data in this format:

«data ****FFD8FFE0[…rest of hex data of JPEG picture…]»

However when I’m trying to save the picture as a new picture-typed record, it creates a picture-typed record that’s empty, displays a 0 byte count and does not even have a physical path to an underlying file in the Finder.

create record with {name:new_name, URL:this_link, type:picture, data:this_picture} in current group

I’m not using the recommended way to download pictures for two reasons:

  • even with the automatic option the URL gets added to the download window but does not start automatically
  • more importantly the command does not give control on the saving location of the downloaded file (it should be current group in my case) or on its name (I need to rename it to the same as my webarchive)

add download this_link referrer this_URL with automatic

Can any of you help?

Try to use the correct syntax, which is “set theResult to create record …” rather than “create record …” and “theResult” should be the new record you what. You could alternatively “set theResult to import …” from the location of the downloaded image. For example, have your script download to ~/Downloads and use the import command to grab the downloaded file. You don’t need to strip the data make a new file with the data.

Thank you korm. When I said in my post “I’m not using the recommended way to download pictures” I did not mean that it’s impossible to get the control from create record — the dictionary makes it clear that it returns a record that can be modified and I use that a lot in other scripts — I meant that add download this_link does not return a record, only a boolean, and it does not provide the choice of the location.

Anyway, I still find it strange that the data I get is not valid for the create record function, is it a type cast issue? I haven’t found yet an example of code using the data property of the record object type.

If there is no way to get around it, then I guess there is no choice but to download the picture on the local disk and import it. I generally prefer to avoid storing anything outside the DEVONthink database in my scripts, even temporarily.

So I did as you suggested, I used curl to get the image:

set the_script to "curl --user-agent " & quoted form of my_user_agent & " --head --silent --write-out '%{http_code}' --output /dev/null " & quoted form of this_link
set status_code to do shell script the_script as text
if (status_code) is "200" then
set temp_file to POSIX path of ((path to downloads folder as text) & "Temp_image.jpg")
set the_script to "curl --referer " & quoted form of this_URL & " --user-agent " & quoted form of my_user_agent & " --output " & quoted form of temp_file & " " & quoted form of this_link
do shell script the_script
import temp_file type picture name new_name to current group
set the_script to "rm " & quoted form of temp_file
do shell script the_script
end if

Here’s a simpler method…

tell application id "DNtp"
	repeat with sel in (selection as list)
		if type of sel = webarchive then
			set {src, fURL} to the {source, URL} of sel
			set imageLinks to get links of src base URL fURL type "JPG"
			repeat with thislink in imageLinks
				if thislink begins with "http" then -- Note: Probably won't work with Wikipedia or some others without massaging.
					set newRecord to create record with {name:(do shell script "basename " & (quoted form of thislink)), type:picture} in incoming group
					set picData to download URL thislink
					set data of newRecord to picData
				end if
			end repeat
		end if
	end repeat
end tell

Hello Jim,
So that’s the secret: add the data in a second step after having created the record.
Well your code is much simpler indeed than my Curl workaround.
Thanks a lot!


curl is very cool stuff, but it’s a little overkill for the simple task at hand.

You’re welcome. :smiley: