Work in progress - Modify Play Counter via script

Download and get help for different MediaMonkey for Windows 4 Addons.

Moderators: Peke, Gurus

roylayer
Posts: 85
Joined: Tue Feb 25, 2003 12:44 am

Post by roylayer »

Thanks for checking that out, Pablo. I guess it's one of those properties that Jiri thought only MM should be updating and thus made it read only via the Item object?

And double thanks for coming up with the workaround! I'll give it a shot.

I also like some of the other touches in your script such as the progress bars. I had one in my script at first, but I took it out when I wanted to reduce the script to the bare minimum to make sure that nothing else was causing me a problem. Now that you've confirmed that UpdateDB doesn't work, I'll feel safer about adding the progress bar back into my script. ;-)

Thanks again for all of your efforts.
Happy user of MediaMonkey Gold version 2.5.5.998
Computer: p4, 2.5 ghz, 3 gb ram, win xp
roylayer
Posts: 85
Joined: Tue Feb 25, 2003 12:44 am

Post by roylayer »

I just implemented the ExecSQL method in my script, and it works great! Now, my next question: How can my script refresh the list of selected songs (or the entire Tracks pane)? Even though the database has been updated, I can't see it in MM until I manually do a refresh.

Not a big deal, but it would be nice to do...
Happy user of MediaMonkey Gold version 2.5.5.998
Computer: p4, 2.5 ghz, 3 gb ram, win xp
jiri
Posts: 5419
Joined: Tue Aug 14, 2001 7:00 pm
Location: Czech Republic
Contact:

Post by jiri »

Normally calling UpdateDB also refreshes shown items as well, however in case of PlayCounter field it doesn't work, because this field is considered to be read-only in this context. Something to be improved in the next version of scripting...

Jiri
Pablo
Posts: 554
Joined: Sun Feb 22, 2004 2:59 am

Post by Pablo »

I think there is no way to tell MM to refresh the view, but we can use what you discovered to our advantage :wink: .

Just add the lines

Code: Select all

  itm.playCounter = itm.playCounter + 1
  itm.updateDB
right after the execSQL statement. We know that does update the value of the counter in the view (not in the DB, but we took care of that with the execSQL).

Of course this solutions relies on a bug so I doubt it will still work in the next release of the scripting system, but for now it works :D.

Pablo
roylayer
Posts: 85
Joined: Tue Feb 25, 2003 12:44 am

Post by roylayer »

That's a good idea! This, then, is the latest version of my IncrPlayCntr script:

Code: Select all

'increments play counter

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) 
    
    'refreshes screen
    itm.playcounter = itm.playcounter + 1
    itm.UpdateDB
  Next
End Sub
I like your scripts because of their flexibility, but this is an alternative for those cases where you might just want a "quick and dirty" PlayCounter increment. If you figure out how to update the LastPlayed column with today's date, I'll add that to the script too; otherwise, I think this version will work nicely as is.

One final question. I notice that you set Progress = Nothing in your script. In my VB programs, I usually set objects = nothing after using them, but I haven't done that in these VB Scripts because I didn't see that in some of the examples in this forum. Would it be a good idea to not only set Progress, but also List and Itm = Nothing at the end of the script too?
Happy user of MediaMonkey Gold version 2.5.5.998
Computer: p4, 2.5 ghz, 3 gb ram, win xp
Pablo
Posts: 554
Joined: Sun Feb 22, 2004 2:59 am

Post by Pablo »

Roylayer,

I'm not sure why I set Progress=nothing... I think I've seen that in another script, but it probably makes no difference at all.

With your help with date formatting, it's possible to set the LastPlayed with today's date. Just add the following after your ExecSQL statement:

Code: Select all


SDB.database.execSQL("UPDATE Songs SET LastTimePlayed=#" & Date() & "# WHERE Id=" & itm.songId)

This sets just the date, the hour will be 12:00 am. I tried using Now() instead of Date(), but the format doesn't match what Access expects. Do you know what is the right format for Date AND Time?

Another thing is that the LastPlayed field is not automatically refreshed. Maybe a trick similar to what I did for the counter works, but I'm not sure...

Pablo
roylayer
Posts: 85
Joined: Tue Feb 25, 2003 12:44 am

Post by roylayer »

