10.5: Convert text to iTunes audio book via Services item

May 09, '08 07:30:04AM

Contributed by: miketyson

After reading this hint on macosxhints about how to create an audio file from text, I figured the process could probably be streamlined a bit. So, I created a Service (that appears in the Services menu) that operates on selected text. Select the plain text in your source location (text file, web page, email, etc.) and then select Services » Speak to iTunes Audiobook to speak the text into a track that will appear in the Audiobooks section of iTunes. Just remember to set the encoder quality beforehand in iTunes, because it's not configurable through AppleScript (I recommend using 'Spoken podcast').

Package download [40KB]

For those curious few, the AppleScript within the service is as follows:

on process(input)
  set theOriginalPath to (path to desktop folder as string) & "Speech.aiff"
  
  -- Speak text
  say input using "Alex" saving to theOriginalPath
  tell application "iTunes"
    -- Import AIFF audio
    set theOriginaliTunesFile to (add theOriginalPath)
    tell application "Finder" to delete theOriginalPath
    -- Prepare AAC encoder
    set lastEncoder to current encoder
    set newEncoder to (item 1 of (every encoder whose format is "AAC"))
    set current encoder to newEncoder
    -- Convert to AAC
    set theM4aFile to item 1 of (convert theOriginaliTunesFile)
    set current encoder to lastEncoder
    -- Remove original
    delete theOriginaliTunesFile
    set fileLocation to location of theM4aFile
    -- Set as audio book type
    tell application "Finder"
      set theFile to fileLocation as alias
      set file type of theFile to "M4B "
      if name extension of theFile is not "m4b" then
        set name of theFile to ((text 1 thru -((length of (get name extension of theFile)) + 1) of (name of theFile as text)) & "m4b") as string
      end if
    end tell
    -- Remove from iTunes, and re-add
    delete theM4aFile
    set theNewTrack to add theFile
    -- Select in iTunes
    reveal theNewTrack
  end tell
end process
[robg adds: I tested this, and it works as described. The service is installed in the top-level /Library » Services menu, and can be removed by simply deleting SpeakToItunesAudiobook.service in that folder. Once installed, you need to refresh the Services menu to see the new service; you can do that by logging out and in, by using one of the methods described in this older hint and its comments, or by using a third-party program such as ServiceScrubber. Note that this is set to 10.5 Only due to the use of the Alex voice; I'm not sure if it would run on 10.4 with a different voice, or if there are other AppleScript commands in the script that are 10.5 only as well.]

Comments (11)


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