by MeMeMe » Thu Jan 05, 2006 1:38 pm
Here's the code I used.
This file was named IncrPlayCntr.vbs
Code: Select all
' increments play counter
' Source: http://www.mediamonkey.com/forum/viewtopic.php?t=1461
' modified to work with a non-English date format and to also update the "Played" table.
Option Explicit
Sub IncrPlayCntr
' Define variables
Dim list, itm, i
' Get list of selected tracks from MediaMonkey
Set list = SDB.SelectedSongList
If list.count=0 Then
Set list = SDB.AllVisibleSongList
End If
' Process all selected tracks
For i=0 To list.count-1
Set itm = list.Item(i)
'must use sql because itm.UpdateDB does not update the PlayCounter property
SDB.database.execSQL("UPDATE Songs SET PlayCounter=" & (itm.PlayCounter + 1) & " WHERE Id=" & itm.songID)
SDB.database.execSQL("UPDATE Songs SET LastTimePlayed='" & Now() & "' WHERE Id=" & itm.songId)
' there is another DB table that stores when the song was played,
' so in order to stay consistent, we update this, too.
' This table is used for the "50 last played songs" playlist
SDB.database.execSQL("INSERT INTO Played (IdSong, PlayDate) VALUES (" & itm.songId & ", '" & Now() & "')")
'refreshes screen
itm.playcounter = itm.playcounter + 1
itm.UpdateDB
Next
End Sub
This is PlayCounter.vbs
Code: Select all
Sub SetPlayCounter
' Define variables
Dim list, itm, i, newPlayCounter, mb, progress
'Get a new value for the Play Counter from the user
newPlayCounter = InputBox("Enter the new value for the Played field", "Set Play Counter")
'If Canceled, exit
If newPlayCounter = "" Then
Exit Sub
End If
'Check that the text entered is a valid parameter. Inform user if it isn't.
If Not IsNumeric(newPlayCounter) Then
mb = MsgBox("You did not enter a number. Please try again.",0,"Error")
Exit Sub
ElseIf newPlayCounter < 0 Then
mb = MsgBox("Only positive numbers or 0 are allowed. Please try again.",0,"Error")
Exit Sub
End If
' Get list of selected tracks from MediaMonkey
Set list = SDB.SelectedSongList
If list.count=0 Then
Set list = SDB.AllVisibleSongList
End If
'No songs selected?
If list.count = 0 Then
mb = MsgBox("No songs were selected. Please select some songs and try again",0,"Error")
Exit Sub
End If
'Set up progress
Set Progress = SDB.Progress
Progress.Text = "Writing New Play Counters..."
Progress.MaxValue = list.count
'Process all selected tracks
For i=0 To list.count-1
Set itm = list.Item(i)
'Set the Play Counter
itm.PlayCounter = newPlayCounter
itm.UpdateDB
Progress.value = i+1
If Progress.terminate Then
Exit For
End if
Next
Set Progress = nothing
End Sub
Sub ResetPlayCounter
'This is drastic, so require confirmation from user
Dim yesimsure
yesimsure = InputBox("Are you sure you want to reset the play counter for all tracks in the Library? Write YES to proceed. Note: You will have to refresh the view (F5) in order to see the changes.", "Reset Play Counter")
If yesimsure <> "YES" Then
Exit Sub
End If
'Reset the counter...
SDB.Database.ExecSQL("UPDATE SONGS SET PLAYCOUNTER = 0 WHERE TRUE=TRUE")
End Sub
Sub RestorePlayCounter
'This is drastic, so require confirmation from user
Dim yesimsure
yesimsure = InputBox("Are you sure you want to restore the play counter to the number of times each track was played in MediaMonkey? Write YES to proceed. Note: You will have to refresh the view (F5) in order to see the changes.", "Restore Play Counter")
If yesimsure <> "YES" Then
Exit Sub
End If
Dim Str1, Str2, Str3, Str4, Str5
'Create a temporary table which stores song IDs and the number of times each song was played
Str1 = "SELECT Songs.ID, timesPlayed INTO updateCounter "
Str2 = "FROM Songs, [SELECT Played.idSong as SID, Count(Played.idSong) AS timesPlayed FROM Played GROUP BY Played.idSong]. AS subq "
Str3 = "WHERE Songs.Id =subq.SID"
SDB.Database.ExecSQL(Str1 & Str2 & Str3)
'Use the table just created to update the playCounter field in the songs table
Str4 = "UPDATE Songs, updateCounter SET songs.playcounter = updateCounter.timesPlayed "
Str5= "WHERE songs.id=updateCounter.id"
SDB.Database.ExecSQL(Str4 & Str5)
'Delete temporary table
SDB.Database.ExecSQL("DROP TABLE updateCounter")
End Sub
Function Min(x, y)
If x<y Then
Min = x
Else
Min = y
End If
End Function
Function Max(x, y)
If x>y Then
Max = x
Else
Max = y
End If
End Function
Sub SetPlayCounterAdvanced
' Define variables
Dim list, itm, i, userInput, newC , mb, progress, C
'Get a new value for the Play Counter from the user
userInput = InputBox("Enter a formula for the new play counter. Use C to refer to the current value. Examples: 0, C+1, Max(1,C).", "Set Play Counter (Advanced)")
'If Canceled, exit
If userInput = "" Then
Exit Sub
End If
' Get list of selected tracks from MediaMonkey
Set list = SDB.SelectedSongList
If list.count=0 Then
Set list = SDB.AllVisibleSongList
End If
'No songs selected?
If list.count = 0 Then
mb = MsgBox("No songs were selected. Please select some songs and try again",0,"Error")
Exit Sub
End If
'Set up progress
Set Progress = SDB.Progress
Progress.Text = "Writing New Play Counters..."
Progress.MaxValue = list.count
'Process all selected tracks
For i=0 To list.count-1
Set itm = list.Item(i)
C = itm.playCounter
'Evaluate user input
newC = Eval(userInput)
'Set the Play Counter
itm.PlayCounter = newC
itm.UpdateDB
Progress.value = i+1
If Progress.terminate Then
Exit For
End if
Next
Set Progress = nothing
End Sub
And I added these to the script.ini file:
Code: Select all
[SetPlayCounterAdvanced]
FileName=PlayCounter.vbs
ProcName=SetPlayCounterAdvanced
Order=20
DisplayName=Set Play Counter (Advanced)
Description=Modify the play counter by setting it to a numerical value or a formula depending on the current value
Language=VBScript
ScriptType=0
[ChangePlayCounter]
FileName=PlayCounter.vbs
ProcName=ChangePlayCounter
Order=20
DisplayName=Change Play Counter
Description=Change the play counter of the selected files
Language=VBScript
ScriptType=0
[ResetPlayCounter]
FileName=PlayCounter.vbs
ProcName=ResetPlayCounter
Order=21
DisplayName=Reset Play Counter
Description=Reset the play counter of all files in the library
Language=VBScript
ScriptType=0
[IncrementPlayCounter]
FileName=IncrPlayCntr.vbs
ProcName=IncrPlayCntr
Order=20
DisplayName=Increment Play Counter
Description=Increment the play counter by 1
Language=VBScript
ScriptType=0
Here's the code I used.
This file was named IncrPlayCntr.vbs
[code]
' increments play counter
' Source: http://www.mediamonkey.com/forum/viewtopic.php?t=1461
' modified to work with a non-English date format and to also update the "Played" table.
Option Explicit
Sub IncrPlayCntr
' Define variables
Dim list, itm, i
' Get list of selected tracks from MediaMonkey
Set list = SDB.SelectedSongList
If list.count=0 Then
Set list = SDB.AllVisibleSongList
End If
' Process all selected tracks
For i=0 To list.count-1
Set itm = list.Item(i)
'must use sql because itm.UpdateDB does not update the PlayCounter property
SDB.database.execSQL("UPDATE Songs SET PlayCounter=" & (itm.PlayCounter + 1) & " WHERE Id=" & itm.songID)
SDB.database.execSQL("UPDATE Songs SET LastTimePlayed='" & Now() & "' WHERE Id=" & itm.songId)
' there is another DB table that stores when the song was played,
' so in order to stay consistent, we update this, too.
' This table is used for the "50 last played songs" playlist
SDB.database.execSQL("INSERT INTO Played (IdSong, PlayDate) VALUES (" & itm.songId & ", '" & Now() & "')")
'refreshes screen
itm.playcounter = itm.playcounter + 1
itm.UpdateDB
Next
End Sub
[/code]
This is PlayCounter.vbs
[code]
Sub SetPlayCounter
' Define variables
Dim list, itm, i, newPlayCounter, mb, progress
'Get a new value for the Play Counter from the user
newPlayCounter = InputBox("Enter the new value for the Played field", "Set Play Counter")
'If Canceled, exit
If newPlayCounter = "" Then
Exit Sub
End If
'Check that the text entered is a valid parameter. Inform user if it isn't.
If Not IsNumeric(newPlayCounter) Then
mb = MsgBox("You did not enter a number. Please try again.",0,"Error")
Exit Sub
ElseIf newPlayCounter < 0 Then
mb = MsgBox("Only positive numbers or 0 are allowed. Please try again.",0,"Error")
Exit Sub
End If
' Get list of selected tracks from MediaMonkey
Set list = SDB.SelectedSongList
If list.count=0 Then
Set list = SDB.AllVisibleSongList
End If
'No songs selected?
If list.count = 0 Then
mb = MsgBox("No songs were selected. Please select some songs and try again",0,"Error")
Exit Sub
End If
'Set up progress
Set Progress = SDB.Progress
Progress.Text = "Writing New Play Counters..."
Progress.MaxValue = list.count
'Process all selected tracks
For i=0 To list.count-1
Set itm = list.Item(i)
'Set the Play Counter
itm.PlayCounter = newPlayCounter
itm.UpdateDB
Progress.value = i+1
If Progress.terminate Then
Exit For
End if
Next
Set Progress = nothing
End Sub
Sub ResetPlayCounter
'This is drastic, so require confirmation from user
Dim yesimsure
yesimsure = InputBox("Are you sure you want to reset the play counter for all tracks in the Library? Write YES to proceed. Note: You will have to refresh the view (F5) in order to see the changes.", "Reset Play Counter")
If yesimsure <> "YES" Then
Exit Sub
End If
'Reset the counter...
SDB.Database.ExecSQL("UPDATE SONGS SET PLAYCOUNTER = 0 WHERE TRUE=TRUE")
End Sub
Sub RestorePlayCounter
'This is drastic, so require confirmation from user
Dim yesimsure
yesimsure = InputBox("Are you sure you want to restore the play counter to the number of times each track was played in MediaMonkey? Write YES to proceed. Note: You will have to refresh the view (F5) in order to see the changes.", "Restore Play Counter")
If yesimsure <> "YES" Then
Exit Sub
End If
Dim Str1, Str2, Str3, Str4, Str5
'Create a temporary table which stores song IDs and the number of times each song was played
Str1 = "SELECT Songs.ID, timesPlayed INTO updateCounter "
Str2 = "FROM Songs, [SELECT Played.idSong as SID, Count(Played.idSong) AS timesPlayed FROM Played GROUP BY Played.idSong]. AS subq "
Str3 = "WHERE Songs.Id =subq.SID"
SDB.Database.ExecSQL(Str1 & Str2 & Str3)
'Use the table just created to update the playCounter field in the songs table
Str4 = "UPDATE Songs, updateCounter SET songs.playcounter = updateCounter.timesPlayed "
Str5= "WHERE songs.id=updateCounter.id"
SDB.Database.ExecSQL(Str4 & Str5)
'Delete temporary table
SDB.Database.ExecSQL("DROP TABLE updateCounter")
End Sub
Function Min(x, y)
If x<y Then
Min = x
Else
Min = y
End If
End Function
Function Max(x, y)
If x>y Then
Max = x
Else
Max = y
End If
End Function
Sub SetPlayCounterAdvanced
' Define variables
Dim list, itm, i, userInput, newC , mb, progress, C
'Get a new value for the Play Counter from the user
userInput = InputBox("Enter a formula for the new play counter. Use C to refer to the current value. Examples: 0, C+1, Max(1,C).", "Set Play Counter (Advanced)")
'If Canceled, exit
If userInput = "" Then
Exit Sub
End If
' Get list of selected tracks from MediaMonkey
Set list = SDB.SelectedSongList
If list.count=0 Then
Set list = SDB.AllVisibleSongList
End If
'No songs selected?
If list.count = 0 Then
mb = MsgBox("No songs were selected. Please select some songs and try again",0,"Error")
Exit Sub
End If
'Set up progress
Set Progress = SDB.Progress
Progress.Text = "Writing New Play Counters..."
Progress.MaxValue = list.count
'Process all selected tracks
For i=0 To list.count-1
Set itm = list.Item(i)
C = itm.playCounter
'Evaluate user input
newC = Eval(userInput)
'Set the Play Counter
itm.PlayCounter = newC
itm.UpdateDB
Progress.value = i+1
If Progress.terminate Then
Exit For
End if
Next
Set Progress = nothing
End Sub[/code]
And I added these to the script.ini file:
[code]
[SetPlayCounterAdvanced]
FileName=PlayCounter.vbs
ProcName=SetPlayCounterAdvanced
Order=20
DisplayName=Set Play Counter (Advanced)
Description=Modify the play counter by setting it to a numerical value or a formula depending on the current value
Language=VBScript
ScriptType=0
[ChangePlayCounter]
FileName=PlayCounter.vbs
ProcName=ChangePlayCounter
Order=20
DisplayName=Change Play Counter
Description=Change the play counter of the selected files
Language=VBScript
ScriptType=0
[ResetPlayCounter]
FileName=PlayCounter.vbs
ProcName=ResetPlayCounter
Order=21
DisplayName=Reset Play Counter
Description=Reset the play counter of all files in the library
Language=VBScript
ScriptType=0
[IncrementPlayCounter]
FileName=IncrPlayCntr.vbs
ProcName=IncrPlayCntr
Order=20
DisplayName=Increment Play Counter
Description=Increment the play counter by 1
Language=VBScript
ScriptType=0
[/code]