Lee_Dailey

Itunes_Disable-AlbumAutoRating

May 22nd, 2016 (edited)
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. file named - Itunes_Disable-AlbumAutoRating.ps1
  3. *****
  4.  
  5. since there is NO WAY to disable album auto-rating in itunes, this script takes
  6. advantage of the way ratings are represented.
  7.  
  8. - no stars    = 0
  9. - one star    = 20
  10. - two stars   = 40
  11. - three stars = 60
  12. - four stars  = 80
  13. - five stars  = 100
  14.  
  15. a side effect of the above is that a rating of 1 is treated as a rating of "no stars" in the
  16. rating and album-rating tests for smart playlists.
  17.  
  18. it also disables the album auto-rating function which only acts on items that have not
  19. been manually rated.
  20. *****
  21.  
  22. currently runs 1000 tracks in ~ 10 minutes if they all need to be changed.
  23. currently runs 1000 tracks in ~ 5 seconds if none need to be changed.
  24.  
  25. that seems to be fast enuf to auto-run weekly with task scheduler.
  26.  
  27. #>
  28.  
  29. # function list start
  30.  
  31. # set powershell window title
  32. function SetWindowTitle([String]$text)
  33.     {
  34.     $Host.UI.RawUI.WindowTitle = $text
  35.     }
  36.  
  37. # function list end
  38.  
  39.  
  40. $StartTime = Get-Date
  41.  
  42. SetWindowTitle($myinvocation.mycommand.name)
  43.  
  44. $ItunesAlreadyRunning = (Get-Process | Where-Object {$_.ProcessName -eq "iTunes"}) -match "iTunes"
  45.  
  46. $iTunesApp = New-Object -comObject iTunes.Application
  47. $iMainLibrary = $iTunesApp.LibraryPlaylist
  48. $iTracks = $iMainLibrary.Tracks
  49. $NumTracks = $iTracks.Count
  50.  
  51. # if you use AlbumRating, then swap these two items
  52. $UseAlbumRating = $False
  53. #$UseAlbumRating = $True
  54.  
  55. # if the album rating was manually set, then AlbumRatingKind = 0
  56. $AlbumRatingSetAutomatically = 1
  57.  
  58. # itunes no longer uses "1" to represent ONLY the audio file "kind".
  59. # - that also is used for books and possibly other file "kind" info.
  60. # - switched to using "KindAsString" instead of "Kind" value for media type test.
  61. # - possible values seem to be "AAC audio file", "Protected AAC audio file", and "MPEG audio file".
  62. # - they all seem to contain "audio file" and that is what is tested for.
  63. # - i say "seem to" because all of my tracks are MPEG files, so i can't test for other types.
  64. $AudioTrackType = "*audio file"
  65. $LogFile = -join ($Env:Temp, "\", $myinvocation.mycommand.name, "_", $StartTime.ToString("yyyy-MM-dd"), ".log")
  66.  
  67. # swap these two when you wanna test just a few tracks
  68. #$x = 10
  69. $x = $NumTracks
  70.  
  71. $ProcessedCount = 0
  72. $ChangedCount = 0
  73. while($x -gt 0)
  74.     {
  75.     $ProcessedCount ++
  76.     $iCurrentItem = $iTracks.Item($x)
  77.     Write-Output ("track index           = {0,6:N0} of {1,6:N0}" -f $x, $NumTracks)
  78.     if(($iCurrentItem.KindAsString -like $AudioTrackType) -and (
  79.         (($UseAlbumRating -eq $True) -and ($iCurrentItem.AlbumRatingKind -eq $AlbumRatingSetAutomatically)) -or (
  80.             ($UseAlbumRating -eq $False) -and ($iCurrentItem.AlbumRating -gt 1)))
  81.         )
  82.         {
  83.         $Location = $iCurrentItem.Location
  84.         $OldAlbumRating = $iCurrentItem.AlbumRating
  85.         $NewAlbumRating = $iCurrentItem.AlbumRating = 1
  86.         $ChangedCount ++
  87.         # show what was done
  88.         Write-Output ("OLD track AlbumRating = $OldAlbumRating")
  89.         Write-Output ("NEW track AlbumRating = $NewAlbumRating")
  90.         Write-Output ("track location        = $Location")
  91.         Write-Output ("")
  92.         }
  93.     $x --
  94.     }
  95.    
  96. # if itunes was NOT already running
  97. #   and itunes is NOT currently playing
  98. #   then we started it and we are not using it, so shut it down
  99. if((-not $ItunesAlreadyRunning) -and ($itunesApp.Playerstate -eq "0"))
  100.     {
  101.     $iTunesApp.Quit()
  102.     }
  103.  
  104.  
  105. $StopTime = Get-Date
  106. $TotalTime = $StopTime - $StartTime
  107.  
  108. Add-Content -path $LogFile -value ("") -PassThru
  109. Add-Content -path $LogFile -value ("started at = $StartTime") -PassThru
  110. Add-Content -path $LogFile -value ("stopped at = $StopTime") -PassThru
  111. Add-Content -path $LogFile -value ("total time = $($TotalTime.ToString())") -PassThru
  112. Add-Content -path $LogFile -value ("processed  = {0,6:N0}" -f $ProcessedCount) -PassThru
  113. Add-Content -path $LogFile -value ("changed    = {0,6:N0}" -f $ChangedCount) -PassThru
  114. Add-Content -path $LogFile -value ("log file   = $LogFile") -PassThru
  115. Add-Content -path $LogFile -value ("") -PassThru
  116.  
  117. # delay so the final output block can be read
  118. Write-Output ("Pausing for 30 seconds ...")
  119. Start-Sleep -Seconds 30
Add Comment
Please, Sign In to add comment