Advertisement
Lee_Dailey

Itunes_Save-R-PC-SC.ps1

Feb 25th, 2016
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. file named - Itunes_Save-R-PC-SC.ps1
  3.  
  4. what it does
  5. - reads rating, playedcount, skippedcount values from itunes
  6. - builds a new comment string with the current values in it
  7. - compares to existing comment and replaces it if different
  8.  
  9. not currently testing in advance to see if SHOULD update comment field cuz ...
  10. - original version that did so was way too slow
  11. - building & then testing newcomment for changes against oldcomment was easier
  12.  
  13. used string operator "-replace" instead of string method cuz ...
  14. - easier to get non-case-sensitive matches
  15. - more tolerant of errors [can do a -replace on a string with nothing to replace & gets no error]
  16.  
  17. used "-match" anyway cuz ...
  18. - not testing for presence of to-be-replaced-string makes me uneasy
  19. - may want to switch to string methods if time would be saved
  20.  
  21. currently runs 1000 tracks in ~ 5 minutes if they all need to be changed
  22. currently runs 1000 tracks in ~ 30 seconds if none need to be changed
  23.  
  24. that seems to be fast enuf to auto-run daily with task scheduler
  25.  
  26. #>
  27.  
  28. # function list start
  29.  
  30. # write to console/file/both
  31. function WriteTo($what, [String]$where, [String]$filename)
  32.     {
  33.     if($where -ne "console" -and $filename -eq "")
  34.         {
  35.         $filename = -join ($Env:Temp, "\", "PowerShell_LogFile_", (get-date).ToString("yyyy-MM-dd"), ".log")
  36.         }
  37.     switch($where.ToLower())
  38.         {
  39.         "console" {$what | Write-Host}
  40.         "file"    {Add-Content -path $filename -value $what}
  41.         "both"    {Add-Content -path $filename -value $what -passthru}
  42.         default   {$what | Write-Host}
  43.         }
  44.     }
  45.  
  46. # trim spaces from each end and reduce multi-spaces to one space
  47. function TrimSpaces([String]$in)
  48.     {
  49.     ($in -replace "\s{2,}", " ").Trim()
  50.     }
  51.  
  52. # function list end
  53.  
  54. $ItunesAlreadyRunning = (Get-Process | Where-Object {$_.ProcessName -eq "iTunes"}) -match "iTunes"
  55.  
  56. $iTunesApp = New-Object -comObject iTunes.Application
  57. $iMainLibrary = $iTunesApp.LibraryPlaylist
  58. $iTracks = $iMainLibrary.Tracks
  59.  
  60. $NumTracks = $iTracks.Count
  61. # itunes no longer uses "1" to represent ONLY the audio file "kind".
  62. # - that also is used for books and possibly other file "kind" info.
  63. # - switched to using "KindAsString" instead of "Kind" value for media type test.
  64. $AudioTrack = "MPEG audio file"
  65. $LogFile = -join ($Env:Temp, "\", $myinvocation.mycommand.name, "_", (get-date).ToString("yyyy-MM-dd"), ".log")
  66. $StartTime = Get-Date
  67.  
  68. # swap these two when you wanna test just a few tracks
  69. $x = $NumTracks
  70. #$x = 10
  71.  
  72. $ProcessedCount = 0
  73. $ChangedCount = 0
  74. while($x -gt 0)
  75.     {
  76.     $ProcessedCount ++
  77.     $iCurrentItem = $iTracks.Item($x)
  78.     if($iCurrentItem.KindAsString -eq $AudioTrack)
  79.         {
  80.         $Rating = $iCurrentItem.Rating
  81.         $PlayedCount = $iCurrentItem.PlayedCount
  82.         $SkippedCount = $iCurrentItem.SkippedCount
  83.         $Location = $iCurrentItem.Location
  84.         $NewComment = $OldComment = $iCurrentItem.Comment
  85.         $ComRating = -join ("rating=", $Rating)
  86.         $ComPlayedCount = -join ("playedcount=", $PlayedCount)
  87.         $ComSkippedCount = -join ("skippedcount=", $SkippedCount)
  88.         if($NewComment.length -gt 0)
  89.             {
  90.             # remove existing rating in the comment field
  91.             if($NewComment -match "rating=\d{1,3}")
  92.                 {
  93.                 $NewComment = $NewComment -replace "rating=\d{1,3}", ""
  94.                 }
  95.             # remove existing playedcount in the comment field        
  96.             if($NewComment -match "playedcount=\d{1,}")
  97.                 {
  98.                 $NewComment = $NewComment -replace "playedcount=\d{1,}", ""
  99.                 }
  100.             # remove existing skippedcount in the comment field        
  101.             if($NewComment -match "skippedcount=\d{1,}")
  102.                 {
  103.                 $NewComment = $NewComment -replace "skippedcount=\d{1,}", ""
  104.                 }
  105.             }    
  106.         # append current rating, playedcount, skippedcount to comment with a spacer space
  107.         $NewComment = $NewComment, $ComRating, $ComPlayedCount, $ComSkippedCount -join " "
  108.         # spaces clean up
  109.         $NewComment = TrimSpaces $NewComment
  110.         # write the new comment to itunes if it actually changed
  111.         if($NewComment -ne $OldComment)
  112.             {
  113.             $iCurrentItem.Comment = $NewComment
  114.             $ChangedCount ++
  115.             }
  116.         #show what was done
  117.         "track index            = $x"
  118.         Write-Host ""
  119.         "track rating           = $Rating"
  120.         "track playedcount      = $PlayedCount"
  121.         "track skippedcount     = $SkippedCount"
  122.         Write-Host ""
  123.         "OLD track comment      = $OldComment"
  124.         "NEW track comment      = $NewComment"
  125.         Write-Host ("itunes track comment   =", $iCurrentItem.Comment)
  126.         "track location         = $Location"
  127.         Write-Host "******************************"
  128.         Write-Host ""
  129.         }
  130.     $x --
  131.     }
  132.    
  133. # if itunes was NOT already running
  134. #   and itunes is NOT currently playing
  135. #   then we started it and we are not using it, so shut it down
  136. if((-not $ItunesAlreadyRunning) -and ($itunesApp.Playerstate -eq "0"))
  137.     {
  138.     $iTunesApp.Quit()
  139.     }
  140.  
  141. $StopTime = Get-Date
  142. $TotalTime = $StopTime - $StartTime
  143.  
  144. WriteTo (-join ("started at = ", $StartTime)) "both" $LogFile
  145. WriteTo (-join ("stopped at = ", $StopTime)) "both" $LogFile
  146. WriteTo (-join ("total time = ", $TotalTime.ToString())) "both" $LogFile
  147. WriteTo (-join ("processed  = ", $ProcessedCount)) "both" $LogFile
  148. WriteTo (-join ("changed    = ", $ChangedCount)) "both" $LogFile
  149. WriteTo (-join ("log file   = ", $LogFile)) "both" $LogFile
  150. WriteTo ("") "both" $LogFile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement