skreien Posted September 8, 2012 Share Posted September 8, 2012 (edited) Hi everyone, I've been an AutoIt user for 10 years, on and off. I've done tons of stuff with it, but I've never done anything with COM. As they say, need is the mother of invention, so now I'm trying to automate iTunes. Here's my situation: I use MythTV to record everything and transcode it into AppleTV format, which then drops it into iTunes. iTunes acts as the media manager. Unfortunately there's no way to auto-remove recordings that I've watched and don't want anymore. So I'm trying to write an AutoIt script that pulls the list of shows from a smart playlist, currently called test (the smart playlist is to make it easier to control what shows get removed), and deletes the files from the library and the disk. Here's what I have so far. It works down to the for statement. $objApp = ObjCreate("iTunes.Application") $colSources = $objApp.Sources $objSource = $colSources.ItemByName("Library") $colPlaylists = $objSource.Playlists $objPlaylist = $colPlaylists.ItemByName("Test") for $TRACK in $objPlaylist.Tracks $objPlaylist.LibraryPlaylist.Tracks.ItemByName($TRACK).Delete Next I've downloaded all the examples I can find but I am really stuck. I can script fairly well, but COM seems to be beyond me. I'd appreciate some help with getting this written and working. I don't post very often so I wasn't sure if I should put this in the COM forum, but since it's not AutoITX I decided to try here. Thanks! Edited September 8, 2012 by skreien Link to comment Share on other sites More sharing options...
FireFox Posted September 8, 2012 Share Posted September 8, 2012 Hi,I don't know if you have already tried, but this should help you : Br, FireFox. Link to comment Share on other sites More sharing options...
skreien Posted September 9, 2012 Author Share Posted September 9, 2012 (edited) Thanks I've been looking at it also, but the thread says it doesn't work on iTunes 10 so other than trying to figure out how to use COM I haven't relied on it for anything. Edited September 9, 2012 by skreien Link to comment Share on other sites More sharing options...
skreien Posted September 9, 2012 Author Share Posted September 9, 2012 OK, so I finally figured out how to get the track name. I've seen in several places track removal, so that's next. $objApp = ObjCreate("iTunes.Application") $colSources = $objApp.Sources $objSource = $colSources.ItemByName("Library") $colPlaylists = $objSource.Playlists $objPlaylist = $colPlaylists.ItemByName("Test") $TRACKNAMES=$objPlaylist.Tracks for $TRACK in $TRACKNAMES ;$objPlaylist.LibraryPlaylist.Tracks.ItemByName($TRACK.Name).Delete $TITLE=$TRACK.Name ;MsgBox(0,"title",$TITLE) $objSource.$TRACK.Delete Next Link to comment Share on other sites More sharing options...
skreien Posted September 9, 2012 Author Share Posted September 9, 2012 (edited) OK, I got 75% of what I want.... here is the code so far. It functions as I wanted, except I haven't figured out how to remove from disk yet. This is much farther in two days than I've gotten in 2 weeks the last time I tried to do something like this. It's a very good thing this doesn't delete from disk yet. The first time I ran it I accidentally remove my entire TV Show library. That's fixed now. Present code below. $ITUNESMUSICSOURCESTODELETE = $ITUNES.Sources $LIBRARYTODELETE = $ITUNESMUSICSOURCESTODELETE.ItemByName("Library") ; main library $PLAYLISTSTODELETE = $LIBRARYTODELETE.Playlists $PLAYLISTTODELETE= $PLAYLISTSTODELETE.ItemByName("WatchDelete") ; Playlist to look for titles of tv shows to delete in the main library $TRACKNAMESTODELETE=$PLAYLISTTODELETE.Tracks for $TRACKTODELETE in $TRACKNAMESTODELETE $TITLETODELETE=$TRACKTODELETE.Name ;MsgBox(0,"title to remove",$TITLE) ;$TRACK.Delete ; deletes track from playlist $ITUNESMUSICSOURCESMAINLIB = $ITUNES.Sources $LIBRARYMAINLIB = $ITUNESMUSICSOURCESMAINLIB.ItemByName("Library") ; main library $PLAYLISTSMAINLIB = $LIBRARYMAINLIB.Playlists $PLAYLISTMAINLIB = $PLAYLISTSMAINLIB.ItemByName("TV Shows") ; search the TV Shows list (all TV Shows in the library) for a title matching this one $TRACKNAMESMAINLIB=$PLAYLISTMAINLIB.Tracks for $TRACKMAINLIB in $TRACKNAMESMAINLIB $TITLEMAINLIB=$TRACKMAINLIB.Name if $TITLEMAINLIB = $TITLETODELETE Then $DELETERETURN=MsgBox(1,"Deleting title",$TITLETODELETE,5) if $DELETERETURN = 1 or $DELETERETURN=-1 Then MsgBox(0,"return",$DELETERETURN) $TRACKMAINLIB.Delete ; deletes track from library entirely ;$TRACKTODELETE.Delete ; deletes track from playlist only EndIf EndIf Next Next Edited September 9, 2012 by skreien Link to comment Share on other sites More sharing options...
kylomas Posted September 9, 2012 Share Posted September 9, 2012 (edited) skreien, Two things: 1 - use code tags to make your code easier to read. ... 2 - add a com eror handler. The following is an example of one. expandcollapse popup; ; ; #include <date.au3> #include <misc.au3> HotKeySet("{ESC}","ESC") local $oerror = ObjEvent("AutoIt.Error", "_DeBug") Local $Obj = ObjGet("winmgmts:{impersonationLevel=impersonate}!" & @ComputerName & "rootcimv2") Local $hObj = ObjCreate("WbemScripting.SWbemSink") If IsObj($Obj) And IsObj($hObj) Then ObjEvent($hObj, "SINK_") $Obj.ExecNotificationQueryAsync($hObj, "SELECT * FROM __InstanceCreationEvent WITHIN 0.1 WHERE TargetInstance ISA 'Win32_Process'") $Obj.ExecNotificationQueryAsync($hObj, "SELECT * FROM __InstanceDeletionEvent WITHIN 0.1 WHERE TargetInstance ISA 'Win32_Process'") EndIf Sleep(9999999999) Func SINK_OnObjectReady($OB) local $str,$owner,$ret Switch $OB.Path_.Class Case "__InstanceCreationEvent" $str &= $OB.TargetInstance.ProcessID & "`" $str &= $ob.targetinstance.name & "`" $str &= $ob.targetinstance.csname & "`" $ret = $ob.targetinstance.getowner($owner) $str &= $ob.targetinstance.creationdate & "`" $str &= $ob.targetinstance.parentprocessid & "`" $str &= $ob.targetinstance.executablepath & @cr consolewrite("+->> " & $str) $str = "" Case "__InstanceDeletionEvent" $str &= $OB.TargetInstance.ProcessID & "`" $str &= $ob.targetinstance.name & "`" $str &= _now() & "`" $str &= ($ob.targetinstance.kernelmodetime + $ob.targetinstance.usermodetime) / 10000000 & @cr consolewrite("+->> " & $str) $str = "" EndSwitch Return 1 EndFunc Func _Debug($oError) ConsoleWrite( _ "! ##################### AUTOIT OBJECT ERROR ######################################" & @CRLF & _ "! err.number is : " & @TAB & hex($oError.number,8) & @CRLF & _ "! err.scriptline is : " & @TAB & $oError.scriptline & @CRLF & _ "! err.windesc is : " & @TAB & $oError.windescription & @CRLF & _ "! err.desc is : " & @TAB & $oError.description & @CRLF & _ "! err.source is : " & @TAB & $oError.source & @CRLF & _ "! err.retcode is : " & @TAB & hex($oError.retcode,8) & @CRLF & _ "! ################################################################################" & @CRLF _ ) Return 0 EndFunc Func ESC() Exit(0) EndFunc Good Luck, kylomas edit: - sorry, I tried to give you an example of what code tags look like without using quotes. code tags = __CODEBOX_0__ edit2 - use the code buttons. I can't get this editor to NOT interpret the code tags Edited September 9, 2012 by kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
skreien Posted September 9, 2012 Author Share Posted September 9, 2012 Thanks kylomas, I'll take a look. I edited my previous post to put the code into code tags. Like I said, I don't post very much. Thanks for the tip. Link to comment Share on other sites More sharing options...
skreien Posted September 9, 2012 Author Share Posted September 9, 2012 After I put the kids (and the wife ) to bed last night I sat down for some serious work. Here's what I have now. It tracks down anything that was last played over seven days ago that's in a playlist called WatchDelete. It retrieves the file path from iTunes, removes it from the main TV Shows library, then removes the file from disk. Still a work in progress though. expandcollapse popup#include <Date.au3> #include <File.au3> $LOGFILE="C:deletelog.txt" _FileWriteLog($LOGFILE, "Attaching to iTunes") $ITUNES = ObjCreate("iTunes.Application") If Not IsObj($ITUNES) Then ;Notify user if user does not have iTunes installed on his computer MsgBox(0, "", "ERROR: Could not create iTunes object." & @CRLF & @CRLF & "Explanation:" & @CRLF & "iTunes, or some of it's components, are not installed on your computer." & @CRLF & "Now Exiting..") Exit EndIf if @error Then _FileWriteLog($LOGFILE, "ERROR: Cannot attach to iTunes, exiting") Exit endif $ITUNESMUSICSOURCESTODELETE = $ITUNES.Sources $LIBRARYTODELETE = $ITUNESMUSICSOURCESTODELETE.ItemByName("Library") ; main library $PLAYLISTSTODELETE = $LIBRARYTODELETE.Playlists _FileWriteLog($LOGFILE, "Getting contents of playlist WatchDelete") $PLAYLISTTODELETE= $PLAYLISTSTODELETE.ItemByName("WatchDelete") ; Playlist to look for titles of tv shows to delete in the main library $TRACKNAMESTODELETE=$PLAYLISTTODELETE.Tracks for $TRACKTODELETE in $TRACKNAMESTODELETE $TITLETODELETE=$TRACKTODELETE.Name ;MsgBox(0,"title to remove",$TITLE) ;$TRACK.Delete ; deletes track from playlist _FileWriteLog($LOGFILE, "Found candidate to remove with title: " & $TITLETODELETE) $ITUNESMUSICSOURCESMAINLIB = $ITUNES.Sources $LIBRARYMAINLIB = $ITUNESMUSICSOURCESMAINLIB.ItemByName("Library") ; main library $PLAYLISTSMAINLIB = $LIBRARYMAINLIB.Playlists $PLAYLISTMAINLIB = $PLAYLISTSMAINLIB.ItemByName("TV Shows") ; search the TV Shows list (all TV Shows in the library) for a title matching this one $TRACKNAMESMAINLIB=$PLAYLISTMAINLIB.Tracks _FileWriteLog($LOGFILE, "Searching main library for a title match") for $TRACKMAINLIB in $TRACKNAMESMAINLIB $TITLEMAINLIB=$TRACKMAINLIB.Name if $TITLEMAINLIB = $TITLETODELETE Then _FileWriteLog($LOGFILE, "Found matching title, getting other info") $LOCATIONMAINLIB=$TRACKMAINLIB.Location _FileWriteLog($LOGFILE, "File is in location " & $LOCATIONMAINLIB) $PLAYEDDATEMAINLIB=$TRACKMAINLIB.PlayedDate $YEAR=StringLeft($PLAYEDDATEMAINLIB,4) $MONTH=StringMid($PLAYEDDATEMAINLIB,5,2) $DAY=StringMid($PLAYEDDATEMAINLIB,7,2) _FileWriteLog($LOGFILE, $TITLEMAINLIB & " was last played " & $YEAR & "-" & $MONTH & "-" & $DAY) $DAYSOLD=_DateDiff("D", $YEAR & "-" & $MONTH & "-" & $DAY, _NowCalcDate()) _FileWriteLog($LOGFILE, "Last play was " & $DAYSOLD & " days ago") $DELETERETURN=MsgBox(1,"Deleting title",$TITLETODELETE,5) if $DELETERETURN = 1 or $DELETERETURN=-1 Then _FileWriteLog($LOGFILE, "Delete approved or timeout approved") ;MsgBox(0,"return",$LOCATIONMAINLIB) ;MsgBox(0,"return",$PLAYEDDATEMAINLIB) ;MsgBox(0,"date",$YEAR & "-" & $MONTH & "-" & $DAY) ;msgbox(0,"Daysold",$DAYSOLD) if $DAYSOLD > "7" Then _FileWriteLog($LOGFILE, "Last play was more than 7 days ago, proceeding with delete") _FileWriteLog($LOGFILE, "Removing from iTunes") ;$TRACKMAINLIB.Delete ; deletes track from library entirely ;$TRACKTODELETE.Delete ; deletes track from playlist only _FileWriteLog($LOGFILE, "Removing from disk") ;FileDelete("$LOCATIONMAINLIB" EndIf EndIf EndIf Next Next $ITUNES = 0 Link to comment Share on other sites More sharing options...
James Posted September 10, 2012 Share Posted September 10, 2012 I don't know if this is of any use, but is slightly newer than torels, but not as feature complete. Though it should set you in the right direction. Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
skreien Posted September 13, 2012 Author Share Posted September 13, 2012 Thanks James, I'll take a look. Mine does what I want it to, now I need to get an error handler put in. Right now when it runs out of objects in the playlist or the playlist is empty it crashes. Other than that it's working great. Link to comment Share on other sites More sharing options...
James Posted September 13, 2012 Share Posted September 13, 2012 COM errors and iTunes are always painful, but IIRC, mine handles them - so just use what you need. Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now