[Solved] Custom script progress report

This forum is for questions / discussions regarding development of addons / tweaks for MediaMonkey for Windows 4.

Moderators: Gurus, Addon Administrators

Edgar
Posts: 81
Joined: Sun Aug 30, 2009 12:06 pm

[Solved] Custom script progress report

Post by Edgar »

I've written a custom script which has quite a bit of code. When I run it on a large number of files it would be nice to know how things are progressing. At a bare minimum it would be nice to know when the script terminates. Is there any way to add a progress bar to a custom script? Barring that, is there any way to give an audible notification (play a short sound file, TTSPlayString etc.) as the last action of a script?

I found a script to copy from; here's an example:
[code]' A script to eliminate unnecessary spaces; with progress bar

Option Explicit

Sub Trivial
' Define variables
Dim list, song, i, temp
Dim Progress, ExpText
Set Progress = SDB.Progress
ExpText = "Eliminating excess spaces..."
Progress.Text = ExpText

' Get list of selected tracks from MediaMonkey
Set list = SDB.SelectedSongList
If list.count=0 Then
Set list = SDB.AllVisibleSongList
End If

Progress.MaxValue = list.count
' Process all selected tracks
For i = 0 To list.count - 1
Set song = list.Item(i)

temp = song.Title
Progress.Text = ExpText & " (" & song.Title & ")"
temp = Replace(temp, "( .)", ".")
temp = Trim(temp)' remove leading and trailing spaces
For j = 1 To 10
temp = Replace(temp, " ", " ")' remove double spaces
Next
song.Title = temp

temp = song.ArtistName
temp = Replace(temp, "( .)", ".")
temp = Trim(temp)
For j = 1 To 10
temp = Replace(temp, " ", " ")
Next
song.ArtistName = temp

temp = song.AlbumArtistName
temp = Replace(temp, "( .)", ".")
temp = Trim(temp)
For j = 1 To 10
temp = Replace(temp, " ", " ")
Next
song.AlbumArtistName = temp

temp = song.AlbumName
temp = Replace(temp, "( .)", ".")
temp = Trim(temp)
For j = 1 To 10
temp = Replace(temp, " ", " ")
Next
song.AlbumName = temp

' Update the changes in DB
song.UpdateDB
Progress.Value = i+1
Next
End Sub
[/code]
–Edgar

64-bit Windows 10 MediaMonkey Lifetime Gold 3, 4 & beta5
Asus ASUS Prime X299-Deluxe motherboard
sound system: Soundcraft Signature 12 MTK mixer, JBL Eon 15 G2 speakers as mains and mains subs, and JBL Eon610 mains with Tascam LF-S8 sub as near field monitors at the computer
rivorson
Posts: 594
Joined: Thu Jul 25, 2013 4:17 am

Re: [Solved] Custom script progress report

Post by rivorson »

You should already get a progress bar from your code, but maybe you just need to add a DoEvents to allow other processes to run and update the display.
Progress.MaxValue sets the end value for your progress bar.
Progress.Value = i+1 sets the current value, but you can replace this with Progress.Increase.

You can also tidy your code up by using a function call instead of repeating the same block of code several times. A regex will also be cleaner and faster than multiple string replace functions too.

Code: Select all

Option Explicit

Sub Trivial()
    ' Define variables
    Dim list, song, i, temp
    Dim Progress, ExpText
    Set Progress = SDB.Progress
    ExpText = "Eliminating excess spaces..."
    Progress.Text = ExpText
    
    ' Get list of selected tracks from MediaMonkey
    Set list = SDB.SelectedSongList
    If list.Count = 0 Then Set list = SDB.AllVisibleSongList
    
    Progress.MaxValue = list.Count
    ' Process all selected tracks
    For i = 0 To list.Count - 1
        'bar.Terminate indicates whether the user has tried to cancel
        If bar.Terminate Then Exit For
        
        Set song = list.Item(i)
        Progress.Text = ExpText & " (" & song.Title & ")"
        Progress.Increase 'increase the progress bar
        DoEvents 'allow other processes to run.
        
        
        song.Title = RemoveExcessSpaces(song.Title)
        song.ArtistName = RemoveExcessSpaces(song.ArtistName)
        song.AlbumArtistName = RemoveExcessSpaces(song.AlbumArtistName)
        song.AlbumName = RemoveExcessSpaces(song.AlbumName)
    Next
    
    list.UpdateAll 'updates DB and file metadata depending on user preferences
    Set Progress = Nothing
End Sub

Function RemoveExcessSpaces(Text)
    On Error Resume Next
    Const reMultipleSpaces = " +"
    Const reEndSpaces = "^ | $| +(?=\.[\w\d]+$)"
    RemoveExcessSpaces = Text 'in case regex causes error
    
    Static Regex 'Static stays defined between function calls
    If Regex Is Nothing Then Set Regex = New RegExp
    Regex.Global = True
    Regex.Pattern = reMultipleSpaces
    Text = Regex.Replace(Text, "")
    Regex.Pattern = reEndSpaces
    RemoveExcessSpaces = Regex.Replace(Text, vbNullString)
End Function
Edgar
Posts: 81
Joined: Sun Aug 30, 2009 12:06 pm

Re: [Solved] Custom script progress report

Post by Edgar »

[quote="rivorson"]maybe you just need to add a DoEvents to allow other processes to run and update the display.
… you can replace this with [i]Progress.Increase[/i].

You can also tidy your code up[/quote]
Thanks! Lots of good stuff here. Could you explain this line:
[code]Const reEndSpaces = "^ | $| +(?=\.[\w\d]+$)"[/code]
I suppose I need to learn more about Regex.
–Edgar

64-bit Windows 10 MediaMonkey Lifetime Gold 3, 4 & beta5
Asus ASUS Prime X299-Deluxe motherboard
sound system: Soundcraft Signature 12 MTK mixer, JBL Eon 15 G2 speakers as mains and mains subs, and JBL Eon610 mains with Tascam LF-S8 sub as near field monitors at the computer
rivorson
Posts: 594
Joined: Thu Jul 25, 2013 4:17 am

Re: [Solved] Custom script progress report

Post by rivorson »

The first regex pattern (" +") matches any instance of one or more spaces. The matched space groups are then each replaced with a single space character.

The second pattern is comprised of three parts:
"^ " - ^ means the start of the string so this matches if the first character is a space.
" $" - $ means the end of the string so this matches if the last character is a space.
" +(?=\.[\w\d]+$)" - matches any number of spaces at the end of a file name but before the extension. The (?= part denotes a positive lookahead so anything contained within is required to exist in the string but isn't considered part of the match. This means it isn't removed when we run the Replace function.
"|" is the Or operator so we use that to combine the three parts into a single query. Because we set Regex.Global=True every possible match for this pattern is executed in a single query.
Edgar
Posts: 81
Joined: Sun Aug 30, 2009 12:06 pm

Re: [Solved] Custom script progress report

Post by Edgar »

rivorson wrote:The first regex pattern…
Thanks again; now I know why I once saw a college class devoted exclusively to Regex <grin>.
–Edgar

64-bit Windows 10 MediaMonkey Lifetime Gold 3, 4 & beta5
Asus ASUS Prime X299-Deluxe motherboard
sound system: Soundcraft Signature 12 MTK mixer, JBL Eon 15 G2 speakers as mains and mains subs, and JBL Eon610 mains with Tascam LF-S8 sub as near field monitors at the computer
Post Reply