Open this script in Script Editor

-- Written by Gabriel Serafini - http://www.gabrielserafini.com
--
gserafini@gmail.com
--
$Id:$

--
This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/2.5/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.

--
this should not be needed if / when the lesson is released as a free
--
downloadable mp3, preferrably in an rss feed that can be subscribed to
--
this is recording something that is already being made free of charge

--
if you are the CSPS, please make the lesson available for free so that
--
it can bless all mankind. Thank you! :)

--
To do:
--
* Add suitable error messages if you don't have the correct apps installed
--
* Test on different OS versions, general QA on machines other than mine
--
* Add appropriate album cover artwork to track
--
* Add indicator for time remaining, possibly using Growl

set scriptName to "Bible Lesson to MP3 Recorder Script"
set scriptVersion to "1.6"
set scriptAuthor to "Gabriel Serafini - gserafini@gmail.com"

--
OPTIONS
set showDialogs to true -- set to false if you want no dialog command line version, useful for cron jobs...
set speakOn to false -- set to false if you're not interested in audio feedback

set includeDatestampInTitle to true -- set this to false if you don't want the YYYYMMDD included in the title of the track
set deleteSourceMP3FileAfterImport to true -- set to false if you don't want the Audio Recorder mp3 file to be deleted after it is copied into iTunes

set streamURL to "http://boss.streamos.com/real/csps/clerks_office/wbl/wbl_rp_28k.smi" -- URL to Real Player file
set recordingTime to 1731 -- (1711 = 28:31 in seconds (the length of each lesson) + some extra if it takes a little longer to buffer - this gives up to 20 seconds before the end will start to get cut off)
set normalBufferingTime to 2 -- average time before clip finishes buffering (wait this long before beginning to record) (shorter time will likely result in silence at beginning of track, longer times result in the beginning of the recording being cut off)

--
Track ID3 tag information
set trackArtist to "Christian Science Publishing Society"
set trackComposer to "Christian Science Publishing Society"
set trackAlbum to "Bible Lesson"
set trackGenre to "Bible Lesson"


set scriptHomeURL to "http://www.gabrielserafini.com/archives/2004/12/23/howto-how-to-record-a-streaming-ram-file-to-mp3-using-os-x"
set openingDialog to scriptName & return & "Version " & scriptVersion & return & return & "This script records the Christian Science Bible Lesson from Spirtuality.com to mp3 format and adds it to your iTunes. It takes approximately 29 minutes. You will not be able to hear it as it is recorded." & return & return & "Note, the timings in this script are optimized for a broadband connection and fairly fast computer. You may experience some silence at the beginning or end of your recording." & return & return & "Turn helper voice on or off?"
set instructionText to "Instructions:" & return & return & "1. Make sure you have the required applications installed. Click Setup if you need help." & return & return & "2. Click OK to begin." & return & return & return & "This was made by Gabriel Serafini" & return & "http://www.gabrielserafini.com/" & return & return
set
thanksText to "Thanks for using " & scriptName & "!" & return & return
set
UIscriptingWarning to "You must have UI Scripting turned on in order to automatically switch on Soundflower and turn it back off. Click 'Open System Preferences'then click the checkbox next to 'Enable access for assistive devices', or click 'Continue anyways' if you already have your audio settings the way you want them, or click 'Quit'."

set gotOriginalOutputSettings to false -- initialize original settings
set gotOriginalInputSettings to false -- initialize original settings
set trackAddedOK to false -- this gets set to true once file has been added to itunes

global scriptName, scriptVersion, scriptHomeURL, scriptAuthor, showDialogs, instructionText, thanksText, streamURL, recordingTime, normalBufferingTime, trackArtist, trackComposer, trackAlbum, trackGenre, includeDatestampInTitle, deleteSourceMP3FileAfterImport, gotOriginalOutputSettings, gotOriginalInputSettings, originalSelectedOutputRow, theOutputRows, originalSelectedInputRow, theInputRows, UIscriptingWarning, playTrack, speakOn, openingDialog, this_track, trackAddedOK


--
Begin script
if showDialogs then
    
displayIntro()
else
    -- or just operate quietly
    
beginRecord()
end if


on
displayIntro()
    