You are correct - Setting Progress = Nothing doesn't affect the operation of the script. It does release your reference to the RAM allocated to that object, however. Depending on how VB Script (or MediaMonkey itself) works, not setting objects = Nothing could cause that RAM to stay allocated after the script ends or even after MM ends. And it could be a cumulative affect if new RAM gets allocated every time you run the script. I know how all that stuff works in VB, but I'm not sure how it works in a VBS routine executed from MM.

#2/17/2004 12:21:46 PM# is an example date/time from the LastTimePlayed column. (You can see that by viewing the Songs table from within Access. If you don't have Access, there are some freeware apps that let you view Access tables. Some of them also let you update the tables.) I hope that helps!

I'll try updating LastTimePlayed with my SQL. Thanks again.
Happy user of MediaMonkey Gold version 2.5.5.998
Computer: p4, 2.5 ghz, 3 gb ram, win xp
popper
Posts: 34
Joined: Mon Feb 21, 2005 5:10 pm
Location: Germany

here's an update of IncrPlayCntr.vbs

Post by popper »

Thanks for the good work. Incrementing the "played#" counter was something I was looking for for quite a while now!

In case anyone is still reading this topic, I have made some minor modifications to your IncrPlayCntr script that I wanted to give back to the forum.
I don't know much about VBS programming, but it seems that what I've done is actually working:
- Now it also works on a Windows with a non-English regional setting.
- LastTimePlayed is set to "Now" which also includes the exact time of day.
- Now also the separate "Played" DB table is updated with the new data so that the information stays consistent across the database.

Hope you like it.

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 

Guest

Post by Guest »

Thanks for sharing your work, popper! I use this script all of the time - just seconds ago, as a matter of fact. (I'm listening to some mp3s burned on a DVD through my stereo system and want to update the played# counter for them.)

I don't have time to play with your script right now, but it looks good. One thing that I think could be done is to combine the execsql statements for playcounter and lasttimeplayed into one. That should reduce the amount of time needed to do the updates, although I don't know how noticeable it would be. The format of the the update statement would be something like:

update songs set playcounter = xxx, lasttimeplayed = yyy where...

I haven't tried it myself, but it should work.

Also, thanks for your other improvements. Someone previously tried to make now() work but wasn't able to do so. I don't think that I even knew about the Played table when I first wrote the script, so it's good that you have made everything consistent now.

Thanks again!
roylayer
Posts: 85
Joined: Tue Feb 25, 2003 12:44 am

Post by roylayer »

P.S. The above post was made by me (roylayer), the original author of the script. I wasn't logged on automatically for some reason. <sigh>
Happy user of MediaMonkey Gold version 2.5.5.998
Computer: p4, 2.5 ghz, 3 gb ram, win xp
pah68
Posts: 1504
Joined: Wed Apr 07, 2004 5:26 pm
Location: Sydney, Australia

Post by pah68 »

Nice work, I'll be using this little gem, cheers :D
pah68
Posts: 1504
Joined: Wed Apr 07, 2004 5:26 pm
Location: Sydney, Australia

Post by pah68 »

There seems to be a limit of how many tracks I can modify (in one go) before it generates script errors. I need to restart MM to get it to work again. :-?
pah68
Posts: 1504
Joined: Wed Apr 07, 2004 5:26 pm
Location: Sydney, Australia

Post by pah68 »

Doesn't seem to have anything to do with the number of tracks I'm modifying. THe script crashes sometimes, I can then successfully try again after restarting MM :-?
roylayer
Posts: 85
Joined: Tue Feb 25, 2003 12:44 am

Post by roylayer »

That is very strange indeed. I have been using the script for a year and never had this problem, yet it happened to me tonight, before reading your note! In my case, I was trying to update far more tracks than I normally would with the script. Darn!
Happy user of MediaMonkey Gold version 2.5.5.998
Computer: p4, 2.5 ghz, 3 gb ram, win xp
pah68
Posts: 1504
Joined: Wed Apr 07, 2004 5:26 pm
Location: Sydney, Australia

Post by pah68 »

I discovered it by initially trying it out on a few tracks...no probs
Tried some more OK
Tried a heap...boom
Tried less OK
Tried 37 tracks..OK
Tried 49..OK
Tried 34..boom

The quantity didn't seem to cause the problem.
I was modifying fields in a an auto playlist that showed unheard songs and wasn't refreshing the list, maybe this was the problem?
Post Reply