Introduction to scripting: Difference between revisions

From MediaMonkey Wiki
Jump to navigation Jump to search
No edit summary
Line 1: Line 1:
== How to create a custom MediaMonkey script ==
There are 2 major kinds of MediaMonkey scripts:
*'''Normal scripts''' are located in the ''Scripts'' folder and are executed according to the script's entry in the [[#Format of the Scripts.ini file|''Scripts\Scripts.ini'' file]].
*'''Auto-scripts''' are located in the ''Scripts\Auto'' folder and its OnStartup method is executed when MediaMonkey starts.


# Create a file in Scripts folder, e.g. ‘MyScripts.vbs’.
It's recommended to create [http://en.wikipedia.org/wiki/VBScript VBScript] (*.vbs) scripts. [http://en.wikipedia.org/wiki/JScript JScript] (*.js) scripts are possible too, but they have only limited support.
# Write a VB script there and put a callable code to some procedure, e.g.  
#: Sub CallMe
#:  ‘ … write some code here
#: End Sub</source>
# Define the new script in Scripts.ini file (see below how to do it).


You can get more information about scripting in general at [http://www.microsoft.com/scripting] (for example VBScript and JScript documentation is there).
Next to the scripts that are launched by MediaMonkey (MediaMonkey scripts), you can also work with MediaMonkey through ''external'' scripts and applications. This is explained at the end of this page.


In order to work with MediaMonkey’s objects from your scripts, there is 'SDB' object always present so that you can access its properties and methods. In case you work outside of MediaMonkey’s scripts (e.g. you create an ASP page) you can create this object by the following code:
== How to create a normal script ==
 
<ol>
<li>Create a plain text file in the ''Scripts'' folder, e.g. ''MyNormalScript.vbs''.</li>
<li>Write your VBScript script and make sure you have a procedure that can be called, e.g.
<source lang="vb">Sub CallMe
 
    ' This script will pop up a simple message box when the procedure CallMe is called.
    MsgBox "CallMe was called by the action defined in the Scripts.ini file."
 
End Sub</source></li>
<li>Create a new entry for your script's ''CallMe'' procedure in the [[#Format of the Scripts.ini file|''Scripts.ini'' file]].</li>
</ol>
 
From within a MediaMonkey script, a reference to the SDB object (an instance of the [[SDBApplication]] class) is always present. By accessing its properties and methods, you can interact with many aspects of MediaMonkey. Check out the [[MediaMonkey Automation objects]] page for other MediaMonkey objects, that can be created starting from the SDB object.
 
The script ''Scripts\MediaMonkey init.vbs'' is always executed just before any other script. It contains some constants that can be used in your own script.
 
 
A good and quite complex example is ''Scripts\Export.vbs'' (distributed with MediaMonkey) which contains code that takes care of all export features of MediaMonkey. For more examples, check out the [[Sample scripts|sample scripts page]].
 
More information about scripting in general can be found at [http://msdn2.microsoft.com/en-us/library/ms950396.aspx Microsoft Scripting] (with official VBScript and JScript documentation).
 
== How to create an auto-script ==
 
<ol>
<li>Create a plain text file in the ''Scripts\Auto'' folder, e.g. ''MyAutoScript.vbs''.</li>
<li>Write your VBScript script and use the procedure ''OnStartup'' as start point for execution, e.g.
<source lang="vb">
<source lang="vb">
Dim SDB
Sub OnStartup
Set SDB = CreateObject( "SongsDB.SDBApplication")
 
</source>
    ' This script will pop up a simple "Hello World!" message box when you start MediaMonkey.
    MsgBox "Hello World!"


'MediaMonkey init.vbs' file is always executed before your script and therefore it contains some constants that can be later used in your script.
End Sub
</source></li>
</ol>


More information about properties and methods of SDB object about other objects and some samples can be found in [MediaMonkey Automation objects]. A good and quite complex example of a script is Export.vbs (distributed with MediaMonkey) which contains code that takes care of all export features of MediaMonkey.
All .vbs scripts in ''Scripts\Auto'' folder are automatically checked during MM start-up, and the '''OnStartup''' procedure (if present) it is called. Auto-scripts e.g. allow to add user interface enhancements like new menu or toolbar items, new option sheets, etc.


== Automatically called scripts ==
Except that these scripts are automatically executed when MediaMonkey starts, they behave identical as the normal scripts. They also get a reference to the SDB object, and the constants defined in ''Scripts\MediaMonkey init.vbs'' are always usable.


All .vbs scripts in Scripts\Auto folder are automatically checked during MM start-up and in case there is ‘OnStartup’ procedure present, it is called. This allows to add for example user interface enhancements like new menu or toolbar items, new option sheets, etc. For more information about how to do it see sample scripts and MM scripting reference.


== Format of Scripts\Scripts.ini file ==
== Format of the ''Scripts.ini'' file ==


This file is a standard .ini file where each section defines one custom script. Section starts with a section identifier [SectionName], where ‘SectionName’ is a unique name of the script. Then follow several lines the further describe the script:
<div style="float: right; margin-left: 1em; margin-bottom: 0.5em; clear: right; background-color:#F9F9F9; border:1px solid #CCCCCC; font-size:95%; padding:5px;">
<source lang="ini">
[AutoIncTrackN]
FileName=AutoIncTrackN.vbs
ProcName=AutoIncTrackNumbers
Order=1
DisplayName=Auto-&increment Track #s...
Description=Sequentially numbers Tracks
Language=VBScript
ScriptType=0
</source>
<div style="position: relative; top: -6px; left: 5px">Example ''Scripts.ini'' entry</div>
</div>
 
This ''Scripts\Scripts.ini'' file is a standard .ini file where each section defines one procedure in a script. The section starts with a section identifier [SectionName], where ‘SectionName’ is a unique name of the script entry.
The following lines further describe the script:
; Filename
; Filename
: Name of a file where the script is located, e.g. ‘MyScripts.vbs’.
: Path of a script file where the procedure is located, relative to the ''Scripts'' folder, e.g. ‘MyScripts.vbs’.
; ProcName
; ProcName
: Name of a procedure to be called when the script is executed in MM. This procedure must exist in the ‘Filename’ specified above.
: Name of a procedure to be called when the script is executed in MM. This procedure must exist in the ‘Filename’ specified above.
; ScriptType
; ScriptType
: Defines type of the script:
: Defines type of the script:
:* 0 = A standard script that appears in Tools\Scripts submenu.
:* 0 = A standard script that appears in the <code>Tools > Scripts</code> submenu.
:* 1 = An export script that can be found in File\Export submenu.
:* 1 = An export script that can be found in the <code>File > Export</code> submenu.
:* 2 = A procedure that is called whenever a new track is started. This script doesn’t appear in any menu, it’s simply called in the background. When this procedure is executed, there is 'CurrentTrack' variable of [[SDBSongData]] object defined and you can use it to get information about the track.
:* 2 = A procedure that is called whenever a new track is started. This script doesn’t appear in any menu, it’s simply called in the background. When this procedure is executed, a variable 'CurrentTrack' (object of the [[SDBSongData]] class) is defined, and you can use it to get information about the started track.
:*      3 = A search script. This script allows custom searched to be programmed in Auto-tag from Web dialog. This script type is a little more complex and so it has its own [[Search scripts|dedicated page]].
:*      3 = A search script. This script allows custom searches to be programmed in the "Auto-tag from Web" dialog. See [[Search scripts]] for more information about this more complex script type.
; Order
; Order
: Defines the order of the script in its section. These numbers are sorted and scripts are listed according to the order then.
: Defines the order of the script in its section. These numbers are sorted and scripts are listed according to the order.
; DisplayName
; DisplayName
: The script is listed under this name in MediaMonkey menu.
: The script is listed as menu item with this name.
; Description
; Description
: This is shown as a tooltip when mouse is over the script in menu.
: Message shown as tooltip when the mouse is moved over the menu item.
; Language
; Language
: Is usually VBScript, but can be any other scripting language, e.g. JScript.
: Is usually VBScript, but can be JScript (not recommended) too.
; Shortcut
; Shortcut
: Can specify a shortcut that will invoke the script in MM. You can use any of the string ‘Shift+’, ‘Ctrl+’ or ‘Alt+’ even combined together and followed either by a single letter or by a special key, which are: BkSp, Tab, Enter, Esc, Space, PgUp, PgDn, End, Home, Left, Up, Right, Down, Ins, Del.
: Specifies a shortcut that invokes the script in MM. You can use any of the strings ‘Shift+’, ‘Ctrl+’ or ‘Alt+’, even combined together and followed either by a single letter or a special key (BkSp, Tab, Enter, Esc, Space, PgUp, PgDn, End, Home, Left, Up, Right, Down, Ins, Del).
 
 
== About external scripts and applications ==
 
External scripts are called from outside MediaMonkey. They can be scripts that are executed from inside a MediaMonkey script (e.g. ''helper scripts''), or from other places outside MediaMonkey (normal *.vbs scripts, local html pages, ASP server pages, ...). The same information applies to applications that are not a part of MediaMonkey, but want to use its COM object (the COM and the scripting API are the same).
 
In order to work with MediaMonkey’s objects from outside a MediaMonkey script, you need to create a reference to the SDB object (an instance of the [[SDBApplication]] class) yourself. For external applications, make sure that you have added a reference to the SongsDB library first.
 
 
If MediaMonkey is already running, a call to the SDB object will return a reference to the running instance. If MediaMonkey is not running yet, it will be started and then a reference will be returned.
When MediaMonkey is started by an SDB call when it wasn't running before, it will automatically shut down when the external script/application exits. You can prevent this (and keep MediaMonkey open) by setting SDB's [[ISDBApplication::ShutdownAfterDisconnect|ShutdownAfterDisconnect]] property to False.
 
 
'''Visual Basic / VBScript example:'''
<source lang="vb">
Dim SDB : Set SDB = CreateObject("SongsDB.SDBApplication")
 
SDB.ShutdownAfterDisconnect = False    ' in case you want to keep an opened instance open after disconnecting the SDB object
</source>
'''C# example:'''
<source lang="csharp">
SongsDB.SDBApplicationClass SDB = new SongsDB.SDBApplicationClass();
 
SDB.ShutdownAfterDisconnect = false    // in case you want to keep an opened instance open after disconnecting the SDB object
</source>

Revision as of 22:02, 19 April 2007

There are 2 major kinds of MediaMonkey scripts:

  • Normal scripts are located in the Scripts folder and are executed according to the script's entry in the Scripts\Scripts.ini file.
  • Auto-scripts are located in the Scripts\Auto folder and its OnStartup method is executed when MediaMonkey starts.

It's recommended to create VBScript (*.vbs) scripts. JScript (*.js) scripts are possible too, but they have only limited support.

Next to the scripts that are launched by MediaMonkey (MediaMonkey scripts), you can also work with MediaMonkey through external scripts and applications. This is explained at the end of this page.

How to create a normal script

  1. Create a plain text file in the Scripts folder, e.g. MyNormalScript.vbs.
  2. Write your VBScript script and make sure you have a procedure that can be called, e.g.
    Sub CallMe
    
        ' This script will pop up a simple message box when the procedure CallMe is called.
        MsgBox "CallMe was called by the action defined in the Scripts.ini file."
    
    End Sub
  3. Create a new entry for your script's CallMe procedure in the Scripts.ini file.

From within a MediaMonkey script, a reference to the SDB object (an instance of the SDBApplication class) is always present. By accessing its properties and methods, you can interact with many aspects of MediaMonkey. Check out the MediaMonkey Automation objects page for other MediaMonkey objects, that can be created starting from the SDB object.

The script Scripts\MediaMonkey init.vbs is always executed just before any other script. It contains some constants that can be used in your own script.


A good and quite complex example is Scripts\Export.vbs (distributed with MediaMonkey) which contains code that takes care of all export features of MediaMonkey. For more examples, check out the sample scripts page.

More information about scripting in general can be found at Microsoft Scripting (with official VBScript and JScript documentation).

How to create an auto-script

  1. Create a plain text file in the Scripts\Auto folder, e.g. MyAutoScript.vbs.
  2. Write your VBScript script and use the procedure OnStartup as start point for execution, e.g.
    Sub OnStartup
    
        ' This script will pop up a simple "Hello World!" message box when you start MediaMonkey.
        MsgBox "Hello World!"
    
    End Sub

All .vbs scripts in Scripts\Auto folder are automatically checked during MM start-up, and the OnStartup procedure (if present) it is called. Auto-scripts e.g. allow to add user interface enhancements like new menu or toolbar items, new option sheets, etc.

Except that these scripts are automatically executed when MediaMonkey starts, they behave identical as the normal scripts. They also get a reference to the SDB object, and the constants defined in Scripts\MediaMonkey init.vbs are always usable.


Format of the Scripts.ini file

[AutoIncTrackN]
FileName=AutoIncTrackN.vbs
ProcName=AutoIncTrackNumbers
Order=1
DisplayName=Auto-&increment Track #s...
Description=Sequentially numbers Tracks
Language=VBScript
ScriptType=0
Example Scripts.ini entry

This Scripts\Scripts.ini file is a standard .ini file where each section defines one procedure in a script. The section starts with a section identifier [SectionName], where ‘SectionName’ is a unique name of the script entry. The following lines further describe the script:

Filename
Path of a script file where the procedure is located, relative to the Scripts folder, e.g. ‘MyScripts.vbs’.
ProcName
Name of a procedure to be called when the script is executed in MM. This procedure must exist in the ‘Filename’ specified above.
ScriptType
Defines type of the script:
  • 0 = A standard script that appears in the Tools > Scripts submenu.
  • 1 = An export script that can be found in the File > Export submenu.
  • 2 = A procedure that is called whenever a new track is started. This script doesn’t appear in any menu, it’s simply called in the background. When this procedure is executed, a variable 'CurrentTrack' (object of the SDBSongData class) is defined, and you can use it to get information about the started track.
  • 3 = A search script. This script allows custom searches to be programmed in the "Auto-tag from Web" dialog. See Search scripts for more information about this more complex script type.
Order
Defines the order of the script in its section. These numbers are sorted and scripts are listed according to the order.
DisplayName
The script is listed as menu item with this name.
Description
Message shown as tooltip when the mouse is moved over the menu item.
Language
Is usually VBScript, but can be JScript (not recommended) too.
Shortcut
Specifies a shortcut that invokes the script in MM. You can use any of the strings ‘Shift+’, ‘Ctrl+’ or ‘Alt+’, even combined together and followed either by a single letter or a special key (BkSp, Tab, Enter, Esc, Space, PgUp, PgDn, End, Home, Left, Up, Right, Down, Ins, Del).


About external scripts and applications

External scripts are called from outside MediaMonkey. They can be scripts that are executed from inside a MediaMonkey script (e.g. helper scripts), or from other places outside MediaMonkey (normal *.vbs scripts, local html pages, ASP server pages, ...). The same information applies to applications that are not a part of MediaMonkey, but want to use its COM object (the COM and the scripting API are the same).

In order to work with MediaMonkey’s objects from outside a MediaMonkey script, you need to create a reference to the SDB object (an instance of the SDBApplication class) yourself. For external applications, make sure that you have added a reference to the SongsDB library first.


If MediaMonkey is already running, a call to the SDB object will return a reference to the running instance. If MediaMonkey is not running yet, it will be started and then a reference will be returned. When MediaMonkey is started by an SDB call when it wasn't running before, it will automatically shut down when the external script/application exits. You can prevent this (and keep MediaMonkey open) by setting SDB's ShutdownAfterDisconnect property to False.


Visual Basic / VBScript example:

Dim SDB : Set SDB = CreateObject("SongsDB.SDBApplication")

SDB.ShutdownAfterDisconnect = False    ' in case you want to keep an opened instance open after disconnecting the SDB object

C# example:

SongsDB.SDBApplicationClass SDB = new SongsDB.SDBApplicationClass();

SDB.ShutdownAfterDisconnect = false    // in case you want to keep an opened instance open after disconnecting the SDB object