Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- file named - Itunes_Save-R-PC-SC.ps1
- what it does
- - reads rating, playedcount, skippedcount values from itunes
- - builds a new comment string with the current values in it
- - compares to existing comment and replaces it if different
- not currently testing in advance to see if SHOULD update comment field cuz ...
- - original version that did so was way too slow
- - building & then testing newcomment for changes against oldcomment was easier
- used string operator "-replace" instead of string method cuz ...
- - easier to get non-case-sensitive matches
- - more tolerant of errors [can do a -replace on a string with nothing to replace & gets no error]
- used "-match" anyway cuz ...
- - not testing for presence of to-be-replaced-string makes me uneasy
- - may want to switch to string methods if time would be saved
- currently runs 1000 tracks in ~ 5 minutes if they all need to be changed
- currently runs 1000 tracks in ~ 30 seconds if none need to be changed
- that seems to be fast enuf to auto-run daily with task scheduler
- #>
- # function list start
- # write to console/file/both
- function WriteTo($what, [String]$where, [String]$filename)
- {
- if($where -ne "console" -and $filename -eq "")
- {
- $filename = -join ($Env:Temp, "\", "PowerShell_LogFile_", (get-date).ToString("yyyy-MM-dd"), ".log")
- }
- switch($where.ToLower())
- {
- "console" {$what | Write-Host}
- "file" {Add-Content -path $filename -value $what}
- "both" {Add-Content -path $filename -value $what -passthru}
- default {$what | Write-Host}
- }
- }
- # trim spaces from each end and reduce multi-spaces to one space
- function TrimSpaces([String]$in)
- {
- ($in -replace "\s{2,}", " ").Trim()
- }
- # function list end
- $ItunesAlreadyRunning = (Get-Process | Where-Object {$_.ProcessName -eq "iTunes"}) -match "iTunes"
- $iTunesApp = New-Object -comObject iTunes.Application
- $iMainLibrary = $iTunesApp.LibraryPlaylist
- $iTracks = $iMainLibrary.Tracks
- $NumTracks = $iTracks.Count
- # itunes no longer uses "1" to represent ONLY the audio file "kind".
- # - that also is used for books and possibly other file "kind" info.
- # - switched to using "KindAsString" instead of "Kind" value for media type test.
- $AudioTrack = "MPEG audio file"
- $LogFile = -join ($Env:Temp, "\", $myinvocation.mycommand.name, "_", (get-date).ToString("yyyy-MM-dd"), ".log")
- $StartTime = Get-Date
- # swap these two when you wanna test just a few tracks
- $x = $NumTracks
- #$x = 10
- $ProcessedCount = 0
- $ChangedCount = 0
- while($x -gt 0)
- {
- $ProcessedCount ++
- $iCurrentItem = $iTracks.Item($x)
- if($iCurrentItem.KindAsString -eq $AudioTrack)
- {
- $Rating = $iCurrentItem.Rating
- $PlayedCount = $iCurrentItem.PlayedCount
- $SkippedCount = $iCurrentItem.SkippedCount
- $Location = $iCurrentItem.Location
- $NewComment = $OldComment = $iCurrentItem.Comment
- $ComRating = -join ("rating=", $Rating)
- $ComPlayedCount = -join ("playedcount=", $PlayedCount)
- $ComSkippedCount = -join ("skippedcount=", $SkippedCount)
- if($NewComment.length -gt 0)
- {
- # remove existing rating in the comment field
- if($NewComment -match "rating=\d{1,3}")
- {
- $NewComment = $NewComment -replace "rating=\d{1,3}", ""
- }
- # remove existing playedcount in the comment field
- if($NewComment -match "playedcount=\d{1,}")
- {
- $NewComment = $NewComment -replace "playedcount=\d{1,}", ""
- }
- # remove existing skippedcount in the comment field
- if($NewComment -match "skippedcount=\d{1,}")
- {
- $NewComment = $NewComment -replace "skippedcount=\d{1,}", ""
- }
- }
- # append current rating, playedcount, skippedcount to comment with a spacer space
- $NewComment = $NewComment, $ComRating, $ComPlayedCount, $ComSkippedCount -join " "
- # spaces clean up
- $NewComment = TrimSpaces $NewComment
- # write the new comment to itunes if it actually changed
- if($NewComment -ne $OldComment)
- {
- $iCurrentItem.Comment = $NewComment
- $ChangedCount ++
- }
- #show what was done
- "track index = $x"
- Write-Host ""
- "track rating = $Rating"
- "track playedcount = $PlayedCount"
- "track skippedcount = $SkippedCount"
- Write-Host ""
- "OLD track comment = $OldComment"
- "NEW track comment = $NewComment"
- Write-Host ("itunes track comment =", $iCurrentItem.Comment)
- "track location = $Location"
- Write-Host "******************************"
- Write-Host ""
- }
- $x --
- }
- # if itunes was NOT already running
- # and itunes is NOT currently playing
- # then we started it and we are not using it, so shut it down
- if((-not $ItunesAlreadyRunning) -and ($itunesApp.Playerstate -eq "0"))
- {
- $iTunesApp.Quit()
- }
- $StopTime = Get-Date
- $TotalTime = $StopTime - $StartTime
- WriteTo (-join ("started at = ", $StartTime)) "both" $LogFile
- WriteTo (-join ("stopped at = ", $StopTime)) "both" $LogFile
- WriteTo (-join ("total time = ", $TotalTime.ToString())) "both" $LogFile
- WriteTo (-join ("processed = ", $ProcessedCount)) "both" $LogFile
- WriteTo (-join ("changed = ", $ChangedCount)) "both" $LogFile
- WriteTo (-join ("log file = ", $LogFile)) "both" $LogFile
- WriteTo ("") "both" $LogFile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement