Check if you already have iTunes Store Tracks in your Music Library

Hi All,

I wanted to be able to check if Tracks I am looking at in the iTunes Store where already in my Music library, and so save me purchasing songs twice.

The Script works on the full list in the track view.
I tested with 150 tracks (madonna) and it did not take too long.
(Not sure if anything already exists to do this…)

I wondered if anyone got any ideas of ways to improve it.:slight_smile:

Cheers

Mark

(* To get correct results when in the store the name column is second from left and artist   column is fourth from left 

example:

|  	| Name    		| Time	| Artist			| Album					|
| 1	| Hollywood		| 5:08	| The Cranberries	| To the Faithul Departed	|
| 2	| salvation		| 2:23	| The Cranberries	| To the Faithul Departed	|
 *)


set AppleScript's text item delimiters to ""
tell application "System Events" to tell process "iTunes"
	
	try
		tell window "iTunes" to tell outline 1 of (get 1st scroll area of splitter group 1 whose static texts of outline 1 is {})
			
			set SongList to {}
			set biglist to {}
			set bigTracklist to {}
			set Yestrack to 0
			(* get name and artist using GUI scripting from iTunes Store list*)
			set SongList to value of text field 1 of every row
			set ArtistList to value of text field 3 of every row
			set trackcountStore to (count of SongList) as string
			tell application "iTunes"
				
				repeat with i from 1 to number of items in SongList
					(* if  name  contains explicit, remove it*)
					set this_song to item i of SongList
					set this_artist to item i of ArtistList
					set oldDelims to AppleScript's text item delimiters
					set AppleScript's text item delimiters to {", explicit"}
					set this_song to text item 1 of this_song as string
					set AppleScript's text item delimiters to oldDelims
					try
						(* find matching song name and artist in your library*)
						set myLibTracks to (name of tracks of library playlist 1 whose name contains this_song and artist contains this_artist)
						set myLibTracksArtist to (artist of tracks of library playlist 1 whose name contains this_song and artist contains this_artist)
						
						set trackcount to count of myLibTracks
						(* there maybe more than one match in your library*)
						if trackcount is greater than 1 then
							repeat with i from 1 to number of items in myLibTracks
								copy (item i of myLibTracks) & " by " & item i of myLibTracksArtist & " (Y)" as string to end of bigTracklist
								set Yestrack to Yestrack + 1
							end repeat
							
						else
							
							copy (myLibTracks) & " by " & myLibTracksArtist & " (Y)" as string to end of bigTracklist
							set Yestrack to Yestrack + 1
						end if
					on error
						(* If no match in your library, an error will occure, so use it to get no match list*)
						copy (this_song) & " by " & this_artist & " (N)" to end of bigTracklist
					end try
					
				end repeat
				set Yestrack to Yestrack as string
				(* show results*)
				set yourSelection to choose from list bigTracklist with prompt Yestrack & " of " & trackcountStore & " Tracks found in you Library" with multiple selections allowed
				if yourSelection is not "" then
					set this_line to ""
					repeat with i from 1 to number of items in yourSelection
						set this_line to this_line & item i of yourSelection & return as string
					end repeat
					(* make a text doc, with items you chose *)
					tell application "TextEdit"
						activate
						make new document
						set the text of the front document to this_line
					end tell
				end if
			end tell
		end tell
	on error mssg
		if mssg contains "NSReceiverEvaluationScriptError: 4" then
			display dialog " Make sure you are in the iTunes Store and " & return & return & "viewing a song list, before you run this script."
		end if
	end try
end tell



*Edit
A line change in the code, see Stefank post for details

Anyone??

Should I have Put this in Code Exhange??

Hi Mark,

very nice script :slight_smile:
In my copy of iTunes the scroll area, which displays the iTS song list, is scroll area 2,
so I changed this line to get the right area reliably

tell window "iTunes" to tell outline 1 of (get 1st scroll area of splitter group 1 whose static texts of outline 1 is {})

the displayed result is a list of all songs regardless if there are in your library or not.
Only the prompt indicates the matched tracks. Is this intended?

Hi Stefan,

Thanks for the bit of code, I will update the original.

That was the results I wanted, There is a (N) for not in library and a (Y) for in library.
The Prompt should tell you Example: “11 of 13 Tracks found in you Library”
Showing the 13 tracks search for, and marked with the (N) and (Y), They will also be in order that matches the store display for side by side for comparison.

Do you think it needs changing?

This script just came in handy.
I was downloading 3 albums from the Store, when my iTunes crashed.
It was clear, some of the songs did not download… AHHHH

I had to bloody google, believe it or not to find out what to do because apple seem to have no obvious help in the support or iTunes sites.
Even then the online thing did not work as described, So I needed to send an email saying what was missing.