display dialog openingDialog buttons {"Voice On", "Voice Off", "Quit"} default button 1 with icon note
    if the button returned of the result is
"Voice On" then
        set
speakOn to true
    else if the button returned of the result is
"Voice Off" then
        set
speakOn to false
    else
        set
speakOn to false
        
displayThanks()
        
return
    end if
    
    if
speakOn then say "Welcome to " & scriptName & " version " & scriptVersion & ". Click Enter or the \"Begin Recording\" button to begin record process, \"Setup\" button for instructions about required applications or the \"Quit\" button to exit."
    
display dialog instructionText buttons {"Begin Recording", "Setup", "Quit"} default button 1 with icon note
    if the button returned of the result is
"Quit" then
        
displayThanks()
        
return
    else if the button returned of the result is
"Setup" then
        
displayThanks()
        
tell application "Safari"
            
launch
            make new document at end of documents
            set URL of document
1 to scriptHomeURL
        
end tell
        return

    else
        
beginRecord()
    
end if
end
displayIntro

--
call this with state set to "soundflower" to turn on Soundflower (2ch) for both
--
input and output devices. Call again with "original" or "" to return to original
--
settings. It is a requirement that user has 'Enable access for assistive devices'
--
selected in System Preferences > Universal Access for this to work
on toggleSoundPrefs(state)
    
tell application "System Preferences"
        
activate
        set current pane to pane
"com.apple.preference.sound"
        
tell application "System Events"
            
tell process "System Preferences"
                
set uiScripting to false
                try
                    -- Select the Output tab in Sound preference panel
                    
tell tab group 1 in window "Sound"
                        
click radio button "Output"
                    
end tell
                    
                    if
gotOriginalOutputSettings is false then
                        -- Get current settings
                        
set theOutputRows to every row of table 1 of scroll area 1 of tab group 1 of window "Sound"
                        
repeat with aRow in theOutputRows
                            
if aRow is selected then
                                set
originalSelectedOutputRow to (value of text field 1 of aRow as text)
                                
global originalSelectedOutputRow
                                
exit repeat
                            end if
                        end repeat
                        set
gotOriginalOutputSettings to true
                    end if
                    
                    if
state is "soundflower" then
                        -- set Output device to Soundflower (2ch)
                        
repeat with aRow in theOutputRows
                            
if (value of text field 1 of aRow as text) is equal to "Soundflower (2ch)" then
                                set selected of
aRow to true
                                exit repeat
                            end if
                        end repeat
                    else
                        -- set back to original values
                        
repeat with aRow in theOutputRows
                            
if (value of text field 1 of aRow as text) is equal to originalSelectedOutputRow then
                                set selected of
aRow to true
                                exit repeat
                            end if
                        end repeat
                    end if

                    
                    --
Select the Output tab in Sound preference panel
                    
tell tab group 1 in window "Sound"
                        
click radio button "Input"
                    
end tell
                    
                    if
gotOriginalInputSettings is false then
                        -- Get current settings
                        
set theInputRows to every row of table 1 of scroll area 1 of tab group 1 of window "Sound"
                        
repeat with aRow in theInputRows
                            
if aRow is selected then
                                set
originalSelectedInputRow to (value of text field 1 of aRow as text)
                                
global originalSelectedInputRow
                                
exit repeat
                            end if
                        end repeat
                        set
gotOriginalInputSettings to true
                    end if
                    
                    if
state is "soundflower" then
                        -- set Input device to Soundflower (2ch)
                        
repeat with aRow in theInputRows
                            
if (value of text field 1 of aRow as text) is equal to "Soundflower (2ch)" then
                                set selected of
aRow to true
                                exit repeat
                            end if
                        end repeat
                    else
                        -- set back to original values
                        
repeat with aRow in theInputRows
                            
if (value of text field 1 of aRow as text) is equal to originalSelectedInputRow then
                                set selected of
aRow to true
                                exit repeat
                            end if
                        end repeat
                    end if
                    set
uiScripting to true -- this confirms that UI scripting is turned on
                
end try
            end tell
        end tell
    end tell
    
    if
uiScripting is false then
        
displayUIscriptingWarning()
    
end if
end
toggleSoundPrefs



on beginRecord()
    
