National Geographic Picture of the Day Script: Feedback please!

Hi Everyone,

I used to use automator to download the National Geographic’s Picture of the Day, however about a week ago the automator would error out because of error -999. I decided to take the plunge and dive into applescript. I’ve put together the following script and would appreciate any feedback you have to make the script runner faster (it runs fine now, but would like to learn if there is any way to make the script better) and/or code in a cleaner matter.

Thank you!

Marc


tell application "System Events" to set foo to (exists process "Safari")
if not foo then -- Safari is not running
	tell application "Safari" to activate
else
	tell application "Safari"
		set miniaturized of window 1 to true --if safari is already running, this minimizes the current window thats open
	end tell
end if
end

tell application "Safari"
	
	make new document
	set URL of document 1 to "http://photography.nationalgeographic.com/photography/photo-of-the-day/"
	
	delay 60 -- this gives the page time to load
	
	set myText to source of front document
	close window 1
	try
		set my text item delimiters to "download_link" --finds the download link text and trims it
		set myText to text item 2 of myText
		
	on error errTxt number errNum
		display dialog "The National Geographic Picture of the Day is not available as a download today."
		tell me to quit
	end try
	
	try
		set my text item delimiters to ">Download Wallpaper (1600 x 1200 pixels)</a></div>" --finds the download link text and trims it
		set myText to text item 1 of myText
	on error errTxt number errNum
		display dialog "The National Geographic Picture of the Day is not available as a download today."
		tell me to quit
	end try
	
	
	try
		set my text item delimiters to "\"><a href=\"" --finds the download link text and trims it
		set myText to text item 2 of myText
	on error errTxt number errNum
		display dialog "The National Geographic Picture of the Day is not available as a download today."
		tell me to quit
	end try
	
	try
		set my text item delimiters to "\"" --finds the download link text and trims it
		set myText to text item 1 of myText
	on error errTxt number errNum
		display dialog "The National Geographic Picture of the Day is not available as a download today."
		tell me to quit
	end try
	
	set my text item delimiters to {""}
	
end tell

myText --this now is the download link and is passed to the download app
set theDate to (do shell script "date +%m/%d/%Y")
set theLocation to (path to desktop) as text
set theDownload to theLocation & theDate & " NGPOD.jpg"
tell application "URL Access Scripting"
	download myText to file theDownload
	
end tell

tell application "Finder" --the file has been downloaded and will now be moved to correct folder
	
	move theDownload to folder "Picture of the Day" of folder "Pictures" of folder "marc" of folder "Users" of startup disk
	set desktop picture to {"Macintosh HD:Users:marc:Pictures:Picture of the Day:" & theDate & " NGPOD.jpg"} as alias
	display dialog "The National Geographic Picture of the Day has been downloaded."
end tell

Model: MacBookPro Fall 2009/ 2.8 GHz Core 2 Duo / 8 GB Ram
AppleScript: 2.3
Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)

Hi,

this is a version without Safari. It uses cURL to download the source and the image.
It’s not recommended to use slashes in file names, because in the underlying file system a slash is a path delimiter.
The script uses underscore characters instead


set myText to do shell script "/usr/bin/curl 'http://photography.nationalgeographic.com/photography/photo-of-the-day/'"
set TID to text item delimiters
set myText to trimText(myText, "download_link", 2)
set myText to trimText(myText, ">Download Wallpaper (1600 x 1200 pixels)</a></div>", 1)
set myText to trimText(myText, "\"><a href=\"", 2)
set myText to trimText(myText, quote, 1)
set text item delimiters to TID

set destinationFolder to POSIX path of (path to pictures folder) & "Picture of the Day/"
set destinationFile to (do shell script "/bin/date +%m_%d_%Y") & "_NGPOD.jpg"
do shell script "/usr/bin/curl -o " & quoted form of (destinationFolder & destinationFile) & space & quoted form of myText
display dialog "The National Geographic Picture of the Day has been downloaded."


on trimText(theText, delim, itemNumber)
	try
		set text item delimiters to delim --finds the download link text and trims it
		set theText to text item itemNumber of theText
		return theText
	on error errTxt number errNum
		display dialog "The National Geographic Picture of the Day is not available as a download today." cancel button "Quit" buttons {"Quit"} default button 1
	end try
end trimText

The script can still be improved by parsing the html source with sed or awk.
But unfortunately right now the 1600x1200 wallpaper is not provided

PS: I found another site where “Download Wallpaper” is available. This works on http://photography.nationalgeographic.com/photography/photo-of-the-day/australian-sundew/


set NGPODURL to "http://photography.nationalgeographic.com/photography/photo-of-the-day/"
try
	set downloadURL to do shell script "/usr/bin/curl " & NGPODURL & " | awk '/Download Wallpaper/' | cut -d '\"' -f 4"
	if downloadURL is "" then error
on error errTxt number errNum
	display dialog "The National Geographic Picture of the Day is not available as a download today." cancel button "Quit" buttons {"Quit"} default button 1
end try

set destinationFolder to POSIX path of (path to pictures folder) & "Picture of the Day/"
set destinationFile to (do shell script "/bin/date +%m_%d_%Y") & "_NGPOD.jpg"
do shell script "/usr/bin/curl -o " & quoted form of (destinationFolder & destinationFile) & space & quoted form of myText
display dialog "The National Geographic Picture of the Day has been downloaded."

Hi Stefan-

Thanks for your quick reply. I tried the second suggestion of yours, but it runs into an error because myText is not defined anywhere.

Thanks again for your help!

Marc

sorry, my fault, replace all occurrences of myText with downloadURL