I ran the script on the iTunes Store list, and got the track names just like that. Selected them in the dialog,
And used the text output to paste into the email. Nice.

So hopefully Apple will give me the tracks I did not get.

I don’t know,
as a dumb user I expected only the matching tracks to be displayed :wink:

How about this, It give a choice of result displayed.

(* To get correct results when in the store the name column is second from left and artist   column is fourth from left 

example:

|	| Name  			| Time	| Artist			| Album					|
| 1	| Hollywood		| 5:08	| The Cranberries	| To the Faithul Departed	|
| 2	| salvation		| 2:23	| The Cranberries	| To the Faithul Departed	|
 *)


set AppleScript's text item delimiters to ""
tell application "System Events" to tell process "iTunes"
	
	try
		--tell window "iTunes" to tell scroll area 1 of splitter group 1 to tell outline 1
		tell window "iTunes" to tell outline 1 of (get 1st scroll area of splitter group 1 whose static texts of outline 1 is {})
			set SongList to {}
			set biglist to {}
			set bigTracklist to {}
			set Yestrack to 0
			set InLibList to {}
			set NotInLibList to {}
			
			(* get name and artist using GUI scripting from iTunes Store list*)
			set SongList to value of text field 1 of every row
			set ArtistList to value of text field 3 of every row
			set trackcountStore to (count of SongList) as string
			tell application "iTunes"
				
				repeat with i from 1 to number of items in SongList
					(* if  name  contains explicit, remove it*)
					set this_song to item i of SongList
					set this_artist to item i of ArtistList
					set oldDelims to AppleScript's text item delimiters
					set AppleScript's text item delimiters to {", explicit"}
					set this_song to text item 1 of this_song as string
					set AppleScript's text item delimiters to oldDelims
					try
						(* find matching song name and artist in your library*)
						set myLibTracks to (name of tracks of library playlist 1 whose name contains this_song and artist contains this_artist)
						set myLibTracksArtist to (artist of tracks of library playlist 1 whose name contains this_song and artist contains this_artist)
						
						set trackcount to count of myLibTracks
						(* there maybe more than one match in your library*)
						if trackcount is greater than 1 then
							repeat with i from 1 to number of items in myLibTracks
								copy (item i of myLibTracks) & " by " & item i of myLibTracksArtist & " (Y)" as string to end of bigTracklist
								set Yestrack to Yestrack + 1
							end repeat
							
						else
							
							copy (myLibTracks) & " by " & myLibTracksArtist & " (Y)" as string to end of bigTracklist
							set Yestrack to Yestrack + 1
						end if
					on error
						(* If no match in your library, an error will occure, so use it to get no match list*)
						copy (this_song) & " by " & this_artist & " (N)" to end of bigTracklist
					end try
					
				end repeat
				set Yestrack to Yestrack as string
				(* show results*)
				repeat with i from 1 to number of items in bigTracklist
					set this_lib_item to item i of bigTracklist
					if this_lib_item contains "(Y)" then
						copy this_lib_item to end of InLibList
					else
						copy this_lib_item to end of NotInLibList
					end if
				end repeat
				set libcount to (count of items in InLibList) as string
				set notlibcount to (count of items in NotInLibList) as string
				set mixedcount to (count of items in bigTracklist) as string
				log mixedcount
				display dialog "Show" buttons {mixedcount & " Mixed Results", libcount & " In Library", notlibcount & " Not in library"} default button 3
				set the button_pressed to the button returned of the result
				if the button_pressed is mixedcount & " Mixed Results" then
					set yourSelection to choose from list bigTracklist with prompt " Tracks Found or Not in you Library" with multiple selections allowed
				else if the button_pressed is libcount & " In Library" then
					set yourSelection to choose from list InLibList with prompt libcount & " out of " & trackcountStore & " Tracks search for  FOUND in you Library" with multiple selections allowed
					
				else
					set yourSelection to choose from list NotInLibList with prompt notlibcount & " out of " & trackcountStore & " Tracks search for NOT found in you Library" with multiple selections allowed
					
				end if
				
				if yourSelection is not "" then
					set this_line to ""
					repeat with i from 1 to number of items in yourSelection
						set this_line to this_line & item i of yourSelection & return as string
					end repeat
					(* make a text doc, with items you chose *)
					tell application "TextEdit"
						activate
						make new document
						set the text of the front document to this_line
					end tell
				end if
			end tell
		end tell
	on error mssg
		if mssg contains "NSReceiverEvaluationScriptError: 4" then
			display dialog " Make sure you are in the iTunes Store and " & return & return & "viewing a song list, before you run this script."
		end if
	end try
	
end tell



Great :slight_smile: