• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

5. AppleScript for iTunes > 80. Search for Lyrics on Google

Search for Lyrics on Google

Use info from the current or selected track to search for lyrics pages via Google in Safari.

There are tons of lyrics sites on the Web that are ready to provide you with the words to your favorite songs. This hack Googles for the lyrics of the currently playing or selected song in iTunes. You’ll be singing along by the second verse.

The Code

This script gets the name and artist of the currently playing track (or, if no track is playing, the selected track), tidies up the text, and incorporates it into a URL that is then used by Safari to search Google for sites containing the lyrics of that particular track:

    -- base of the URL string, includes the term "lyrics"
    property baseURL : "http//www.google.com/search?q=lyrics+"

    tell application "iTunes"

      -- get a reference to playing or selected track
      if player state is not stopped then
        set theTrack to current track
      else if selection is not {} then
        set theTrack to (item 1 of selection)
      else
        display dialog "Nothing is playing or selected." buttons {"Cancel"} ¬
          default button 1 with icon 0
      end if

      -- get the name and artist and replace "bad" characters
      tell theTrack
        set nom to my fixChars(name)
        set art to my fixChars(artist)

      end tell

      -- assemble URL string, replace spaces with "+"
      set theURL to (baseURL & ¬
        (my replace_chars((art & "+" & nom), " ", "+"))) as text

        my open_location(theURL)

      end tell

      on fixChars(a)
        set myDelims to {"!", "@", "#", "$", "%", "^", "&", "*",¬
          "(", ")", "-", "-", "+", "=", ":", ";", "'", ",", ".", "/", ¬
          "<", ">", "?", "{", "}", "[", "]"}
      repeat with curDelim in myDelims
        set AppleScript's text item delimiters to curDelim
        set s to every text item of a
        set AppleScript's text item delimiters to {""}
        set a to s as string
      end repeat
      return a
    end fixChars

    on replace_chars(txt, srch, repl)
      set AppleScript's text item delimiters to the srch
      set the item_list to every text item of txt
      set AppleScript's text item delimiters to the repl
      set txt to the item_list as string
      set AppleScript's text item delimiters to ""
      return txt
    end replace_chars

    to open_location(theURL)
      tell application "Safari"
        activate
        -- un-comment....Search:" then
          make new document at end of documents
        end if
        set URL of document 1 to theURL
      end tell
    end open_location

PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint