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)