Creating script

Let's describe creating a script in an example. We will create a script showing a message on a program startup. The creation process consists of three steps:

  1. Creating a directory

  2. Writing a script descriptor

  3. Writing a script code


Step #1: Create Directory

In the plugins/com.nomagic.magicdraw.jpython folder, create a scripts sub-folder, and then a folder for the particular script. For example, plugins/com.nomagic.magicdraw.jpython/scripts/example

Step #2: Write Script Descriptor

A script descriptor is a file written in XML and named script.xml. The script descriptor provides information about a script file to run, version of script, ID, and other.

In the created directory, create a script.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<script
id="example 1"
name="Simple menu item"
version="1.0"
provider-name="No Magic"
script-file="main.py"
requiresApi="1.0">
</script>

The following table describes the script.xml file structure:

Element

Description

script

Attributes

Name

Description

id

A scrip ID, should be unique. Used to identify a script. Example: “my.first.script.0”

name

A script name. No strict rules applied to this attribute. Example: “Example script”

version

A script version. Allows numbers separated with one dot value. Examples: “1.0”, “0.1”

provider-name

A script provider name. A company or an author name. Example: "No Magic"

script-file

A relative path to a script file. This file will be executed. Example:”main.py”

requires-api

A program API version required by a script. Example:”1.0”

Step #3: Write Script Code

Then in the same directory, create the main.py file:

from javax.swing import JOptionPane
 
# Script starts here
print "Starting script, descriptor", pluginDescriptor
JOptionPane.showMessageDialog( None, "I am a script!!!")

After saving files, restart your modeling tool. On a program startup, a message dialog should appear.

images/download/attachments/47117575/I_am_script.png

Variables Passed to Script

The program passes the one variable to the script pluginDescriptor. This variable contains information from the parsed script.xml file. A variable is an instance of a com.nomagic.magicdraw.jpython.PythonPluginDescriptor class.

A script can retrieve the script directory and other necessary information from the pluginDescriptor variable. There is no need to change any other fields for this variable.