Script: Gathering lyrics for all tracks in iTunes

Discussion in 'Mac Apps and Mac App Store' started by alumac, May 15, 2009.

  1. alumac macrumors member

    Joined:
    Mar 31, 2009
    #1
    I've slightly modified Doug's "Lyrics via Lyrics wiki" script so that it automatically crawls through the entire Library (or current playlist) in iTunes, gets and saves lyrics for each tracks.

    You can view the lyrics by using the "Get Info" command on a track. If you want to use it, open Script Editor and copy the following script into it.

    Open iTunes, play the first track in the Library. (You may also mute the volume at the this time). Then go back to script editor, click Compile, and then click Run.

    iTunes should now begin gathering lyrics for your tracks. It took me about 7 minutes to get lyrics for 410 items. iTunes was working, visible but irresponsive. So keep that in mind.

    Finally, it might throw some errors if the lyrics server is being irresponsive. So don't worry and just click OK.

    SCRIPT:

    Code:
    (*
    "Collect Lyrics for All Tracks" for iTunes
    A Modified version of "Lyrics via LyricWiki" for iTunes written by Doug Adams
    dougadams@mac.com
    Thanks to the many Correspondents who emailed me info on the SOAP command.
    <http://lyricwiki.org/LyricWiki_talk:SOAP>
    
    v1.0 February 28 2009
    -- initial release
    
    Get more free AppleScripts and info on writing your own
    at Doug's AppleScripts for iTunes
    http://dougscripts.com/itunes/
    
    This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
    
    This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
    
    Get a copy of the GNU General Public License by writing to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    
    or visit http://www.gnu.org/copyleft/gpl.html
    
    *)
    
    
    property my_title : "Lyrics via LyricWiki"
    property nom : ""
    property art : ""
    global sel
    
    repeat
    	tell application "iTunes"
    		if player state is playing then
    			set sel to current track
    			tell sel to set {nom, art} to {get name, get artist}
    		else if selection is not {} and (count items of selection) is 1 then
    			set sel to item 1 of selection
    			tell sel to set {nom, art} to {get name, get artist}
    		else
    			return
    		end if
    	end tell
    	log {nom, art}
    	try
    		tell application "http://lyricwiki.org/server.php" to set rez to (call soap {method name:"getSong", method namespace uri:"urn:LyricWiki", SOAPAction:"urn:LyricWiki#getSong", parameters:{artist:art as text, song:nom as text}})
    	on error m
    		tell application "iTunes"
    			display dialog "Error from LyricWiki:" & return & return & tab & m with title my_title
    		end tell
    	end try
    	-- log rez
    	set theLyrics to (lyrics of rez) as text
    	set lyricWikiPageURL to |url| of rez
    	
    	log theLyrics
    	
    	-- copy to lyrics or display LyricsWiki page?
    	if theLyrics does not contain "Not found" then
    		set displayLyrics to my text_to_list(theLyrics, (ASCII character 10))
    		
    		tell application "iTunes"
    			try
    				set sel's lyrics to theLyrics
    			end try
    		end tell
    	end if
    	
    	tell application "iTunes"
    		next track
    	end tell
    	
    end repeat
    
    on text_to_list(txt, delim)
    	set saveD to AppleScript's text item delimiters
    	try
    		set AppleScript's text item delimiters to {delim}
    		set theList to every text item of txt
    	on error errStr number errNum
    		set AppleScript's text item delimiters to saveD
    		error errStr number errNum
    	end try
    	set AppleScript's text item delimiters to saveD
    	return (theList)
    end text_to_list
    
    Have fun.
     
  2. acraig00 macrumors newbie

    Joined:
    Aug 13, 2009
    #2
    Script: Gathering Lyrics for Playlist 2.0

    LyricWiki API changed and broke this script. I have updated it using Doug's Apple Scripts' Lyrics via LyricsWiki 2.0. Enjoy.

    Code:
    (*
    "Collect Lyrics for All Tracks" for iTunes
    A Modified version of "Lyrics via LyricWiki" for iTunes written by Doug Adams
    dougadams@mac.com
    
    Thanks to the many Correspondents who emailed me info on the SOAP command.
    <http://lyricwiki.org/LyricWiki_talk:SOAP>
    
    v2.0 August 10 2009
    -- works around LyricWiki API issues
    See <http://groups.google.com/group/lyricwiki-api/browse_thread/thread/733ccd919d654040>
    
    v1.1 May 11 2009
    -- Addresses issue with high-ASCII text
    
    v1.0 February 28 2009
    -- initial release
    
    Get more free AppleScripts and info on writing your own
    at Doug's AppleScripts for iTunes
    http://dougscripts.com/itunes/
    
    This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
    
    This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
    
    Get a copy of the GNU General Public License by writing to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    
    or visit http://www.gnu.org/copyleft/gpl.html
    
    *)
    
    
    property my_title : "Lyrics via LyricWiki"
    property nom : ""
    property art : ""
    global sel
    
    repeat
    	tell application "iTunes"
    		if player state is playing then
    			set sel to current track
    			tell sel to set {nom, art} to {get name, get artist}
    		else if selection is not {} and (count items of selection) is 1 then
    			set sel to item 1 of selection
    			tell sel to set {nom, art} to {get name, get artist}
    		else
    			return
    		end if
    	end tell
    	log {nom, art}
    	try
    		tell application "http://lyricwiki.org/server.php" to set rez to (call soap {method name:"getSong", method namespace uri:"urn:LyricWiki", SOAPAction:"urn:LyricWiki#getSong", parameters:{artist:art as text, song:nom as text}})
    	on error m
    		tell application "iTunes"
    			display dialog "Error from LyricWiki:" & return & return & tab & m with title my_title
    		end tell
    	end try
    	-- log rez
    	set lyricWikiPageURL to my replace_chars(|url| of rez, "%2F", "_")
    	
    	if (lyrics of rez) as text does not contain "Not found" then
    		try
    			set theLyrics to (do shell script "curl " & quoted form of lyricWikiPageURL & "|grep \"'lyricbox'\"|sed -e \"s/<div class='.*lyricbox' >//g\"|sed -e's/<br \\/>/" & (ASCII character 13) & "/g'")
    			set displayLyrics to my text_to_list(theLyrics, (ASCII character 13))
    			
    			(*
    	tell application "Finder"
    		open location lyricWikiPageURL
    	end tell
    	*)
    			tell application "iTunes"
    				try
    					set sel's lyrics to (theLyrics as text)
    				end try
    			end tell
    		end try
    	end if
    	
    	tell application "iTunes"
    		next track
    	end tell
    	
    end repeat
    
    on text_to_list(txt, delim)
    	set saveD to AppleScript's text item delimiters
    	try
    		set AppleScript's text item delimiters to {delim}
    		set theList to every text item of txt
    	on error errStr number errNum
    		set AppleScript's text item delimiters to saveD
    		error errStr number errNum
    	end try
    	set AppleScript's text item delimiters to saveD
    	return (theList)
    end text_to_list
    
    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
    more info:
    LyricWiki API Changes

    Lyrics via Lyric Wiki 2.0

    -andrew
    acraig (zero) (zero) at gmail dot com
    twitter.com/acraig666
     

Share This Page