Categories: Misc / DotNet / Java / Coder / Linux / PHP Ask - La ask - La Answer

How to identify tracks in Library?

I am trying to write a script that deletes tracks in the Library. But I can't find a clean way to specify a track. In my script, I have a list that contains the database id's of tracks I want to delete, but the only way I can find to do that is with:

set theTrack to some track whose database id is (item x of theIDs)

But this is nasty, as it has to search the entire library to find, which takes quite a while when you have thousands of tracks. There must be a way to identify a track directly...this is a database after all.

TIA...
[571 byte] By [bogen2] at [2007-11-9 15:30:25]
# 1 Re: How to identify tracks in Library?
How are you getting the database IDs to begin with? It seems to me that if you're getting them from a selection or playlist, that you shouldn't have to loop through every track in the Library.

If you set a variable to the database id like so:

repeat with aTrack in theTracks
set dbID to database ID of aTrack
end repeat

where theTracks are a selection or playlist, then you could delete tracks two ways, like this:

set theTrack to (every track of library playlist 1 whose database ID is dbID)

or this:

delete theTrack
eustacescrubb at 2007-11-15 17:25:01 >
# 2 Re: How to identify tracks in Library?
If yuo are interested, check out my Delete Files ( http://applescript.plaidcow.net/iTunes/delete_files.html) script.
DeltaTee at 2007-11-15 17:26:02 >
# 3 Re: How to identify tracks in Library?
That is a good script; I was wondering why bogen2 flet like he needed a new delete script, when such good ones are already out there.
eustacescrubb at 2007-11-15 17:27:00 >
# 4 Re: How to identify tracks in Library?
If you like the script please email me a "text bite" to include on my website.
DeltaTee at 2007-11-15 17:28:05 >
# 5 Re: How to identify tracks in Library?
DeltaTee: That's a nice script, but you have run into the same problem that I have...I have quoted the problem code from your script below:

repeat with i from 1 to (count of tracks)
if the location of (track i) is trackLocation then
delete (track i)
exit repeat
end if
end repeat

You are checking each track in the library to see if its location is a match, then deleting it. I assumed that there would be a way to identify a track by its id...guess not.

eustacescrubb: First, this script doesn't do what I need exactly. And secondly, I want to learn more about itunes scripting, so writing scripts would seem to be a reasonable way to meet this goal.
bogen2 at 2007-11-15 17:29:01 >
# 6 Re: How to identify tracks in Library?
Originally posted by bogen2
set theTrack to some track whose database id is (item x of theIDs)

I can't get this code to work.

To delete a track in iTunes you must have a reference to that track. If you want to remove that track from the library (and hence all playlists) you must have a reference to that track in the Library. Deleting a reference that points to a playlist will only remove that one instance from that playlist.

That being said, if you have a reference to a track in the library, simply use the command delete theTrack to remove it from the library and all playlists.

iTunes does something special when you are in a playlist and press Option-Delete: it removes not only the selected track from the playlist, but also from the library. Now, using GUI scripting you can tell iTunes to do a Option Delete:

tell application "System Events"
tell process "iTunes"
keystroke (ASCII character 8) using option down
end tell
end tell

I hope that this helps some. I am a bit under the weather today.
DeltaTee at 2007-11-15 17:30:11 >
# 7 Re: How to identify tracks in Library?
The newest version of my script to delete the selected files.

on main()
tell application "iTunes"
activate
set theTracks to (get the selection of browser window 1)

repeat with theTrack in theTracks
set trackLocation to location of theTrack

-- Now actually remove the file from the Finder
if trackLocation is not missing value then
tell application "Finder"
delete trackLocation
end tell
end if
end repeat --with theTrack in theTracks
end tell

tell application "System Events"
tell process "iTunes"
keystroke (ASCII character 8) using option down
end tell
end tell
end main
DeltaTee at 2007-11-15 17:31:04 >
# 8 Re: How to identify tracks in Library?
You don't need a track's location to delete it from iTunes. eustacescrubb's database id example is perfectly valid. When you delete a track from the "Library" it will be removed completely from iTunes.

This will delete a single selected track completely from iTunes (CAREFUL, there is no warning):

tell application "iTunes"
set lib to a reference to library playlist 1
set t to get item 1 of selection
delete (every track of lib whose database ID is (get t's database ID))
end tell

You only need location if you wish to delete the file from the hard drive via the Finder or shell scripting ("rm " & (quoted form of POSIX path of (location of t as string))).

You can get the database id for every track by:

tell application "iTunes"
set lib to a reference to library playlist 1
set allDBIDs to (get database ID of file tracks of lib)
end tell

You could just as easily used selection instead of library playlist 1. It should work pretty quickly due to the "a reference to" bit. Of course, if you run it in Script Editor it will take a while.
Doug Adams at 2007-11-15 17:32:06 >
# 9 Re: How to identify tracks in Library?
ok... so what's missing from delta tee's method is a way to select a list of tracks (that you have as a variable). how to you turn a selection into a list?

and would that method work if: a) iTunes were in the background, b) iTunes were minimized?
WoodenBrain at 2007-11-15 17:33:05 >
# 10 Re: How to identify tracks in Library?
I know of no way to tell iTunes to set the selection to a list of tracks. You may be able to use a select command to select a single track. Another way (that I really would work) would be to play the track and then use Command-L (but iTunes would need to be in the front).
DeltaTee at 2007-11-15 17:34:06 >
# 11 Re: How to identify tracks in Library?
ok, after turning to the experts, best to DYI. ;-) just kidding you guys are the sh*t.

