Download NPR audio files with Safari

Mar 20, '03 09:10:00AM

Contributed by: pheel

NPR has switched from publishing RealAudio streams as .ram files to .smil files. Unfortunately, the Safari web browser appears to truncate that suffix to just .smi, which tells Mac OS that the file is a Self Mounting Image file. The system attempts to mount the bogus image on the Desktop and fails, never opening the file in the RealOne Player as intended. The first file downloaded will have the name "dmg.smi". Subsequent files are named "dmg-1.smi", "dmg-2.smi", and so on.

This script watches for files with names like that to appear in your Safari Download folder. When one does appear, the script alerts you and lets you choose whether to open it in RealOne Player or ignore it and do nothing. You can disable the confirmation dialog box by changing the value of the "request_confirmation" property to "false".

If you're a better AppleScripter than I am, and you almost certainly are, please add comments to this hint and/or send any improvements to me at pheelb at yahoo dot com. I hope this improves your NPR listening!

[robg adds: I have not tested this script, but there's no potentially dangerous code involved.]

HOW TO INSTALL:

The script is implemented as a Folder Action. As an unfortunate consequence, installing it is a bit harder than dragging it somewhere and forgetting about it. Also, it requires OS X 10.2 or later (for the built-in Script Menu). Don't worry, though, the steps are simple.

  1. Launch /Applications -> AppleScript -> Script Editor, and paste the script below into a new script window. Make sure you paste into the main area of the window and not the part labeled "Description". Select File -> Save, and give it a name meaningful to you. Under "Format" choose "Compiled Script" if it isn't the default. Save it to the Desktop.

  2. Activate the Script Menu (standard with OS X 10.2 and later). This is easy: "Just drag the ScriptMenu.menu icon, located in the AppleScript folder in the Applications folder, to the menu bar at the top right of your screen and let go." (quoted from Apple's ScriptMenu page)

  3. Once the Script Menu is in your menu bar, select "Open Scripts Folder" from the menu (it's the last item). The location of this folder is /Users -> yourname -> Library -> Scripts. Inside it should be a folder called "Folder Action Scripts." Create it if it isn't there, and put the script file you saved in step one inside it.

  4. Now select Script Menu -> Folder Actions -> Attach Script to Folder. In the dialog box that appears, select the name of this script and press "OK". You'll now be asked what folder you want to attach the script to. Select your Safari Download folder, which is the Desktop unless you've changed it in the Safari preferences.

  5. Finally, select Script Menu -> Folder Actions -> Enable Folder Actions.
Damn, that was long. Sorry. But now it's ready to go. Go to npr.org in Safari and listen to something. The script will detect when the new file appears in your Downloads folder and ask you what to do. Press "Open with RealOne Player" to listen, or press "Cancel" to ignore it. If you don't want to be asked, change the property called "request_confirmation" from true to false and resave the script.
-- controls whether or not to display a confirmation dialog box. change value 
-- to 'false' if you want the script to automatically open files with names 
-- like "dmg*.smi" in RealOne Player.
property request_confirmation : true

-- set the amount of time before dialogs auto-answer.
property dialog_timeout : 30

on adding folder items to this_folder after receiving added_items
  
  -- find out how many new items have been placed in the folder
  set the item_count to the number of items in the added_items
  
  -- continue only if just one item was placed in the folder
  if the item_count is 1 then
    try
      tell application "Finder"
        --get the name of the folder
        set the folder_name to the name of this_folder
      end tell
      
      set the_item to item 1 of the added_items
      
      tell application "Finder"
        set item_name to the name of the_item
      end tell
      
      set saved_delimiters to AppleScript's text item delimiters
      set AppleScript's text item delimiters to {"."}
      
      set the_base_name to the first text item of item_name
      set the_suffix to the last text item of item_name
      
      set AppleScript's text item delimiters to saved_delimiters
      
      if (the the_base_name begins with "dmg") and (the the_suffix is "smi") then
        if request_confirmation is true then
          --create the dialog box alert string
          set alert_message to "Folder Actions Alert:" & return & return & ¬
          "New item in " & folder_name & ": " & item_name & return & return
          
          display dialog the alert_message buttons {"Open with RealOne Player", "Cancel"} ¬
          default button 2 with icon 1 giving up after dialog_timeout
          
          set my_choice to the button returned of the result
        else
          set my_choice to "Open with RealOne Player"
        end if
        
        if my_choice is "Open with RealOne Player" then
          tell application "RealOne Player"
            activate
            open the_item
          end tell
        end if
      end if
      
    end try
  end if
  
end adding folder items to

Comments (9)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20030318141130827