by Nanya » Thu Oct 04, 2012 11:52 pm
I made some modifications to this script, which makes it more of a "Simple Insert" rather than just append and prepend.
1: Now includes option to insert in the middle of the string, just specify an index!
2: Don't want/need a space added when you insert your string? There's a checkbox for that!
3: Simplified the code that applies the new data.
If you find this code useful/helpful, please let me know, I'm new at this!
Code: Select all
Option Explicit
'==========================================================================
'
' MediaMonkey Script
'
' SCRIPTNAME: Simple Append
' INITIAL VERSION: 2009.01.09
Dim Version : Version = "2009.04.10.1600"
' DESCRIPTION: simple dialog to append or pre-pend user text to specified field
' FORUM THREAD: http://www.mediamonkey.com/forum/viewtopic.php?f=2&t=36013
' [SimpleAppend]
' filename=simpleappend.vbs
' procname=start
' displayname=Simple Append
' language=vbscript
' scripttype=0
' INSTALL:
' - download install package @ http://teknojnky.googlepages.com/simpleappend.mmip
' - activate via script menu'
' Initial Release:
' Due to apparent problem saving stringvalue with leading/trailing spaces
' there is a space hard coded in'
' Recent Updates:
' * fixed type error
'
' * added genre, classifcations and custom fields'
' * forgot to disabled resize'
Dim MainForm, UserText, Action, Field, FieldText, Preview, SongList
Preview = ""
''''''''''''''''''''''''
Dim indexField, Index, spaceBox, Space
Index = 0
''''''''''''''''''''''''
Sub Start()
Set Songlist = SDB.CurrentSongList
If Songlist.Count > 0 Then
Set MainForm = SDB.UI.NewForm
MainForm.Caption = "Simple Append/Pre-pend"
MainForm.Common.SetRect 200, 200, 600, 100
MainForm.FormPosition = 4
MainForm.BorderStyle = 3
MainForm.StayOnTop = True
MainForm.SavePositionName = "SimpleAppend"
Dim DropAction
Set DropAction = SDB.UI.NewDropDown(MainForm)
DropAction.Common.ControlName = "Action"
DropAction.Common.Hint = "Action"
DropAction.Style = 2
DropAction.Common.SetRect 10, 10, 100, 30
DropAction.AddItem("Append (to end)")
DropAction.AddItem("PrePend (to front)")
'
DropAction.AddItem("Insert at: ")
'
If SDB.IniFile.IntValue("SimpleAppend","Action") = 0 Then
DropAction.ItemIndex = 0
ElseIf SDB.IniFile.IntValue("SimpleAppend","Action") = 1 Then
DropAction.ItemIndex = 1
Else
DropAction.ItemIndex = 2
End If
Action = DropAction.ItemIndex
Script.RegisterEvent DropAction, "OnChange", "PreviewIt"
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Set indexField = SDB.UI.NewEdit(MainForm)
indexField.Common.ControlName = "Index"
indexField.Common.Hint = "Index in the field where the string should be added"
indexField.Common.SetRect 120, 10, 30, 30
indexField.Common.Enabled = False
indexField.Text = "..."
Script.RegisterEvent indexField, "OnChange", "PreviewIt"
Set spaceBox = SDB.UI.NewCheckBox(MainForm)
spaceBox.Common.ControlName = "Space"
spaceBox.Common.Hint = "Add a space before/after your text?"
spaceBox.Common.SetRect 500, 10, 80, 20
spaceBox.Caption = "Space?"
spaceBox.Checked = True
Script.RegisterEvent spaceBox.Common, "OnClick", "PreviewIt"
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim edtUser
Set edtUser = SDB.UI.NewEdit(MainForm)
edtUser.Common.ControlName = "Edit"
edtUser.Common.Hint = "Text you wish to add"
edtUser.Common.SetRect 160, 10, 180, 30
If SDB.IniFile.StringValue("SimpleAppend","UserText") <> "" Then
edtUser.Text = SDB.IniFile.StringValue("SimpleAppend","UserText")
End If
UserText = edtUser.Text
Script.RegisterEvent edtUser, "OnChange", "PreviewIt"
Dim lbl
Set lbl = SDB.UI.NewLabel(MainForm)
lbl.Caption = "into"
lbl.Common.SetRect 346, 12, 20, 30
Dim DropField
Set DropField = SDB.UI.NewDropDown(MainForm)
DropField.Common.ControlName = "Field"
DropField.Common.Hint = "Field to add to"
DropField.Style = 2
DropField.Common.SetRect 370, 10, 120, 30
DropField.AddItem("Title")
DropField.AddItem("Comment")
DropField.AddItem("Artist")
DropField.AddItem("AlbumArtist")
DropField.AddItem("Album")
DropField.AddItem("Tempo")
DropField.AddItem("Mood")
DropField.AddItem("Quality")
DropField.AddItem("Custom 1")
DropField.AddItem("Custom 2")
DropField.AddItem("Custom 3")
DropField.AddItem("Custom 4")
DropField.AddItem("Custom 5")
Script.RegisterEvent DropField, "OnChange", "PreviewIt"
If SDB.IniFile.IntValue("SimpleAppend","Field") < 1 Then
DropField.ItemIndex = 0
Else
DropField.ItemIndex = SDB.IniFile.IntValue("SimpleAppend","Field")
End If
' Field = DropField.ItemText(DropField.ItemIndex)
Field = DropField.ItemIndex
Dim btnDoIt
Set btnDoIt = SDB.UI.NewButton(MainForm)
btnDoIt.Caption = "&Do It!"
btnDoIt.Common.Hint = "Apply It Now (NO UNDO) Escape to Cancel"
btnDoIt.ModalResult = 1
btnDoIt.Common.SetRect 500,35,80,30
Set lbl = SDB.UI.NewLabel(MainForm)
lbl.Caption = "PREVIEWING: 1st of " & Songlist.Count
lbl.Common.SetRect 12, 45, 120, 30
Dim edtPreview
Set edtPreview = SDB.UI.NewEdit(MainForm)
edtPreview.Common.ControlName = "Preview"
edtPreview.Common.Hint = "Preview of the first of the selected track(s)"
edtPreview.Common.SetRect 140, 40, 350, 50
edtPreview.Common.Enabled = False
edtPreview.Text = Preview
Dim btnClose
Set btnClose = SDB.UI.NewButton(MainForm)
btnClose.Cancel = True
btnClose.ModalResult = 2
btnClose.Common.SetRect 0,0,0,0
If MainForm.ShowModal = 1 Then
DoIt(MainForm)
End If
Script.UnregisterAllEvents
End If
End Sub
Sub PreviewIt(obj)
Dim tmp, Song
Set tmp = obj.Common.TopParent
Action = tmp.Common.ChildControl("Action").ItemIndex
UserText = tmp.Common.ChildControl("Edit").Text
Field = tmp.Common.ChildControl("Field").ItemIndex
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'if "Insert at" is selected:
If Action = 2 Then
If indexField.Common.Enabled = False Then
indexField.Text = "0"
indexField.Common.Enabled = True
ElseIf isNumeric(indexField.Text) Then
Index = indexField.Text
Else
Exit Sub
End If
Else
indexField.Common.Enabled = False
End If
'If the spaceBox is Checked, toggle the variable
Space = spaceBox.Checked
If spaceBox.Checked Then
Space = " "
Else
Space = ""
End If
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If Songlist.Count > 0 Then
Set Song = Songlist.Item(0)
Select Case Field
Case 0
FieldText = Song.Title
Case 1
FieldText = Song.Comment
Case 2
FieldText = Song.ArtistName
Case 3
FieldText = Song.AlbumArtistName
Case 4
FieldText = Song.AlbumName
Case 5
FieldText = Song.Genre
Case 6
FieldText = Song.Tempo
Case 7
FieldText = Song.Mood
Case 8
FieldText = Song.Occasion
Case 9
FieldText = Song.Quality
Case 10
FieldText = Song.Custom1
Case 11
FieldText = Song.Custom2
Case 12
FieldText = Song.Custom3
Case 13
FieldText = Song.Custom4
Case 14
FieldText = Song.Custom5
Case Else
msgbox("Invalid field selected?")
End Select
If Action = 0 Then 'Append
Preview = FieldText & Space & UserText
ElseIf Action = 1 Then 'Prepend
Preview = UserText & Space & FieldText
Else 'Insert
If Index = 0 Then
Preview = UserText & Space & Mid(FieldText,Index+1,len(FieldText))
ElseIf Index = len(FieldText) Then
Preview = Mid(FieldText,1,Index) & Space & UserText
Else
Preview = Mid(FieldText,1,Index) & Space & UserText & Space & Mid(FieldText,Index+1,len(FieldText))
End If
End If
If Not tmp.Common.ChildControl("Preview") Is Nothing Then
tmp.Common.ChildControl("Preview").Text = Preview
End If
End If
SDB.IniFile.IntValue("SimpleAppend","Action") = Action
SDB.IniFile.IntValue("SimpleAppend","Field") = Field
SDB.IniFile.StringValue("SimpleAppend","UserText") = UserText
End Sub
Sub DoIt(obj)
' msgbox("Doing It, there is no undo!")
If Songlist.Count > 0 Then
Dim Txt, Track, x, NewSongs, Song, Preview, NewField
Set NewSongs = SDB.NewSongList
For x = 0 to Songlist.Count-1
Set Song = Songlist.Item(x)
Select Case Field
Case 0
FieldText = Song.Title
Case 1
FieldText = Song.Comment
Case 2
FieldText = Song.ArtistName
Case 3
FieldText = Song.AlbumArtistName
Case 4
FieldText = Song.AlbumName
Case 5
FieldText = Song.Genre
Case 6
FieldText = Song.Tempo
Case 7
FieldText = Song.Mood
Case 8
FieldText = Song.Occasion
Case 9
FieldText = Song.Quality
Case 10
FieldText = Song.Custom1
Case 11
FieldText = Song.Custom2
Case 12
FieldText = Song.Custom3
Case 13
FieldText = Song.Custom4
Case 14
FieldText = Song.Custom5
Case Else
msgbox("Invalid field selected?")
End Select
If Action = 0 Then 'Append
NewField = FieldText & Space & UserText
ElseIf Action = 1 Then 'Prepend
NewField = UserText & Space & FieldText
Else 'Insert
If Index = 0 Then
NewField = UserText & Space & Mid(FieldText,Index+1,len(FieldText))
ElseIf Index = len(FieldText) Then
NewField = Mid(FieldText,1,Index) & Space & UserText
Else
NewField = Mid(FieldText,1,Index) & Space & UserText & Space & Mid(FieldText,Index+1,len(FieldText))
End If
End If
Select Case Field
Case 0
Song.Title = NewField
Case 1
Song.Comment = NewField
Case 2
Song.ArtistName = NewField
Case 3
Song.AlbumArtistName = NewField
Case 4
Song.AlbumName = NewField
Case 5
Song.Genre = NewField
Case 6
Song.Tempo = NewField
Case 7
Song.Mood = NewField
Case 8
Song.Occasion = NewField
Case 9
Song.Quality = NewField
Case 10
Song.Custom1 = NewField
Case 11
Song.Custom2 = NewField
Case 12
Song.Custom3 = NewField
Case 13
Song.Custom4 = NewField
Case 14
Song.Custom5 = NewField
Case Else
End Select
NewSongs.Add(Song)
SDB.ProcessMessages
Next
Else
msgbox("No Tracks Selected")
End If
NewSongs.UpdateAll
End Sub
Sub Install()
Dim scriptName : scriptName = "SimpleAppend"
'Add scripts.ini entries
Dim inip : inip = SDB.ApplicationPath&"Scripts\Scripts.ini"
Dim inif : Set inif = SDB.Tools.IniFileByPath(inip)
If Not (inif Is Nothing) Then
inif.StringValue(scriptName,"Filename") = "SimpleAppend.vbs"
inif.StringValue(scriptName,"Procname") = "Start"
inif.StringValue(scriptName,"DisplayName") = "Simple Append"
inif.StringValue(scriptName,"Description") = "Adds text to selected field"
inif.StringValue(scriptName,"Language") = "VBScript"
inif.StringValue(scriptName,"ScriptType") = "0"
SDB.RefreshScriptItems
End If
End Sub
I made some modifications to this script, which makes it more of a "Simple Insert" rather than just append and prepend.
1: Now includes option to insert in the middle of the string, just specify an index!
2: Don't want/need a space added when you insert your string? There's a checkbox for that!
3: Simplified the code that applies the new data.
If you find this code useful/helpful, please let me know, I'm new at this!
[code]Option Explicit
'==========================================================================
'
' MediaMonkey Script
'
' SCRIPTNAME: Simple Append
' INITIAL VERSION: 2009.01.09
Dim Version : Version = "2009.04.10.1600"
' DESCRIPTION: simple dialog to append or pre-pend user text to specified field
' FORUM THREAD: http://www.mediamonkey.com/forum/viewtopic.php?f=2&t=36013
' [SimpleAppend]
' filename=simpleappend.vbs
' procname=start
' displayname=Simple Append
' language=vbscript
' scripttype=0
' INSTALL:
' - download install package @ http://teknojnky.googlepages.com/simpleappend.mmip
' - activate via script menu'
' Initial Release:
' Due to apparent problem saving stringvalue with leading/trailing spaces
' there is a space hard coded in'
' Recent Updates:
' * fixed type error
'
' * added genre, classifcations and custom fields'
' * forgot to disabled resize'
Dim MainForm, UserText, Action, Field, FieldText, Preview, SongList
Preview = ""
''''''''''''''''''''''''
Dim indexField, Index, spaceBox, Space
Index = 0
''''''''''''''''''''''''
Sub Start()
Set Songlist = SDB.CurrentSongList
If Songlist.Count > 0 Then
Set MainForm = SDB.UI.NewForm
MainForm.Caption = "Simple Append/Pre-pend"
MainForm.Common.SetRect 200, 200, 600, 100
MainForm.FormPosition = 4
MainForm.BorderStyle = 3
MainForm.StayOnTop = True
MainForm.SavePositionName = "SimpleAppend"
Dim DropAction
Set DropAction = SDB.UI.NewDropDown(MainForm)
DropAction.Common.ControlName = "Action"
DropAction.Common.Hint = "Action"
DropAction.Style = 2
DropAction.Common.SetRect 10, 10, 100, 30
DropAction.AddItem("Append (to end)")
DropAction.AddItem("PrePend (to front)")
'
DropAction.AddItem("Insert at: ")
'
If SDB.IniFile.IntValue("SimpleAppend","Action") = 0 Then
DropAction.ItemIndex = 0
ElseIf SDB.IniFile.IntValue("SimpleAppend","Action") = 1 Then
DropAction.ItemIndex = 1
Else
DropAction.ItemIndex = 2
End If
Action = DropAction.ItemIndex
Script.RegisterEvent DropAction, "OnChange", "PreviewIt"
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Set indexField = SDB.UI.NewEdit(MainForm)
indexField.Common.ControlName = "Index"
indexField.Common.Hint = "Index in the field where the string should be added"
indexField.Common.SetRect 120, 10, 30, 30
indexField.Common.Enabled = False
indexField.Text = "..."
Script.RegisterEvent indexField, "OnChange", "PreviewIt"
Set spaceBox = SDB.UI.NewCheckBox(MainForm)
spaceBox.Common.ControlName = "Space"
spaceBox.Common.Hint = "Add a space before/after your text?"
spaceBox.Common.SetRect 500, 10, 80, 20
spaceBox.Caption = "Space?"
spaceBox.Checked = True
Script.RegisterEvent spaceBox.Common, "OnClick", "PreviewIt"
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim edtUser
Set edtUser = SDB.UI.NewEdit(MainForm)
edtUser.Common.ControlName = "Edit"
edtUser.Common.Hint = "Text you wish to add"
edtUser.Common.SetRect 160, 10, 180, 30
If SDB.IniFile.StringValue("SimpleAppend","UserText") <> "" Then
edtUser.Text = SDB.IniFile.StringValue("SimpleAppend","UserText")
End If
UserText = edtUser.Text
Script.RegisterEvent edtUser, "OnChange", "PreviewIt"
Dim lbl
Set lbl = SDB.UI.NewLabel(MainForm)
lbl.Caption = "into"
lbl.Common.SetRect 346, 12, 20, 30
Dim DropField
Set DropField = SDB.UI.NewDropDown(MainForm)
DropField.Common.ControlName = "Field"
DropField.Common.Hint = "Field to add to"
DropField.Style = 2
DropField.Common.SetRect 370, 10, 120, 30
DropField.AddItem("Title")
DropField.AddItem("Comment")
DropField.AddItem("Artist")
DropField.AddItem("AlbumArtist")
DropField.AddItem("Album")
DropField.AddItem("Tempo")
DropField.AddItem("Mood")
DropField.AddItem("Quality")
DropField.AddItem("Custom 1")
DropField.AddItem("Custom 2")
DropField.AddItem("Custom 3")
DropField.AddItem("Custom 4")
DropField.AddItem("Custom 5")
Script.RegisterEvent DropField, "OnChange", "PreviewIt"
If SDB.IniFile.IntValue("SimpleAppend","Field") < 1 Then
DropField.ItemIndex = 0
Else
DropField.ItemIndex = SDB.IniFile.IntValue("SimpleAppend","Field")
End If
' Field = DropField.ItemText(DropField.ItemIndex)
Field = DropField.ItemIndex
Dim btnDoIt
Set btnDoIt = SDB.UI.NewButton(MainForm)
btnDoIt.Caption = "&Do It!"
btnDoIt.Common.Hint = "Apply It Now (NO UNDO) Escape to Cancel"
btnDoIt.ModalResult = 1
btnDoIt.Common.SetRect 500,35,80,30
Set lbl = SDB.UI.NewLabel(MainForm)
lbl.Caption = "PREVIEWING: 1st of " & Songlist.Count
lbl.Common.SetRect 12, 45, 120, 30
Dim edtPreview
Set edtPreview = SDB.UI.NewEdit(MainForm)
edtPreview.Common.ControlName = "Preview"
edtPreview.Common.Hint = "Preview of the first of the selected track(s)"
edtPreview.Common.SetRect 140, 40, 350, 50
edtPreview.Common.Enabled = False
edtPreview.Text = Preview
Dim btnClose
Set btnClose = SDB.UI.NewButton(MainForm)
btnClose.Cancel = True
btnClose.ModalResult = 2
btnClose.Common.SetRect 0,0,0,0
If MainForm.ShowModal = 1 Then
DoIt(MainForm)
End If
Script.UnregisterAllEvents
End If
End Sub
Sub PreviewIt(obj)
Dim tmp, Song
Set tmp = obj.Common.TopParent
Action = tmp.Common.ChildControl("Action").ItemIndex
UserText = tmp.Common.ChildControl("Edit").Text
Field = tmp.Common.ChildControl("Field").ItemIndex
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'if "Insert at" is selected:
If Action = 2 Then
If indexField.Common.Enabled = False Then
indexField.Text = "0"
indexField.Common.Enabled = True
ElseIf isNumeric(indexField.Text) Then
Index = indexField.Text
Else
Exit Sub
End If
Else
indexField.Common.Enabled = False
End If
'If the spaceBox is Checked, toggle the variable
Space = spaceBox.Checked
If spaceBox.Checked Then
Space = " "
Else
Space = ""
End If
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If Songlist.Count > 0 Then
Set Song = Songlist.Item(0)
Select Case Field
Case 0
FieldText = Song.Title
Case 1
FieldText = Song.Comment
Case 2
FieldText = Song.ArtistName
Case 3
FieldText = Song.AlbumArtistName
Case 4
FieldText = Song.AlbumName
Case 5
FieldText = Song.Genre
Case 6
FieldText = Song.Tempo
Case 7
FieldText = Song.Mood
Case 8
FieldText = Song.Occasion
Case 9
FieldText = Song.Quality
Case 10
FieldText = Song.Custom1
Case 11
FieldText = Song.Custom2
Case 12
FieldText = Song.Custom3
Case 13
FieldText = Song.Custom4
Case 14
FieldText = Song.Custom5
Case Else
msgbox("Invalid field selected?")
End Select
If Action = 0 Then 'Append
Preview = FieldText & Space & UserText
ElseIf Action = 1 Then 'Prepend
Preview = UserText & Space & FieldText
Else 'Insert
If Index = 0 Then
Preview = UserText & Space & Mid(FieldText,Index+1,len(FieldText))
ElseIf Index = len(FieldText) Then
Preview = Mid(FieldText,1,Index) & Space & UserText
Else
Preview = Mid(FieldText,1,Index) & Space & UserText & Space & Mid(FieldText,Index+1,len(FieldText))
End If
End If
If Not tmp.Common.ChildControl("Preview") Is Nothing Then
tmp.Common.ChildControl("Preview").Text = Preview
End If
End If
SDB.IniFile.IntValue("SimpleAppend","Action") = Action
SDB.IniFile.IntValue("SimpleAppend","Field") = Field
SDB.IniFile.StringValue("SimpleAppend","UserText") = UserText
End Sub
Sub DoIt(obj)
' msgbox("Doing It, there is no undo!")
If Songlist.Count > 0 Then
Dim Txt, Track, x, NewSongs, Song, Preview, NewField
Set NewSongs = SDB.NewSongList
For x = 0 to Songlist.Count-1
Set Song = Songlist.Item(x)
Select Case Field
Case 0
FieldText = Song.Title
Case 1
FieldText = Song.Comment
Case 2
FieldText = Song.ArtistName
Case 3
FieldText = Song.AlbumArtistName
Case 4
FieldText = Song.AlbumName
Case 5
FieldText = Song.Genre
Case 6
FieldText = Song.Tempo
Case 7
FieldText = Song.Mood
Case 8
FieldText = Song.Occasion
Case 9
FieldText = Song.Quality
Case 10
FieldText = Song.Custom1
Case 11
FieldText = Song.Custom2
Case 12
FieldText = Song.Custom3
Case 13
FieldText = Song.Custom4
Case 14
FieldText = Song.Custom5
Case Else
msgbox("Invalid field selected?")
End Select
If Action = 0 Then 'Append
NewField = FieldText & Space & UserText
ElseIf Action = 1 Then 'Prepend
NewField = UserText & Space & FieldText
Else 'Insert
If Index = 0 Then
NewField = UserText & Space & Mid(FieldText,Index+1,len(FieldText))
ElseIf Index = len(FieldText) Then
NewField = Mid(FieldText,1,Index) & Space & UserText
Else
NewField = Mid(FieldText,1,Index) & Space & UserText & Space & Mid(FieldText,Index+1,len(FieldText))
End If
End If
Select Case Field
Case 0
Song.Title = NewField
Case 1
Song.Comment = NewField
Case 2
Song.ArtistName = NewField
Case 3
Song.AlbumArtistName = NewField
Case 4
Song.AlbumName = NewField
Case 5
Song.Genre = NewField
Case 6
Song.Tempo = NewField
Case 7
Song.Mood = NewField
Case 8
Song.Occasion = NewField
Case 9
Song.Quality = NewField
Case 10
Song.Custom1 = NewField
Case 11
Song.Custom2 = NewField
Case 12
Song.Custom3 = NewField
Case 13
Song.Custom4 = NewField
Case 14
Song.Custom5 = NewField
Case Else
End Select
NewSongs.Add(Song)
SDB.ProcessMessages
Next
Else
msgbox("No Tracks Selected")
End If
NewSongs.UpdateAll
End Sub
Sub Install()
Dim scriptName : scriptName = "SimpleAppend"
'Add scripts.ini entries
Dim inip : inip = SDB.ApplicationPath&"Scripts\Scripts.ini"
Dim inif : Set inif = SDB.Tools.IniFileByPath(inip)
If Not (inif Is Nothing) Then
inif.StringValue(scriptName,"Filename") = "SimpleAppend.vbs"
inif.StringValue(scriptName,"Procname") = "Start"
inif.StringValue(scriptName,"DisplayName") = "Simple Append"
inif.StringValue(scriptName,"Description") = "Adds text to selected field"
inif.StringValue(scriptName,"Language") = "VBScript"
inif.StringValue(scriptName,"ScriptType") = "0"
SDB.RefreshScriptItems
End If
End Sub
[/code]