Work in progress - Modify Play Counter via script

Download and get help for different MediaMonkey Addons.

Moderators: Peke, Gurus

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

Post by roylayer » Fri Mar 12, 2004 10:09 pm

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 » Fri Mar 12, 2004 10:20 pm

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: 5394
Joined: Tue Aug 14, 2001 7:00 pm
Location: Czech Republic
Contact:

Post by jiri » Sat Mar 13, 2004 3:26 am

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 » Sat Mar 13, 2004 3:27 am

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 » Sat Mar 13, 2004 10:41 am

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 » Sat Mar 13, 2004 12:39 pm

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 » Sat Mar 13, 2004 1:20 pm

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 » Mon Feb 21, 2005 5:46 pm

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 » Mon Feb 21, 2005 7:29 pm

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 » Mon Feb 21, 2005 7:32 pm

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: 1496
Joined: Wed Apr 07, 2004 5:26 pm
Location: Sydney, Australia

Post by pah68 » Mon Feb 21, 2005 10:49 pm

Nice work, I'll be using this little gem, cheers :D

pah68
Posts: 1496
Joined: Wed Apr 07, 2004 5:26 pm
Location: Sydney, Australia

Post by pah68 » Mon Feb 21, 2005 10:52 pm

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: 1496
Joined: Wed Apr 07, 2004 5:26 pm
Location: Sydney, Australia

Post by pah68 » Mon Feb 21, 2005 10:57 pm

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 » Mon Feb 21, 2005 11:03 pm

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: 1496
Joined: Wed Apr 07, 2004 5:26 pm
Location: Sydney, Australia

Post by pah68 » Tue Feb 22, 2005 3:42 pm

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