O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
iPod and iTunes Hacks
By Hadley Stern
October 2004
More Info

HACK
#92
Search the iTunes Music Store for the Current Song
Search the iTunes Music Store using info from the currently playing track.
The Code
[Discuss (0) | Link to this hack]

The Code

The script gets tag info from the currently playing track and displays it in a listbox so the user can choose which tags to use as search criteria. The script then affixes the chosen data to a URL, which is sent to iTunes.

	-- get info from current track
	tell application "iTunes"
	  if player state is not stopped then
	    if class of current track is not URL track then
		  tell current track to set {alb, art, nom, com} to 
		    {album, artist, name, composer}
		else

		if current stream title is not "" then 
		  set {art, nom} to my text_to_list(current stream title, " - ")	
		  set {alb, com} to {"", ""}
        else 
		  display dialog "Can't get data from stream." buttons  
		    {"Cancel"} default button 1 with icon 0 giving up after 30 
		end if

	    end if	
	  else 
		display dialog "No track currently playing." buttons {"Cancel"}  
          default button 1 with icon 0 giving up after 30 
      end if 
	end tell

	-- build list to display in choose from list box 
	set theOptions to {"Song:" & tab & nom, "Artist:" & tab & art,  
      "Album:" & tab & alb, "Composer:" & tab & com}

	-- display choose from list box
	set searchOpts to (choose from list theOptions with prompt  
	  "Select search terms:" OK button name  
	  "Search iTMS" default items {}  
	  with multiple selections allowed) as string
	if searchOpts is "false" then error number -128

	-- base search string
	-- (remove the continuation characters!)
	set searchStr to 
	  "itms://phobos.apple.com/WebObjects/MZSearch.woa/ 
	wa/advancedSearchResults?"

	-- build search string URL 
	if searchOpts contains "Song:" then set searchStr to searchStr &  
	  "songTerm=" & replace_chars(nom, " ", "%20") & "&"
        if searchOpts contains "Artist:" then set searchStr to searchStr &  
	  "artistTerm=" & replace_chars(art, " ", "%20") & "&"
        if searchOpts contains "Album:" then set searchStr to searchStr &  
	  "albumTerm=" & replace_chars(alb, " ", "%20") & "&"
        if searchOpts contains "Composer:" then set searchStr to searchStr &  
	  "composerTerm=" & replace_chars(com, " ", "%20") & "&"

	-- send to iTunes 
	open location searchStr

	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

	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

If iTunes is not stopped (that is, if it is playing or paused), the script gets the name, artist, album, and composer info from the current trackobject. If the current track is a stream and the streamer is sending the info correctly, the script just gets the name and artist of the track. The info obtained is displayed in a listbox from which the user can select which tags to use in a search of the iTunes Music Store, as shown in .

Figure 1. Selecting which of the current track's tags to use for a search of the iTunes Music Store

The result of the choose from liststatement is stored in the searchOptsvariable. It will contain a list of keywords and properties. The script checks for each keyword in searchOptsand adds the corresponding property to searchString.The script also converts any spaces to hexadecimal %20using the replace_chars()handler.

Finally, the searchStringis sent to the iTunes Music Store using the open locationcommand. iTunes receives the result because the URL begins with the itms:protocol.


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.