Creating a small AppleScript for iTunes

GO TO ADMIN PANEL > ADD-ONS AND INSTALL VERTIFORO SIDEBAR TO SEE FORUMS AND SIDEBAR

jowie

New member
Joined
Jan 21, 2005
Messages
13
Points
0
Age
48
Location
London
Website
www.jowie.net
Hi there,

I recently had problems with my iTunes Library which I've (kind of) sorted now, however there are several entries in my Library which cannot find their position within my new folder, even though the files are there (they are looking for an old HD that broke down).

So... I would like to create a script to get round this. I have seen a script on Doug's site that allows you to delete all the "!" files, but I don't want to do that, I want to relocate them instead.

AT ITS MOST SIMPLE:
I would like the script to do what I am doing by hand... Going through the main library in iTunes and clicking on each tune... Each time it cannot find a file it automatically throws up a dialog box and I go hunting for it. So a script that just loops through all of my tunes, clicking on them would work for me.

AT ITS MOST COMPLEX:
Even better would be to create an array of all these missing files (such as Doug's AppleScript already does), but instead of deleting them, edits their pathname to remove the old HD name and insert the new one... That way they will find the file in the correct place on my new HD.

Are either of these possible? Especially the last one, if I edit the AppleScript that already exists on Doug's website? I am a programmer but have not touched AppleScript so any help would be appreciated, thanks!:D
 

toothpaste

New member
Joined
May 22, 2004
Messages
3,585
Points
0
Location
New York City
Doug is actually a member at this forum, so your best bet would be to contact Doug. I know this is not the kind of answer you might be looking for, but I'm not a programmer so I don't know how to accomplish what you want. But Doug might very well assist you.

Good luck.
 

S2_Mac

New member
Joined
Oct 24, 2006
Messages
4,878
Points
0
Location
About 3 feet in front of the monitor
Check the bottom of the home page at DougScripts.com or any of his documentation (like, for the Super Remove Dead Tracks script ;-). (Overly cautious of me, p'bly, but I hate giving harvesters any help.)

The skeleton for such a script could look like this (working example):
Code:
tell application "iTunes"
  set mainLibrary to library playlist 1
  set totalTracks to count of file tracks of mainLibrary
  
  set oldfi to fixed indexing
  set fixed indexing to true
  
  with timeout of 30000 seconds
    repeat with t from totalTracks to 26 by -1
      try
        set this_track to file track t of mainLibrary
        if this_track's location is not missing value then
          set new_file to (choose file with prompt "Locate \"" & name of this_track & "\"...")
          add new_file
          delete this_track
        end if
      on error
        set fixed indexing to oldfi
        error number -128
      end try
    end repeat
  end timeout
  
  set fixed indexing to oldfi
end tell
where:
asking for a file track's "location" property is equivalent to click-select/Get Info;
and "add" tells iTunes to add a track entry for the specified path;
and "delete" removes the track whose file couldn't be found.
(The location prop is read-only; you can't just set it and be done)

(Of course, this doesn't do any niceties like including the "new" track in any playlists which might have contained the "old" track....)
 
Last edited:

jowie

New member
Joined
Jan 21, 2005
Messages
13
Points
0
Age
48
Location
London
Website
www.jowie.net
Hi S2_mac,

Thanks for your help! For some reason I never received the email telling me someone had replied to my thread, so I gave it a go myself last night. After a bit of fiddling I had something close to what you are suggesting... The only unfortunate thing about your way is that it removes all the current data for the missing tracks and starts again... I'm trying (hopefully) to just point the current tracks at the new positions. I tried setting the old track's locations:

Code:
if t_oTrack's location is missing value then
	tell t_oTrack to set {nom, art, alb, knd} to {name, artist, album, kind}
	
	if nom is "" then set nom to "Unknown Song Title"
	if art is "" then set art to "Unknown Artist"
	if alb is "" then set alb to "Unknown Album"
	if knd contains "MPEG" then set knd to "mp3"
	if knd contains "Protected" then set knd to "m4p"
	if knd contains "AAC" then set knd to "m4a"
	set t_sFilename to ("Hybrid:iTunes:iTunes Music:" & art & ":" & alb & ":" & nom & "." & knd & return) as text
	
	-- add to text file
	my fAddToFile(t_sFilename)

	-- this is the bit that didn't work because it's read-only :(
	set location of t_oTrack to t_sFilename
	
	set _nTracksToRetrieve to _nTracksToRetrieve + 1
end if
It's unfortunate that this won't work because I don't really want to delete the info that's already there. To be honest I can add all the missing ones again from scratch, just by dragging the iTunes Music folder onto the iTunes icon in the dock and waiting as it imports everything it doesn't already have... But I'd rather keep that as a last-ditch attempt.

So if setting its location isn't going to work, I guess I'd have to try the simpler route of having a script which just selects and tries to play any file with a missing value (basically a macro for what I've been doing so far)... However I did this and it didn't work:


Code:
if t_oTrack's location is missing value then
	play t_oTrack
	
	-- add to text file
	my fAddToFile(t_sFilename)
	
	set _nTracksToRetrieve to _nTracksToRetrieve + 1
end if
What am I doing wrong here? It produced my log file showing me the tunes that were missing, but didn't try playing (and therefore bringing up the dialog) for each missing tune... If there's no way of doing this either then I guess I'll have to create a script which puts all the missing files into a playlist which I can then go through manually.

Any thoughts?

Thanks! :D
 

jowie

New member
Joined
Jan 21, 2005
Messages
13
Points
0
Age
48
Location
London
Website
www.jowie.net
Boo...

I just tried getting it to add missing tracks to a new playlist... But it won't allow me to add any tracks to playlists with missing values! *sob*

Am I now lumbered with one option, to do it manually without AppleScript? :(
 

S2_Mac

New member
Joined
Oct 24, 2006
Messages
4,878
Points
0
Location
About 3 feet in front of the monitor
Yeah, that sucks. I think there's a workaround, though...

In the Source column, select the "Music" playlist to display all your toonz; now select all those tracks (like, Cmd-A after putting focus on the big list). Now select "Make playlist from selection" from the file menu; voila! Even the dead tracks show up in the new playlist.

Now run something like this (hardcoded; I named my playlist "workaround")
Code:
tell application "iTunes"
  set dest_playlist_name to "workaround"
  set dest_playlist to playlist dest_playlist_name
  set view of front browser window to dest_playlist
  set totalTracks to count of file tracks of dest_playlist
  set ofi to fixed indexing
  
  set fixed indexing to true
  with timeout of 3000 seconds
    repeat with i from totalTracks to 1 by -1
      set file_path to location of track i of playlist "Workaround"
      if file_path is not missing value then delete track i of playlist dest_playlist_name
    end repeat
  end timeout
  set fixed indexing to ofi
  
  display dialog "Done"
end tell
 
Top