views:

663

answers:

1

I've created the following AppleScript for deleting all the selected tracks:

property okflag : false

-- check if iTunes is running 
tell application "Finder"
    if (get name of every process) contains "iTunes" then set okflag to true
end tell
if okflag then
    tell application "iTunes"
     if selection is not {} then
      repeat with this_track in selection
       try
        try
         set cla to class of this_track
         set floc to (get location of this_track)
         delete this_track
        on error error_message number error_number
         display alert error_message message ("Error number: ") & error_number & "."
        end try
        if cla is file track then
         my delete_the_file(floc)
        end if
       end try
      end repeat
     end if
    end tell
end if

to delete_the_file(floc)
    try
     -- tell application "Finder" to delete floc
     do shell script "mv " & quoted form of POSIX path of (floc as string) & " " & quoted form of POSIX path of (path to trash as string)
    on error
     display dialog "Track deleted, but could not be moved to trash" buttons {"Hmm"} default button 1 with icon 1
    end try
end delete_the_file

It works fine when I select a single item, but when I select more than one I get: "Can't get location of item 2 of selection" (error number -1728). I believe this is because by deleting a track, the script's index into the selection is corrupted.

I thought I'd try making my own list of tracks to be deleted first:

tell application "iTunes"
 if selection is not {} then
  set to_delete to {}
  repeat with this_track in selection
   try
    set cla to class of this_track
    set floc to (get location of this_track)
    if cla is file track then
     set pair to {this_track, floc}
     set to_delete to to_delete & pair
    end if
   end try
  end repeat
  repeat with pair in to_delete
   set the_track to item 1 of pair
   set floc to item 2 of pair
   delete the_track
   my delete_the_file(floc)
  end repeat
 end if
end tell

But then I get 'Can't get item 1 of item 1 of selection of application "iTunes".' I think the problem is "this_track" is not an object of class Track, but an item of a selection. How do I get the actual track object from the selection item?

If you don't see the solution, I'll welcome tips on debugging or any other suggestions.

+1  A: 

The variable this_track is a reference to an object specifier. You have to use the contents property to get the enclosed object specifier. The same is true for accessing the variable pair in the second loop. See the class reference section on the class reference in the AppleScript language guide.

Another problem exists in the way the list to_delete is being built. The statement set to_delete to to_delete & pair will not produce a list of pairs but a flat list. See the class reference section on the class list in the AppleScript language guide.

Here's a version of your second script, where these bugs have been removed:

tell application "iTunes"
    if selection is not {} then
     set to_delete to {}
     repeat with this_track in selection
      try
       set cla to class of this_track
       set floc to (get location of this_track)
       if cla is file track then
        set pair to {contents of this_track, floc}
        copy pair to end of to_delete
       end if
      end try
     end repeat
     repeat with pair in to_delete
      set the_track to item 1 of contents of pair
      set floc to item 2 of contents of pair
      delete the_track
      my delete_the_file(floc)
     end repeat
    end if
end tell
sakra
I made your change and got the error: 'Can't get item 1 of <<class cFIT>> id 41009 of <<class cUsP>> id 40953 of <<class cSrc>> id 40 of application "iTunes".'
Daryl Spitzer
Thank you for updating your answer. I guess I need to figure out how to debug AppleScript—I'll ask a question.
Daryl Spitzer