It now employs a 4-digit year prefix and preserves the date of APOD pictures.
The script uses Safari to scrape the APOD url from the currently active page, so it works well in conjunction with my Previous APOD script and a script-runner such as FastScripts or Keyboard Maestro with suitable keyboard shortcuts.
------------------------------------------------------------------------------------------------
# Created: 2012-09-05 : 22:49
# Modified: 2012-09-07 : 00:20
# Application: Safari | awk | curl | date | echo | mkdir | perl | sed
# Purpose: Download the APOD image in the currently displayed Safari web page.
# Dependencies: None: Only stock OSX components used.
------------------------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------------------------
on fileExists(_str)
if _str starts with "/" then
set _str to POSIX file _str
else if _str starts with "~/" then
quoted form of _str
set _str to do shell script "sed -E 's#(~/|$)(.*)#\\1\"
\\2\"#g' <<< "
& result set _str to do shell script "echo " & _str
set _str to POSIX file _str
end if
try
_str as alias
return true
on error
return false
end try
end fileExists
------------------------------------------------------------------------------------------------
on getUrlFileName(_url)
local urlFileName
set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
set urlFileName to last text item of _url
set AppleScript's text item delimiters to oldTIDS
return urlFileName
end getUrlFileName
------------------------------------------------------------------------------------------------
on safariLinks(regexStr, tagName, tagType)
set js to "function in_array (array, item) {
for (var i=0; i < array.length; i++) {
if ( array[i] == item ) {
return true;}}
return false;}
var a_tags = document.getElementsByTagName('" & tagName & "');
var href_array = new Array();
var reg = new RegExp(/" & regexStr & "/i);
for (var i=0; i < a_tags.length; i++) {
var href = "" style="font-family: Verdana; color: rgb(0, 0, 0); "> & tagType & ";
if ( reg.test(href)) {
if ( !in_array(href_array, href)) {
href_array.push(href);}}}
href_array;"
try
tell application "Safari" to set linkList to do _javascript_ js in document 1
on error
set linkList to {}
end try
return linkList
end safariLinks
------------------------------------------------------------------------------------------------
on safariSrc()
tell application "Safari" to return (quoted form of (get source of front document))
end safariSrc
------------------------------------------------------------------------------------------------
on safariUrl()
tell application "Safari"
if front document exists then
set _url to URL of front document
end if
try
return _url
on error
error "Failure in safariUrl() Handler."
end try
end tell
end safariUrl
------------------------------------------------------------------------------------------------
--» MAIN
------------------------------------------------------------------------------------------------
try
set pageURL to safariUrl()
set apodSrcQtd to safariSrc()
set apodImgURL to safariLinks(regexStr, "*", "href")
if apodImgURL = {} then
beep
else if apodImgURL ≠ {} then
set apodPicFldrHFS to "" & (path to pictures folder) & "APOD_Images:"
set apodPicFldr to POSIX path of apodPicFldrHFS
if fileExists(apodPicFldr) = false then
do shell script "mkdir " & apodPicFldr
end if
set urlFileName to getUrlFileName(pageURL)
set apodCaption to do shell script "echo "
& apodSrcQtd & " | awk -F' *<
\\/?b> *' '/^<b>.+<
\\/b>.*<br>/ { print $2 }' |
sed -E -e 's/'\\''/’/' -e 's/:/»/'"
set apodDate to do shell script "perl -wlne 'if (m/(?i)^ap(
\\d{6})
\\.html/) {print \"$1\"}' <<< "
& quoted form of urlFileName
if apodDate ≠ "" then
set apodDate to do shell script "date -jf \"%y%m%d\" \"" & apodDate & "\" \"+%Y%m%d\""
else if apodDate = "" then
set apodDate to do shell script "perl -wlne 'if (m/(?i)(^[
\\t]*\\d{4} [a-z]+
\\d{1,2})/) {print \"$1\"}' <<< "
& apodSrcQtd if apodDate ≠ "" then
set apodDate to do shell script "date -jf \"%Y %B %e\" \"" & apodDate & "\" \"+%Y%m%d\""
else
error "APOD date not found!"
end if
end if
set apodImgURL to item 1 of apodImgURL
set apodImgName to do shell script "echo " & apodDate & " » " & apodCaption & " » `sed -E 's|.+/(.+)$|\\1|' <<< " & quoted form of apodImgURL & "`"
set apodImgDldPath to apodPicFldr & apodImgName
if fileExists(apodImgDldPath) = false then
do shell script "curl -L --user-agent 'Opera/9.70 (Linux ppc64 ; U; en) Presto/2.2.1' --url " & apodImgURL & " -o " & "\"" & apodImgDldPath & "\""
set apodFileAlias to (alias POSIX file (do shell script "echo " & "\"" & apodImgDldPath & "\""))
tell application "Finder"
reveal apodFileAlias
# activate
end tell
else
error "APOD file already exists!"
end if
end if
end if
on error e number n
set e to "Error: " & e & return & return & "Error Number: " & n
beep
tell me to set dDlg to display dialog e buttons {"Cancel", "Copy", "OK"} default button "OK"
if button returned of dDlg = "Copy" then set the clipboard to e
end try
------------------------------------------------------------------------------------------------