Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Use AppleScript to listen to NPR programs in Safari Web Browsers
Poor RealMedia plug-in support in the Camino browser finally forced me to look for an alternative way to listen to my favorite NPR programs. After very little research of the examples on Apple's AppleScript site, I decided to use the Safari browser and AppleScript to automate the process. What follows is an AppleScript that uses Safari and the RealOne player to play my two favorite programs from NPR. After I finished, I decided that it might be of some use to others -- so here it is.

Please note this script depends upon the largely static nature of the web pages from which it builds the streaming media URLs. Any significant changes in these sites may result in this script breaking.

[robg adds: Read the rest of the article for the script; I have not tested it myself.]

Note that the code that follows has been formated for 80 columns using "soft" returns (Option Return within Script Editor). If these don't survive the formating process, you will get errors when you cut and paste the code into Script Editor. Each error will point you either to the end of a line that is missing a "soft" return or to the line following the line missing the "soft" return. Just delete the line break at these points and the code should compile.
-------------------------------- Start of Script "NPR.SCPT"

Note: Your connection speed may affect 'wait_delay' value that 
you should use, a slower connection may require a higher value.
display dialog the 
  "Stream which show?" buttons {"Wait Wait!", "Car Talk", "Cancel"} 
  default button "Cancel"

if the button returned of the result is "Wait Wait!" then
  --- Wait wait don't tell me!
  set wait_delay to 3
  set webURL to ""
  set start_delimitor to "<a href=\"javascript:getStaticMedia('"
  set end_delimitor to "','"
  set prepend_URL to "rtsp://"
  set append_URL to ".rm"
else if the button returned of the result is "Car Talk" then
  --- Car Talk
  set wait_delay to 3
  set webURL to ""
  set start_delimitor to "<TR VALIGN=TOP><TD><B><A HREF="
  set end_delimitor to ">Segments"
  set prepend_URL to ""
  set append_URL to ""
end if

tell application "Safari"
    --- Assume that Safari opens a blank doc at startup
    set the URL of document 1 to webURL
    display dialog the 
      "Press button when page is loaded. . ." buttons {"Continue"} 
      giving up after wait_delay
    set the page_source to the source of document 1
    set the realmedia_link to my 
      parse_item(page_source, start_delimitor, end_delimitor)
    set the rm_link to prepend_URL & realmedia_link & append_URL
    tell application "RealOne Player"
      GetURL rm_link
    end tell
    on error the error_message number the error_number
    display dialog the error_message buttons {"Cancel"} 
      default button "Cancel"
  end try
end tell

This routine was stolen from Apple's "Reuters Top News" script.  However,
I have altered it s.t. it returns only one string item instead of a list of 
string items and the returned string item is exclusive of the 
bracketing tags.  For example, 'parse_item("ABCDEFGH","BC","F")' should
return "DE".

on parse_item(page_source, open_tag, close_tag)
  set the tag_length to the length of the close_tag
  set the open_tag_offset to the offset of the open_tag in the page_source
  if the open_tag_offset is not 0 then
    set the open_tag_offset to open_tag_offset + (length of open_tag)
    set the page_source to 
      (text open_tag_offset thru -1 of the page_source)
    set the close_tag_offset to the offset of the close_tag 
      in the page_source
    set this_item to (text 1 thru (the close_tag_offset - 1) 
      of the page_source)
  end if
  return this_item
end parse_item

-------------------------------- End of Script "NPR.SCPT"
  • Currently 2.67 / 5
  You rated: 2 / 5 (3 votes cast)

Use AppleScript to listen to NPR programs in Safari | 0 comments | Create New Account
Click here to return to the 'Use AppleScript to listen to NPR programs in Safari' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.