(*
new AutoAPOD - 10/1/04 - from Johnny AppleScript <
email@hidden> 11/26/04 - add local to variables 1/23/05 - exit to AutoApod Preferences on error 6/2/05 - removed "aypod image already downloaded" anouncement 4/20/2010 - Remove AutoAPOD link
*)
on run
main()
end run
on main()
try -- exit gracefully if script won't run
(*
local a, APOD, apodEXT, apodName, archiveEXT, b, curAPOD, d
local dateTAG, destinationFolder, dfExists, dn, downloadOption, errMsg
local fileName, filePath, flag, folderName, getImage, imageEXT, imageURL, leadTAG, leadTAG1
local leadTAG2, m, mn, pFlag, rootURL, test, ThePicture, trailingTAG, trailingTAG1
local trailingTAG2, y, yn
*)
set test to false -- false disables dialogs
set {y, m, d} to {year, month, day} of (current date) --> date "Sunday, September 26, 2004 7:45:07 PM"
set d to d as text
if number of characters in d < 2 then set d to "0" & d as string -- good catch, Bob; I should have predicted this
set flag to quoted form of (y & " " & m & " " & d as string) --> "2004 September 26"
set yn to y as text --> "2004"
set yn to characters -2 thru -1 in yn as string --> "04"
set mn to (m as number) as text --> "9"
if number of characters in mn < 2 then set mn to "0" & mn --> "09"
set dn to d as text --> ex.: "6"
if number of characters in dn < 2 then set dn to "0" & dn --> ex.: "06"
set pFlag to yn & mn --> "0409"
set dateTAG to pFlag & dn --> "040926"
set archiveEXT to "archivepix.html"
try
set APOD to do shell script "curl " & rootURL & archiveEXT & " | grep " & flag
on error
-- if test then
display dialog "1, APOD Image Retriever: " & return & rootURL & return & "is currently unavailable. Please try again later." buttons {"Poopy"} default button 1 giving up after 5 with icon 2
return
end try
set leadTAG to "<a href="">
set trailingTAG to "\">"
set leadTAG1 to (offset of leadTAG in APOD)
set trailingTAG1 to (offset of trailingTAG in APOD)
set apodEXT to characters (leadTAG1 + 9) thru (trailingTAG1 - 1) in APOD as string
-- set apodName to characters (trailingTAG1 + 2) thru -9 in APOD as string
try
set curAPOD to do shell script "curl " & rootURL & apodEXT & " | grep href | grep jpg | grep " & pFlag -- Doh! What if it's a GIF?
on error errMsg
try
set curAPOD to do shell script "curl " & rootURL & apodEXT & " | grep href | grep gif | grep " & pFlag
-- this method is bound to fail. We need a better analysis; can you think of one?
on error
if test then display dialog "2, APOD Image Retriever: locate daily image failed: " & errMsg with icon 0 -- giving up after 15
(* -- AutoAPOD not available for Snow Leopard
tell application "System Events"
tell application "System Preferences" to activate
tell process "System Preferences"
click menu item "AutoAPOD" of menu "View" of menu bar item "View" of menu bar 1
tell radio button 1 of tab group 1 of window 1 to if value is 0 then click
click button 1 of tab group 1 of window 1 -- "Fetch Now"
delay 1
click button 2 of tab group 1 of window 1 -- "Set As Deskop"
delay 1
end tell
return
end tell
*)
end try
end try
set leadTAG2 to (offset of leadTAG in curAPOD) -- using a different flag length than above
set trailingTAG2 to (offset of trailingTAG in curAPOD) -- using a different flag length than above
set imageEXT to characters (leadTAG2 + 9) thru (trailingTAG2 - 1) in curAPOD as string
set filename to reverse of every character of imageEXT as string
set filename to reverse of every character of (characters 1 thru ((offset of "/" in filename) - 1) in filename as string) as string
set filename to dateTAG & "-" & filename -- let's add today's date string to the prefix for our own archives
set imageURL to rootURL & imageEXT
set folderName to "APOD ƒ"
set destinationFolder to ((path to pictures folder) & folderName & ":" as string) -- we'll need to use this path more than once, so let's give it a unique variable name
-- 040928 added the 'APOD ƒ' folder to the destination path string; be sure this folder actually exists, or the script will fail
tell application "Finder" to set dfExists to (exists folder destinationFolder)
if dfExists is false then tell application "Finder" to make new folder at (path to pictures folder) with properties {name:folderName}
set filePath to POSIX path of destinationFolder -- here's the first use; we need a UNIX-style path string; i.e., /Users/bob/Pictures/
set ThePicture to ((destinationFolder & filename) as string) -- here's the second use; we need and HFS-style path string; i.e., :Users:bob:Pictures:file_name
tell application "Finder" to set downloadOption to (exists file ThePicture)
if downloadOption is false then
set getImage to "curl " & imageURL & " -o " & "\"" & filePath & filename & "\""
-- set {a, b} to {"Thanks", "Download"}
-- set dlOption to button returned of (display dialog "Today's image is " & return & "'" & apodName & "'" & return & return & imageURL buttons {a, b} default button 2 with icon 1)
-- if dlOption is b then
try
do shell script getImage
-- delay 10 -- need an appropriate delay here - - if using curl; not required with URL Access Scripting - - before setting a picture to desktop background that may still be downloading; might be best to either run a different script, or run an idle handler to allow enough safe time for d/l of very large images or if there is an image d/l failure. Don't forget about using try blocks.
-- tell application "URL Access Scripting" to download imageURL to ThePicture -- unlike curl, this method will stop the rest of the script until the d/l is completed; no additional delay command is required.
on error errMsg
if test then display dialog "3, APOD Image Retriever: download failed: " & errMsg with icon 0 -- giving up after 15
end try
-- set desktopSet to button returned of (display dialog "Set image as desktop?" buttons {"No", "Yes"} default button 1 with icon 1 giving up after 10)
try
set ThePicture to ThePicture as alias -- need to make a file path string into an object Finder can recognize
-- if desktopSet is "Yes" then
tell application "Finder" to set desktop picture to ThePicture
on error errMsg
if test then display dialog "5, " & return & errMsg with icon 0 -- giving up after 15
end try
-- end if
else
ignoring application responses
# say "autopod im age already downloaded"
end ignoring
end if
on error errMsg
end try
return
end main
Robert Poland - Fort Collins, CO