Introduction to Scripting using Enterprise Architect

Webinar May 2015
In this webinar you will learn how to:
  • Create, edit and execute scripts
  • Manipulate elements, diagrams and other model artifacts programmatically
  • Import and export scripts

Enterprise Architect implements a flexible and easy to use scripting capability based on industry standard Javascript, Microsoft JScript and VBScript. Geert Bellekens, owner of and Sparx Systems partner will demonstrate how automation can save hours of your time.

Scott Hebbard
Geert Bellekens

Explore the Example Model and Associated Scripts

We have provided the example Enterprise Architect model used in the webinar.
Example model: EAExample.eap Download the Enterprise Architect Example Model and associated scrips

Questions from the Audience

Click on a question to see the answer.


 What Version of Enterprise Architect was used throughout the webinar?

Enterprise Architect 12 (Build 1213).

 Is Scripting available in Enterprise Architect 11?

Yes. Scripting has been available in every version of Enterprise Architect since Enterprise Architect 7.5, Corporate Edition or higher.

Read the Enterprise Architect User Guide for more information about Automation and Scripting.

 Are the scripts shown in the webinar being made available to the public?

Yes. Click the link above to download the scripts.

 Can I gain access to the Script that was used to update the line styles?

Yes. Click the link above to download the scripts.

 Where can we download the model being used to demonstrate the scripts?

The EAExample model was used throughout the webinar. This model ships with every copy of Enterprise Architect.

 Does SPARX EA 12 bring something new relative to former EA 11 (for Scripting)?

Automation API support included to allow custom extensions for other frameworks and export formats.

Executing Scripts

 Can I schedule scripts to run automatically?

Sparx: Yes. You would have to write an executable that can be run as a scheduled task by Windows. Read the Community Site article to learn more information on running scheduled scripts in Windows Server.

Geert: See EA-Matic and its open source code on Github for an example.

 Is it possible to run scripts on several elements that have been selected?

Yes. The webinar demonstrated how a script could be applied to several diagram elements.

 Is it possible to execute several scripts, one after another?

Yes. You can include and execute scripts from other scripts using the !INC statement.

 Is there any way to launch a script from a UI button or hyperlink in Enterprise Architect?


Security, Sharing and Version Control

 Is there any security on the scripts such that if sharing a model certain users can run them and others cannot see them?

You can use security to allow or disallow Workflow Scripts, but unfortunately this is not available for Standard Scripts. Review the Enterprise Architect User Guide for more information on Workflow Scripts.

 Is it possible to HIDE a script?


 Is it possible to prevent users from modifying scripts in multi-user scenarios?


However, scripts can be included with MDG Technologies to prevent users from modification. Read the Enterprise Architect User Guide to learn more about adding Scripts to an MDG Technology file.

 Do you have best practices for version control management of these scripts?

It is probably best to organize that outside of EA using a version control system of some kind.

 Is it possible to store and handle the script under a version control repository, like SVN?

There is currently no integration with a version control system for scripts. Any version control will have to be done manually.

 What is the scope of these scripts? How can scripts can be 'shared', or re-used, across different Enterprise Architect files?

The scripts created in the webinar are stored in the model and available only in that model. You can export them and import them into another model in order to use them there as well as shown in the webinar.
Other options for sharing scripts amongst different model is storing them as a local script by saving the file in the appropriate folder:
<EA Installation Folder>\EA\Scripts.

You can also save scripts in an MDG technology file and distribute them that way.

 Is it possible to limit the context scripts to only some kind of elements? For example, let's say by element stereotype.

No. You can test the elements stereotype in the script and react appropriately.


 Where is the documentation for the Scripting API located?

The API is documented in the help file. See section Automation and Scripting | Enterprise Architect Object Model | Reference

Scripting Language Support

 Are all the embedded scripting languages equal? Is VBScript more reliable than JScript, for example?

The selection of a scripting Language was a matter of personal preference.

 Support for ECMAScript 6?


 Is there Java, or Python scripting planned?



 Is it possible to suppress a script context menu in case the menu is not supported? for example, if we click the "Webinar examples" node, the script menu is not displayed as if we click the menu it will only display "not supported" message?

