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

5. AppleScript for iTunes > 92. Search the iTunes Music Store for the Current S...

Search the iTunes Music Store for the Current Song

Search the iTunes Music Store using info from the currently playing track.

You can search the iTunes Music Store (iTMS) using the handy link arrows associated with each track in the library. But this AppleScript hack lets you search the iTMS for the currently playing song, even if it’s playing from a stream.

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

PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


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