if showDialogs and speakOn then say "Now we're going to change your system sound preferences to enable sound flower 2 channel mode. After this is done, Real Player and Audio Recorder will launch and the process will begin. This will take a total of about 29 minutes, during which time you will not hear anything. The recording will stop automatically."
    
toggleSoundPrefs("soundflower")
    
    
tell application "RealPlayer" to activate
    tell application
"Audio Recorder" to activate
    
    tell application
"RealPlayer"
        
open clip with URL streamURL
    
end tell
    
    tell
application
"Audio Recorder"
        
set the output format to MP3
        set the quality to voice
        delay
normalBufferingTime -- Real Player usually takes this long to buffer stream
        
start recording
        set
fileLocation to current file path -- this will use the current default location set in the preferences for Audio Recorder
        
        
tell application "RealPlayer"
            
set clipTitle to clip title of player 0
        
end tell
        
        delay
recordingTime -- this is how long we want to record
        
stop recording
    end tell
    
    try

        tell application
"Audio Recorder" to quit
    end try
    
    try

        tell application
"RealPlayer" to quit
    end try

    
    --
set sound prefs back to original
    
toggleSoundPrefs("original")
    
if showDialogs and speakOn then say "Your sound preferences have been restored to the way they were before. Now we are going to import the recorded mp3 into iTunes."
    
    
try
        tell application
"System Preferences" to quit
    end try
    
    set
fileAlias to POSIX file fileLocation as alias
    set
theDate to current date
    set
yyyy to year of theDate
    
    
if includeDatestampInTitle is true then
        -- convert date to YYYYMMDD format
        
copy ((offset of (the month of (theDate)) in "jan feb mar apr may jun jul aug sep oct nov dec ") + 3) / 4 as integer to mo
        
if mo < 10 then copy "0" & mo as string to mo
        
copy day of (theDate) to da
        
if (day of (theDate) < 10) then copy "0" & da as string to da
        
copy (year of (theDate) as string) & mo & da to shortDate
        
        
set clipTitle to shortDate & " - " & clipTitle -- doing this so it lines up with other Bible Lessons in iTunes when sorted by Name
    
end if
    
    tell
application
"iTunes" to activate
    tell application
"iTunes"
        
launch
        try
            -- add ID3 tags
            
set this_track to add fileAlias to playlist "Library" of source "Library"
            
set name of this_track to clipTitle
            
set artist of this_track to trackArtist
            
set composer of this_track to trackComposer
            
set album of this_track to trackAlbum
            
set genre of this_track to trackGenre
            
set year of this_track to yyyy
            
set comment of this_track to "Saved from Spirituality.com, using \"" & scriptName & " version " & scriptVersion & "\" (written by " & scriptAuthor & ")" & return & return & "Info: " & scriptHomeURL
            
            
set trackAddedOK to true
        end try
    end tell
    
    if
deleteSourceMP3FileAfterImport is true then
        tell application
"Finder"
            
delete fileAlias
        
end tell
    end if
    
    
displayThanks()
    
end beginRecord

on displayUIscriptingWarning()
    
if showDialogs is true then
        display dialog
UIscriptingWarning buttons {"Open System Preferences", "Continue Anyways", "Quit"} default button 2 with icon warning
        
if the button returned of the result is "Quit" then
            
displayThanks()
        
else if the button returned of the result is "Open System Preferences" then
            tell application
"System Preferences"
                
activate
                set current pane to pane
"com.apple.preference.universal"
            
end tell
            
displayUIscriptingWarning()
        
end if
    end if
end
displayUIscriptingWarning

on displayThanks()
    
if showDialogs is true then
        if
trackAddedOK then
            display dialog
thanksText buttons {"Play mp3 in iTunes and Exit", "Exit without playing"} default button 1
            
if the button returned of the result is "Play mp3 in iTunes and Exit" then
                if
speakOn then say "Thanks. Your recording will begin playing now. Goodbye."
                
delay 2
                
tell application "iTunes"
                    --
and now play it
                    
play this_track
                
end tell
                return

            else
                if
speakOn then say "Thanks and goodbye!"
                
return
            end if
        else
            display dialog
thanksText buttons {"Exit"} default button 1
            
if the button returned of the result is "Exit" then
                if
speakOn then say "Thanks and goodbye!"
                
return
            end if
        end if
    end if
end
displayThanks