Downloading an image as the desktop image, strange error message

Hi, I hope I’m not breaking etiquette, I’m pretty new here and I couldn’t find a previous post that directly helped out. What I’m trying to do is write a script that will on a regular or daily basis download an image from http://antwrp.gsfc.nasa.gov/apod/ and set it as the desktop image. I could do it easily in automator, but I’m having a hard time in applescript. I’m trying to write this for my dad who is a real astronomy fan but doesn’t have automator on his computer. Here’s what I have so far:


--if Finder activate then

set the destination_file to ((path to desktop as string) & "space.jpg")

tell application "Safari"
	activate
	--open location target_URL
	make new document at end of documents
	set URL of document 1 to "http://antwrp.gsfc.nasa.gov/apod/"
	delay 2
	set done to false
	repeat with i from 1 to 20
		tell application "Safari"
			if (do JavaScript "document.readyState" in document 1) is "complete" then
				set done to true
			else
				delay 1
			end if
		end tell
	end repeat
	
	if done is true then
		set thisImageSRC to do JavaScript the "documents.images[0].src" in document 1
	end if
	
end tell

tell application "URL Access Scripting"
	download thisImageSRC to destination_file replacing yes
end tell

tell application "Finder"
	set desktop picture to file "space.jpg" of desktop
end tell

--end if

The inactivated sections at the top and bottom are what I tried for the timing of running the script, but that probably wouldn’t work and I’ve never tested it; I was planning on just listing the script as a startup item to make sure that the timing works. It seems to run fine, opening the proper website, letting it load, but then the same error comes up. When it tries to download the image, URL Access Scripting brings up an error type-31050. Does anyone know what this means? I’ve tried looking this up to no success and I’ve tried looking up other methods of directly downloading an image with no real success. As far as I can tell, everything but the download line works just fine, but I found that line in the sample that came with applescript for downloading the weather map. Am I doing something wrong? My room mate helped and he has a lot of experience coding, but not with applescript. Is there some alternative to using URL Access Scripting, or is a previous line causing a conflict that I don’t see?

Craig

Hi,

Your script up to this line:

set thisImageSRC to do JavaScript the “documents.images[0].src” in document 1

returns an empty string “”. I don’t know javascript, so don’t know why. If I get the url of the jpeg and use it to download, then it works:


set the destination_file to ((path to desktop as string) & "space.jpg") as file specification
set thisImageSRC to "http://antwrp.gsfc.nasa.gov/apod/image/0601/saturnetempete250106_2.jpg"
tell application "URL Access Scripting"
	download thisImageSRC to destination_file replacing yes
end tell

Note that I coerced the string (path to the file to download to) to file specification which is a file that may or may not exist. Just the string path doesn’t work.

gl,

I don’t know JavaScript either, but going on the example of the earlier call, I tried changing ‘documents’ to ‘document’ and obtained an intelligent result:

I too use a script to connect daily to NASA’s Astronomy Picture of the Day, though I don’t use it to save the image to disk. Last year, there was an extended period when the default page at the directory URL didn’t change from day to day. I don’t know if that was due to some oversight at NASA or my ISP’s Web cache being lazy. I got round this by writing a handler to calculate the exact URL for the current day’s page. Here it is, if you’re interested. I use an old-fashioned method for the month number as my Internet machine runs Jaguar.

on todaysApodUrl()
	-- Get the date at the APOD site, allowing extra for
	-- the picture not changing at exactly midnight.
	set APODdate to (current date) - 5.5 * hours -- I'm in the UK.
	set {year:y, day:d} to APODdate
	
	copy APODdate to b
	set b's month to January
	set m to (b - 2500000 - APODdate) div -2500000
	
	return "http://antwrp.gsfc.nasa.gov/apod/ap" & ¬
		text 3 thru 8 of ((y * 10000 + m * 100 + d) as string) & ".html"
end todaysApodUrl

tell application "Safari"
	activate
	-- open location my todaysApodUrl()
	make new document at end of documents
	set URL of document 1 to my todaysApodUrl()
	-- etc.
end tell

Thanks for the help, changing the line documents.images[0].src to document.images[0].src seems to have fixed whatever that error was. Now to hope that it works on my dad’s computer. Thanks again!

Craig

Good catch Nigel! You have good eyes.

One other thing is that sometimes the file doesn’t download and throws an error. You might want to use a repeat loop to try the download several times. I put the download that tries up to 3 times:

set the destination_file to ((path to desktop as string) & “space.jpg”) as file specification
tell application “Safari”
activate
–open location target_URL
make new document at end of documents
set URL of document 1 to “http://antwrp.gsfc.nasa.gov/apod/
delay 2
set done to false
repeat with i from 1 to 20
tell application “Safari”
if (do JavaScript “document.readyState” in document 1) is “complete” then
set done to true
else
delay 1
end if
end tell
end repeat
if done is true then
set thisImageSRC to (do JavaScript the “document.images[0].src” in document 1)
end if
end tell
set no_err to false
set the_count to 0
repeat until no_err
try
tell application “URL Access Scripting”
set new_pic to (download thisImageSRC to destination_file replacing yes)
end tell
set no_err to true
on error
beep 1
set the_count to the_count + 1
if the_count is 3 then error “Could not download the picture.”
end try
end repeat
tell application “Finder”
set desktop picture to (new_pic as alias)
end tell

gl,