With the “context” script groups you can limit the menu's to Element, Package, Diagram or Connector. If you need more fine-grained control you can consider writing an add-in or using the third party add-in EA-Matic written by Geert Bellekens.

 When an element is selected in the project browser is there quick bit of code to determine where it is in the tree structure? i.e.Model/package1/package 1.1/element 1

You'll have to work bottom up using EA.Element.ParentID , EA.Element.PackageID and EA.Package.ParentID
something like:

function getQualifiedNameElement(element, fqn)
'add my name to the fqn
fqn = & "/" & fqn
'first check if this element is nested under another element
if element.ParentID > 0 then
dim parentElement as EA.Element
set parentElement = Repository.GetElementByID(element.ParentID)
fqn = getQualifiedNameElement(parentElement, fqn)
'no parent element, so we go to the package
dim parentpackage as EA.Package
set parentPackage = Repository.GetPackageByID(element.PackageID)
fqn = getQualifiedNamePackage(parentPackage, fqn)
end if
'return fqn
getQualifiedNameElement = fqn
end function

function getQualifiedNamePackage(package, fqn)
'add my name to the fqn
fqn = & "/" & fqn
if package.ParentID > 0 then
dim parentpackage as EA.Package
set parentPackage = Repository.GetPackageByID(package.ParentID)
fqn = getQualifiedNamePackage(parentPackage, fqn)
end if
'return fqn
getQualifiedNamePackage = fqn
end function

 Is it possible to script the reverse-engineering from code files?

Yes. On the Project Interface there are operations like ImportDirectory() and ImportFile() that can be used to reverse engineer from code.

 Is it possible to export all diagrams in a project to PDF via scripting?

Yes. For a simple run of a report, read the Enterprise Architect User Guide topic on RunReport. Note that setting the Filename extension (.rtf or .pdf) sets the output type. For a more complex scenario there is the Document Generator Class available to do specific scripted reporting.

Project.RunReport(PackageGUID, "Diagram Report", "C:\Temp\MyDiagrams.pdf")

Or export each individual diagram to a separate PDF using Diagram.SaveAsPDF.

 Is there a library of examples that we can play with?

You can find examples in

 Could we perform script on a complete model in 1 click: example: change all line color depending on the tagged value.


 What are the typical use cases of scripting in Enterprise Architect, according to your experience?

There is a wide variety in use cases. Typically we see thing like

  • Model maintenance – Scripts to do some cleanup in the model
  • Helper functions – Scripts that help the user to do a complex or time consuming action.
  • Import/Export – Scripts that import or export stuff to and from the model
  • … and a lot more ...

 If you select an element on a diagram, does Repository.GetContextObject return an EA.Element or a DiagramObject? How can you test to see what type of thing is returned?

You can use Repository.GetContextItemType() to determine the type of the object in context.

 How can I discover more about Workflow Scripting?

 Is there any support for script obfuscation?


 Can the scripts perform VBA automation to, for instance, populate an Excel spreadsheet from Enterprise Architect?

Geert: Yes, you can use the Excel API to create an excel file from within a script. Or you could do it the other way around and write a VBA script in Excel and then use the EA API from within Excel. An example of such an Excel file importing elements into EA is available for free on

Sparx: MDG Integration for Office is designed to integrate with the Microsoft Office and Visio Suites. Import documents and spreadsheets into Enterprise Architect, providing a rich modeling experience. Learn more about MDG Integration for Microsoft Office and an interface with Microsoft Project.

 In the diagram group script, why is there double negative check e.g. "if not selectedDiagram is nothing then" rather than just "if selectedDiagram"?

This is standard VBScript syntax for checking if an object is initialized.

 I would like to understand what are things we SHOULD NOT DO while scripting.. any tips?

  • Do not write a huge script and only start testing it when it is finished. Instead write small bits and test them while progressing. Debugging large scripts is very hard.
  • Do not attempt to iterate the whole model in your scripts. Iterating is slow, and if you iterate the whole model that can easily take minutes or even hours for a big model. Instead use Repository.SQLQuery() to find the id's of the elements you need and instantiate them using Repository.GetElementByID()

 Is there any scripting template for adding tagged values to selected elements?

There is an example script in the Local Scripts that deals with tagged values. VBScript – Element Extras Example