Add text box to Dialog

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

Moderators: Peke, Gurus

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

Add text box to Dialog

Post by Edgar »

I'm fairly new to MediaMonkey scripting. In my script I open a dialog and want to ask the user for some text input then use that text later on in the script. I have no idea how to create a TextBox (it need not be a RichTextBox) nor how to use its content (after testing to make sure that it is not empty).

Code: Select all

' A script that edits the Artist of selected tracks and also assigns the album artist

Sub EditArtists
   Const mmAnchorRight = 4
   Const mmAnchorBottom = 8
   Const mmAlignBottom = 2
   Const mmFormScreenCenter = 4
   Dim list, item, increment, temporary, amount, newName
   Dim UI, MainWindow, Footer
   Dim OKButton, CancelButton, TextField

   Set UI = SDB.UI
   Set MainWindow = UI.NewForm
   MainWindow.Common.SetRect 500, 350, 500, 300
   MainWindow.Common.MinWidth = 200
   MainWindow.Common.MinHeight = 150
   MainWindow.Caption = SDB.Localize("Enter a new artist name")
   MainWindow.StayOnTop = True

   'Set TextField = UI.NewTextBox
   'TextField.Common.SetRect 20, 25, 485, 40

   Set Footer = UI.NewPanel(MainWindow)
   Footer.Common.Align = mmAlignBottom
   Footer.Common.Height = 35

   Set CancelButton = UI.NewButton(Footer)
   CancelButton.Caption = SDB.Localize("&Cancel")
   CancelButton.Common.SetRect dialogWidth - 106, 5, 85, 25
   CancelButton.Common.Anchors = mmAnchorRight + mmAnchorBottom
   CancelButton.Cancel = True
   CancelButton.UseScript = Script.ScriptPath
   CancelButton.OnClickFunc = "OnCancel"

   Set OKButton = UI.NewButton(Footer)
   OKButton.Caption = SDB.Localize("&OK")
   OKButton.Common.SetRect dialogWidth - 306, 5, 85, 25
   OKButton.Common.Anchors = mmAnchorRight + mmAnchorBottom
   OKButton.Cancel = False
   OKButton.UseScript = Script.ScriptPath
   OKButton.OnClickFunc = "OnOK"

   SDB.ProcessMessages

   MainWindow.SavePositionName = "EditArtistNameWindow"
   MainWindow.Common.Visible = True
   SDB.Objects("Edit") = MainWindow

   SDB.ProcessMessages
End Sub

Sub OnCancel(Btn)
   SDB.Objects("Edit") = Nothing
End Sub

Sub OnOK(Btn)
   ' If TextField.Text <> "" Then
   '    RenameArtist (TextField.Text)
   ' Else
   '    Return
   ' End If
   SDB.Objects("Edit") = Nothing
End Sub

Sub RenameArtist(pNewName)
   Set list = SDB.CurrentSongList
   amount = list.count - 1
   For increment = 0 To amount
      Set item = list.Item(increment)
      item.ArtistName = pNewName
      item.AlbumArtistName = pNewName
   Next
   list.UpdateAll
End Sub
The first problem is creating the text box itself:

Code: Select all

   'Set TextField = UI.NewTextBox
   'TextField.Common.SetRect 20, 25, 485, 40
the next problem is actually using the input:

Code: Select all

   ' If TextField.Text <> "" Then
   '    RenameArtist (TextField.Text)
   ' Else
   '    Return
   ' End If
a final problem is that I have no idea how to declare a sub-function with a string input parameter:

Code: Select all

Sub RenameArtist(pNewName)
   Set list = SDB.CurrentSongList
   amount = list.count - 1
   For increment = 0 To amount
      Set item = list.Item(increment)
      item.ArtistName = pNewName
      item.AlbumArtistName = pNewName
   Next
   list.UpdateAll
