A script to use voice recognition with iTunes

Feb 27, '03 09:32:00AM

Contributed by: jkooy

There is a scriptable application hidden in /System -> Library -> Frameworks -> Carbon.framework -> Frameworks -> SpeechRecognition.framework -> Versions -> Current -> Resources called "SpeechRecognitionServer." It allows you to write scripts that have voice recognition components to them. I have written an AppleScript that will allow me to speak the song I want to play. I saved this script as a compiled script called "play" and placed it in the Speakable Items folder (in ~/Library -> Speech once Speech is enabled in System Prefs). Then when Speakable Items is turned on, I can say "play" "artist" "song" and iTunes will play that track. Read the rest of the article for the script...

Enter the following in Script Editor and remember to save it as a compiled script:

tell application "iTunes"
  set myFinishedList to {"Cancel"} as list -- Add Cancel to the list
  set myList to (get artist of every track of playlist 1) as list ¬
    -- gets every artist in first iTunes library
  
  repeat with myItem in myList
    if myFinishedList does not contain myItem then
      set myFinishedList to myFinishedList & myItem ¬
        -- remove any duplicate band names
    end if
  end repeat
end tell
tell application "SpeechRecognitionServer"
  set myArtist to (listen for myFinishedList with prompt myFinishedList)
end tell

if myArtist is not "Cancel" then
  tell application "iTunes"
    set myTracks to get every track of playlist 1 where artist ¬
      is myArtist -- playlist 1 is my mp3 library
    if number of items of myTracks is 1 then
      play (get first item of myTracks) ¬
        -- if only one song by an artist play that one
    else
      set myTrackNames to {"Cancel"} & (get name of every track of playlist 1 ¬
        where artist is myArtist) -- filter by artist
      tell application "SpeechRecognitionServer"
        set myTrack to (listen for myTrackNames with prompt ¬
          myTrackNames) -- get the song
      end tell
      if myTrack is not "Cancel" then
        set myTrackList to (get every track of playlist 1 where name is myTrack)
        play (get first item of myTrackList) ¬
          -- should be only one item in list but just in case play first one
      end if
    end if
  end tell
end if
[robg adds: I haven't tested this one, as I'm currently lacking a microphone...]

Comments (14)


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