Get links from a url via script

I’m trying to get the astronomy image of the day from NASA, but I’m doing something wrong and I don’t know what.

My code is:

on getAtronomyImageOfDay()
	set myNews to {}
	tell application id "DNtp"
		set apodHtml to download URL apodUrl
		set apodLinks to get links of apodHtml
		set numLinks to count of apodLinks
		if (numLinks = 0) then
			display alert numLinks
			display alert numLinks
		end if
		return ""
	end tell
end getAtronomyImageOfDay

(ignore the nonsense starting with “if(numLinks)…”, it is for debugging)

My code fails in the “get links of” with a big popup showing some kind of cannot convert "data ***3C216… " that seems base64 converted string.

If I omit the “download URL” statement, and use “get links of”, numLinks is set to 0.

Just replace “download URL” with “download markup from”. The following script might be useful too, it’s used by a daily reminder for a bookmark ( in one of my databases.

property pLocation : "/APOD"

on performReminder(theBookmark)
	tell application id "DNtp"
			set theDatabase to database of theBookmark
			with timeout of 30 seconds
				set theURL to URL of theBookmark
				set this_page to download markup from theURL
				set these_images to get embedded images of this_page base URL theURL
				repeat with this_image in these_images
					if not (exists record with URL this_image) then
						set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
						set this_name to the last text item of this_image
						set AppleScript's text item delimiters to od
						if not (exists record at pLocation in theDatabase) then
							set this_group to create location pLocation in theDatabase
							set thumbnail of this_group to ""
							set this_group to get record at pLocation in theDatabase
						end if
						set this_record to create record with {name:this_name, type:picture, URL:this_image, unread:true} in this_group
						set image of this_record to this_image
					end if
				end repeat
			end timeout
		end try
	end tell
end performReminder

Wow! So easy when you know it!

I must be dumb over any possibilities, because still cannot put the image into a paragraph.

My current code to get the image is this:

on getAtronomyImageOfDay()
	tell application id "DNtp"
		set apodHtml to download markup from apodUrl
		set images to get embedded images of apodHtml base URL apodUrl
		set numImages to count of images
		if (numImages = 0) then
			return ""
		end if
		--display alert numImages
		set imageUrl to item 1 of images
		set imageContent to download markup from imageUrl
		return imageContent
	end tell
end getAtronomyImageOfDay

I assume this return an image. Then, in the part of the code that inserts the image into the document, I have this:

set apod to my getAtronomyImageOfDay()
if (apod is not "") then
	set the last character to the last character & return & "APOD:" & return
	bold last paragraph
	unbold last character
	set the last character to the last character & apod & return
end if

What I get is a big rtf with the binary content of the image. I tried some things found in internet, but seems related to Pages, because does nothing in the RTF document or the script fails at compile time.

I assume this return an image.

You should see this in the Replies section of Script Editor. Here’s a stripped down version of Criss’ code noting the replies…

Didn’t knew that from script editor!!

Ok, tried with all my effort. Or I get printed the image url or nothing. This is my code and the “Replies” of the script editor. Same as Criss’ code but blank result.

Just discovered that part of the script generates an image in my inbox…

Correct. Criss’ script (and my subset) is not creating an RTF file or adding an image to one. It is downloading and creating a new image file from the image on the remote server.

PS: Scripting RTF(D) files is not a simple thing. There have to be specific implementations, like DEVONthink’s bold and unbold, etc. or you have to try and mess with the code under-the-hood (not for the faint of heart).

Ok, then if there is no specific script commands, I will deal with that. I will generate the same file I want by hand and then inspect the internals and try to make it work.

At the end, if I do it, I will post here and add it to our journal script that is shared in other part of this forum.