it's sad that you can't delete a list of files, because each removal from the library takes a long time. but searching the library for a database id match also takes a really long time.

so i came up with a new method. benchmark: my method (on my library, which as i said is very huge): about 1 or 2 seconds. old method: about 1 minute.

Here's the test script. select a track or tracks on a playlist that's not your library and run from script editor.

property mlimit : 0
-- set mlimit to 0 for the new method; 99 to test "old" method

tell application "iTunes"
set trl to selection
set tr to item 1 of trl
set dbi to database ID of tr
set sstr to name of tr & " " & artist of tr
set matchtracks to search library playlist 1 for sstr
set cmt to count matchtracks
if cmt > mlimit then
repeat with i from 1 to cmt
if database ID of (item i of matchtracks) = dbi then
set matchtr to item i of matchtracks
exit repeat
end if
end repeat
else
-- should work but use old method
set matchtr to (some track of library playlist 1 whose database ID is dbi)
end if
set result to {tr, matchtr}
end tell
WoodenBrain at 2007-11-15 17:35:15 >
# 12 Re: How to identify tracks in Library?
This appears to be the secret (sicne I've neaver heard of this command before):
set matchtracks to search library playlist 1 for sstr

Is this a new command?
DeltaTee at 2007-11-15 17:36:14 >
# 13 Re: How to identify tracks in Library?
"search" has been available since iTunes 4.5 ( http://www.dougscripts.com/itunes/itinfo/itunes45info.php) ;)
Doug Adams at 2007-11-15 17:37:17 >
# 14 Re: How to identify tracks in Library?
so what do you all think?
WoodenBrain at 2007-11-15 17:38:12 >
# 15 Re: How to identify tracks in Library?
About what?

Are you the same WoodenBrain that helped Doug on scripts when I was first getting started a couple of years ago?
DeltaTee at 2007-11-15 17:39:15 >
# 16 Re: How to identify tracks in Library?
DT: WoodenBrain and I have been writing scripts since SoundJam in the last century. He want to know what you think of his script.
Doug Adams at 2007-11-15 17:40:19 >
# 17 Re: How to identify tracks in Library?
Originally posted by DeltaTee
About what?

Are you the same WoodenBrain that helped Doug on scripts when I was first getting started a couple of years ago?

yep i'm the one. never made it over to these forums when doug moved to the lounge--quite busy with real work. but i got obsessed now with a very large bit of disk space that iTunes considers duplicates and i wanted to see which actually were.

so far, going up to the letter H in song names that iTunes considers duplicates in my library (for a total of I won't say how many gigabytes ;-), my iDupe has identified about 500 MB of actually bona fide dupes.
WoodenBrain at 2007-11-15 17:41:16 >
[an error occurred while processing this directive]
[an error occurred while processing this directive]