iTunes Get Lyrics

This is my first Applescript that I wrote (although I have dabbled in Lotusscript before) to assist with a solution of getting Lyrics to my iTunes songs without copy & paste from a website. I use this script in conjunction with the pearLyrics widget.
I was only able to complete this with the assistance of the Macscripter site by viewing solutions posted here. Thanks all !!

How it works
The script searches the main library for songs without any Lyrics. It excludes video’s, movies and podcasts then builds a new playlist of those songs. The new playlist is then selected and the user is prompted to enter in the amount of time in seconds to play each song for. This is so the widget can access the internet and get the lyrics. Each song in the playlist is played for the amount of time specified. The script ends when the user stops the current song being played or is at the end of the playlist.
If you restart the script and the playlist exists you are prompted to rebuild the playlist. If you select no, the script will continue from where it left off, again prompting for the amount of time to play each song for.

This is only tested with iTunes v7, iMac G5 and pearLyrics v0.5 & v0.6

I would appreciate any feedback and hope this is useful to others.
Cheers


tell application "iTunes"
	set the_result to ""
	set doit to button returned of (display dialog "This program will make a new playlist of all songs in the main library with no lyrics, then play each song so your pearlyrics widget can get the lyrics." with icon 2 buttons {"Quit", "OK"} default button 2)
	if doit is "Quit" then error number -128
	
	if (exists playlist "Tunes No Lyrics") then
		set the_result to button returned of (display dialog "The 'Tunes No Lyrics' Playlist Exists - Rebuild the Playlist?" with icon 2 buttons {"Yes", "No"} default button 2)
		if the_result is "Yes" then
			set view of front browser window to playlist "Tunes No Lyrics"
			using terms from application "iTunes"
				delete (every track of playlist "Tunes No Lyrics")
				with timeout of 600 seconds
					display dialog "Building the Playlist 'Tunes No Lyrics'. This may take a while so please be patient." with icon 2 buttons {"OK"} giving up after 30
					duplicate (file tracks of library playlist 1 whose lyrics is "" and kind does not contain "video" and kind does not contain "movie" and podcast is not true) to playlist "Tunes No Lyrics"
				end timeout
			end using terms from
		end if
	end if
	if the_result = "" then
		if not (exists playlist "Tunes No Lyrics") then make new playlist with properties {name:"Tunes No Lyrics"}
		with timeout of 600 seconds
			display dialog "Building the Playlist 'Tunes No Lyrics'. This may take a while, so please be patient." with icon 2 buttons {"OK"} giving up after 130
			duplicate (file tracks of library playlist 1 whose lyrics is "" and kind does not contain "video" and kind does not contain "movie" and podcast is not true) to playlist "Tunes No Lyrics"
		end timeout
	end if
end tell


tell application "iTunes"
	set view of front browser window to playlist "Tunes No Lyrics"
	display dialog "Enter the Time in Seconds to play the tracks for. You may need longer for modem connections !" with icon 1 default answer "12" buttons {"Cancel", "OK"} default button 2 giving up after 60
	copy the result as list to {text_returned, button_pressed}
	play
	repeat until player state is stopped
		delay {text_returned}
		next track
		
	end repeat
	
end tell

Hi, Redman.

That’s pretty good for a first AppleScript and it works very well. :slight_smile:

Just to be finicky, these three lines:

end if
if the_result = "" then
	if not (exists playlist "Tunes No Lyrics") then make new playlist with properties {name:"Tunes No Lyrics"}

. could be reduced to:

else
	make new playlist with properties {name:"Tunes No Lyrics"}

If you were so moved, you could also replace the two similar ‘with timeout’ blocks with a handler, which would reduce the amount of code slightly. To call the handler from within the ‘tell’ block, you’d need to put ‘my’ before the calls, to indicate that the handler belongs to the script, not to iTunes.

on rebuildPlaylist(giveupTime)
	tell application "iTunes"
		with timeout of 600 seconds
			display dialog "Building the Playlist 'Tunes No Lyrics'. This may take a while so please be patient." with icon 2 buttons {"OK"} giving up after giveupTime
			duplicate (file tracks of library playlist 1 whose lyrics is "" and kind does not contain "video" and kind does not contain "movie" and podcast is not true) to playlist "Tunes No Lyrics"
		end timeout
	end tell
end rebuildPlaylist

tell application "iTunes"

	-- Blah blah blah.

			delete (every track of playlist "Tunes No Lyrics")
			my rebuildPlaylist(30)
		end if
	else
		make new playlist with properties {name:"Tunes No Lyrics"}
		my rebuildPlaylist(130)
	end if

	-- etc.
end tell

More seriously, I’d recommend not doing this:

display dialog "Enter the Time in Seconds to play the tracks for. You may need longer for modem connections !" with icon 1 default answer "12" buttons {"Cancel", "OK"} default button 2 giving up after 60
copy the result as list to {text_returned, button_pressed}

In the second line, the record returned by ‘display dialog’ is coerced to list and the first two items in the list are assigned in order to the variables text_returned and button_pressed. Although this will almost certain work every time in here, the point of a record is that its properties aren’t necessarily in any particular order. In theory, the coercion to list could return the ‘button returned’, ‘text returned’, and ‘gave up’ results in a different order, in which case you’d be setting the variables to the wrong values. There’s an equivalent method for setting several variables to the values in a record:

display dialog "Enter the Time in Seconds to play the tracks for. You may need longer for modem connections !" with icon 1 default answer "12" buttons {"Cancel", "OK"} default button 2 giving up after 60
set {text returned:text_returned, button returned:button_pressed} to the result

However, since your script doesn’t use button_pressed, you could simply use the conventional syntax here:

set text_returned to text returned of the result