AppleScript Help Needed: Delete Watched TV Shows in iTunes

Discussion in 'Mac Programming' started by AustinMatherne, Feb 16, 2010.

  1. AustinMatherne macrumors newbie

    AustinMatherne

    Joined:
    Feb 16, 2010
    #1
    First off, I'm extremely new to AppleScripting, I've only read a few beginners guides and I'm just starting to understand how it all works. But you gotta start somewhere right?

    What I'm trying to create is an AppleScript that will delete any watched TV Show from both the iTunes database and the physical video files. I figure that if I create a smart playlist in iTunes that pulls in every TV Show with a play count greater than 0 it should make deleting the shows a bit easer.

    If anyone knows how to do this and could point me in the right direction, I would truly appreciate it.

    Thanks,
     
  2. AustinMatherne thread starter macrumors newbie

    AustinMatherne

    Joined:
    Feb 16, 2010
    #2
    I've got the script below which seems to work perfectly, but I'm stuck on adding an error check so that if it doesn't find anything to delete it won't display an error. If anyone knows how to do this, I'd truly appreciate the help.

    Code:
    tell application "iTunes"
     	set filesToDelete to location of (file tracks whose (video kind is TV show) and (unplayed is false))
     	delete (tracks whose (video kind is TV show) and (unplayed is false))
     end tell
     tell application "Finder"
     	repeat with theFile in filesToDelete
     		delete theFile
     	end repeat
     end tell
    Thanks,
     
  3. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #3
    What line is giving you the error? The delete lines? You might try something like this:

    Code:
    tell application "iTunes"
    	set filesToDelete to location of (file tracks whose (video kind is TV show) and (unplayed is false))
    	try
    		delete (tracks whose (video kind is TV show) and (unplayed is false))
    	on error errMsg number errNum
    		display dialog (errNum as string) & ":" & return & errMsg
    	end try
    end tell
    tell application "Finder"
    	repeat with theFile in filesToDelete
    		try
    			delete theFile
    		on error errMsg number errNum
    			display dialog (errNum as string) & ":" & return & errMsg
    		end try
    		
    	end repeat
    end tell
    
    You could leave out the "display dialogs" and the error will pass with no interaction. Or you could trap the error like this:

    Code:
    	try
    		delete (tracks whose (video kind is TV show) and (unplayed is false))
    	on error errMsg number errNum
    		if errNum ≠ 1 then -- assumes you want to ignore error 1
    			display dialog (errNum as string) & ":" & return & errMsg -- other errors reported
    		end if
    	end try
    
    mt
     

Share This Page