End Sub
The reason I want to do this is to make a (much simplified) replacement for the built-in File Properties dialog. Because the Properties dialog has many dropdowns for things like: Artist; Album; Genre etc., all of whose lists need to be populated dynamically, it can take a very long time for the dialog to open.

Thanks in advance for any help!
–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
Edgar
Posts: 81
Joined: Sun Aug 30, 2009 12:06 pm

Re: Add text box to Dialog

Post by Edgar »

I spent the whole day trying to pull myself up by my bootstraps and figured out almost all of it…

Code: Select all

' A script that edits the Artist of selected tracks and also assigns the album artist

Dim NameTextField, UI, MainWindow
Set UI = SDB.UI
Set MainWindow = UI.NewForm
Set NameTextField = UI.NewEdit (MainWindow)

Sub EditArtists
   Const mmAnchorRight = 4
   Const mmAnchorBottom = 8
   Const mmAlignBottom = 2
   Dim Footer, OKButton, CancelButton, list, item, dialogWidth

   dialogWidth = 500
   Set list = SDB.CurrentSongList
   Set item = list.Item(0)

   MainWindow.Common.SetRect 500, 300, dialogWidth, 200
   MainWindow.Common.MinWidth = 200
   MainWindow.Common.MinHeight = 200
   MainWindow.Caption = SDB.Localize("Enter a new artist name")
   MainWindow.StayOnTop = True

   NameTextField.Text = item.ArtistName
   NameTextField.Common.SetRect 5, 5, (dialogWidth - 50), 40
SDB.MessageBox "at entry x_" & NameTextField.Text & "_x", 2, Array(4)

   Set Footer = UI.NewPanel(MainWindow)
   Footer.Common.Align = mmAlignBottom
   Footer.Common.Height = 35

   Set CancelButton = UI.NewButton(Footer)
   CancelButton.Caption = SDB.Localize("&Cancel")
   CancelButton.Common.SetRect dialogWidth - 106, 5, 85, 25
   CancelButton.Common.Anchors = mmAnchorRight + mmAnchorBottom
   CancelButton.Cancel = True
   CancelButton.UseScript = Script.ScriptPath
   CancelButton.OnClickFunc = "OnCancel"

   Set OKButton = UI.NewButton(Footer)
   OKButton.Caption = SDB.Localize("&OK")
   OKButton.Common.SetRect dialogWidth - 306, 5, 85, 25
   OKButton.Common.Anchors = mmAnchorRight + mmAnchorBottom
   OKButton.Cancel = False
   OKButton.UseScript = Script.ScriptPath
   OKButton.OnClickFunc = "OnOK"

   SDB.ProcessMessages

   MainWindow.SavePositionName = "EditArtistNameWindow"
   MainWindow.Common.Visible = True
   SDB.Objects("EditArtistName") = MainWindow

   SDB.ProcessMessages
End Sub

Sub OnCancel(Btn)
   SDB.Objects("EditArtistName") = Nothing
End Sub

Sub OnOK(Btn)
SDB.MessageBox "on okay x_" & NameTextField.Text & "_x", 2, Array(4)
   Dim pList, pItem, increment, amount
   Set  pList = SDB.CurrentSongList
   amount =  pList.count - 1
   For increment = 0 To amount
       Set pItem = pList.Item(increment)
       pItem.ArtistName = NameTextField.Text
       pItem.AlbumArtistName = NameTextField.Text
   Next
   pList.UpdateAll
   SDB.Objects("EditArtistName") = Nothing
End Sub
Everything is working except that the artist name and album artist name fields are being set to an empty string instead of to the content of the edit box. It feels almost as if the edit box and its content has been destroyed by the time I get into the on okay button handler. I have made the text box a global variable but the OnOK function might be creating a local variable and masking the global variable.

There are two MessageBox statements, almost identical, the first is within the entry function, the second is in the OnOK function. Each has a trivial preface (so that it's easy to tell where the message is coming from) followed by the contents of the text box (wrapped in "x_"/"_x" so that it is easy to tell that the string is empty). In the OnOK function the string is empty.
–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