Does anyone remember the post that gets the daily astronomy screensaver through applescript soap calls? I searched for it, but there are too many posts about xml-rpc soap and not just the ones about astronomy. It was about ten years ago.
Thanks,
Model: MacBook Pro
AppleScript: 2.2.3
Browser: Safari 536.26.17
Operating System: Mac OS X (10.8)
I’m not sure that the saved posts go back that far, kel1, because the board has been changed (and downsized) several times since then. I have no control of that – Ray Barber owns the site and its CoLo servers. Further, the post you’re looking for may have been in ScriptBuilders and Ray never restarted that after the last board revision. I used to administer ScriptBuilders about 10 years ago, but don’t recall that script.
Come to think of it, maybe the poster wanted to download the picture of the day and ended up with javascript. I was thinking that you can’t download pictures with Soap. I searched for javascript and astronom, but couldn’t find the post.
set the_url to "http://apod.nasa.gov/apod/astropix.html"
tell application "Safari"
activate
open location the_url
-- wait until page loaded
if my page_loaded(20) is false then error number -128
-- get number of links
set num_links to (do JavaScript "document.links.length" in document 1)
-- get the index of the last link
set last_link_index to num_links - 1
-- get every link url
set this_url to ""
set jpeg_url_list to {}
repeat with i from 0 to last_link_index
-- get link url
set this_url to do JavaScript "document.links[" & i & "].href" in document 1
-- add the url to the list if its a jpeg
if this_url ends with ".jpg" then
set end of jpeg_url_list to this_url
end if
end repeat
end tell
jpeg_url_list
on page_loaded(timeout_value)
delay 2
repeat with i from 1 to the timeout_value
tell application "Safari"
if (do JavaScript "document.readyState" in document 1) is "complete" then
return true
else if i is the timeout_value then
return false
else
delay 1
end if
end tell
end repeat
return false
end page_loaded
This should give a list of jpeg urls on the page. The only problem is that they changed the jpeg from yesterday to a flash movie. Just when you think you have all the answers, they change the questions. I’ll test it at google pictures or something.
I signed up for the pictures to be sent from nasa. Might try processing the pictures through a rule in Mail.
Thanks,
BTW, Adam there’s still a lot of useful posts there. Thanks for saving them, to you and Ray.
This, for interest, fetches today’s APOD page (ignoring time-zone differences) and parses the HTML for the URLs of the displayed image and the “highest resolution” version. The “highest resolution” image for last Sunday is smaller than the displayed one, which is why I haven’t limited the script to returning only that. And since JPEGS aren’t the only kind of picture, I haven’t picked out those exclusively either, though it could easily be done.
set {year:y, month:m, day:d} to (current date)
set todaysURL to "http://apod.nasa.gov/apod/ap" & text 3 thru -1 of ((y * 10000 + m * 100 + d) as text) & ".html"
set image_url_list to paragraphs of (do shell script ("curl " & quoted form of todaysURL & " |
sed -En '# Limit the interest to lines between <br> and </center> tags.
/<br>/,/<[/]center>/ {
# Within that limit, find lines containing "href=\"" or "src=\"".
/.*([Hh][Rr][Ee][Ff]|[Ss][Rr][Cc])=\"([^\"]+).+/ {
# Extract the URL from between the relevant quotes.
s||\\2| ;
# If the URL does not begin with "http:" then prepend the "apod" root.
/^http:/ !s|^|http://apod.nasa.gov/apod/| ;
# Print the URL.
p ;
}
# Exit at the </center> tag after the (first) <br> so that only the image or movie URL(s) are returned.
/<[/]center>/ q ;
}'"))
Edit: Modified to cope with a variation in the APOD HTML’s “
Great script, quick and nice comments. I was just going through a sed tutorial the other day, so it’ll be a good project trying to understand this. Never thought about reading the html.
------------------------------------------------------------------------------------------------
# Author: Christopher Stone
# Created: 2012-08-30 : 12:05
# Modified: 2012-08-30 : 03:36
# Application: Safari, Shell: awk, date, sed
# Purpose: Open Today's APOD if front Safari Document is not on that domain.
# If on APOD domain go to previous APOD page.
------------------------------------------------------------------------------------------------
tell application "Finder" to set desktopBounds to bounds of window of desktop
tell application "Safari"
try
set _url to URL of front document
_url
on error
activate
set _url to ""
end try
if _url does not start with "http://apod.nasa.gov/apod/" then
set newDoc to make new document with properties {URL:"http://apod.nasa.gov"}
set bounds of front window to desktopBounds
else
tell front document
tell me to set _date to do shell script "echo " & _url & "| awk -F'/apod/ap' '{ print $2 }'| awk -F. '{ print $1 }'"
if _date ≠"" then
tell me to set newURL to do shell script "echo 'http://apod.nasa.gov/apod/ap'`date -v -1d -jf \"%y%m%d\" " & "\"" & _date & "\" \"+%y%m%d\"`.html"
else
set _src to quoted form of (get source)
tell me to set newURL to do shell script "
APOD=http://apod.nasa.gov/apod/ap;
D1=`echo " & _src & " | sed -En 's/(^ *[0-9]{4} [a-zA-Z]+ [0-9]{2}) */\\1/p'`;
D2=`date -v -1d -jf \"%Y %B %d\" \"$D1\" \"+%y%m%d\"`;
echo $APOD$D2.html;"
end if
set URL to newURL
end tell
end if
end tell
------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------
# Author: Christopher Stone <junksieve@thestoneforge.com>
# 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 = a_tags[i]." & 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
*)
tell application "Safari"
try
set _url to URL of front document
_url
on error
activate
set _url to ""
end try
if _url does not start with "http://apod.nasa.gov/apod/" then
set newDoc to make new document with properties {URL:"http://apod.nasa.gov"}
repeat
try
set _url to URL of front document
_url
exit repeat
end try
end repeat
end if
end tell
return _url
end safariUrl
------------------------------------------------------------------------------------------------
--» MAIN
------------------------------------------------------------------------------------------------
try
set pageURL to safariUrl()
if pageURL starts with "http://apod.nasa.gov/apod/" then
set apodSrcQtd to safariSrc()
set regexStr to "http:\\/\\/apod\\.nasa\\.gov\\/apod\\/image\\/.+\\.(jpg|gif)"
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
------------------------------------------------------------------------------------------------