-
Notifications
You must be signed in to change notification settings - Fork 17
Scripts in Enterprise Architect
Enterprise Architect offers the capability to edit, run, debug and manage scripts. Note: scripting is not available in the Professional and Desktop editions [Sparx].
In short, EA serves as a ActiveX scripting host for 3 ActiveX scripting engines, each supporting a different language, more specifically
- a VBScript engine (installed by default on Windows)
- a JScript engine (installed by default on Windows)
- a JavaScript engine (in the EA installation?)
VBScript is a subset of Visual Basic [Wikipedia]. JScript/JavaScript and Java are completely unrelated to each other [Wikipedia], but the syntax and naming conventions are similar. So depending on one's background, one may prefer one or the other language. For more information about VBScript and JScript, see VBScript and JScript Users' Guides and Language References.
Through scripts in EA, the internals of EA models can be accessed. E.g. the author of an class can be changed, a list of the elements in a package can be retrieved or tagged values can be added to elements fulfilling certain criteria. All the objects, methods, operators, etc. available in JScript, VBScript and JavaScript are of course also available in scripts in EA. This includes e.g. array handling, regular expressions and file processing.
Suggested naming conventions for scripts (prefixes):
- CHG: scripts that change the model
- DOC scripts that produce documentation of the model
- INFO scripts that show information about the eap-file or related stuff
- VAL scripts that validate models
- scripts that start with an underscore should not/cannot be run directly
When using VBScript or JScript, files can be read and written through FileSystemObject
. An object is created as follows:
' VBScript
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
// JScript
var fso;
fso = new ActiveXObject("Scripting.FileSystemObject");
See this introduction for more information about how to use FileSystemObject
.
UTF-8 is not supported by FileSystemObject
, so it may be a better option to use ADODB.Stream
instead. ADODB.Stream
is an ActiveX Data Object, and is as such part of any Windows installation in Windows Vista or later Windows version [Microsoft/Windows Data Access Components (MDAC/WDAC)].
// JScript
var fileName;
// fileName = ... full path
var stream = new ActiveXObject("ADODB.Stream");
stream.Type = 2 // text data (as apposed to binary data)
stream.Charset = "utf-8";
stream.LineSeparator = -1; // carriage return line feed
stream.Open();
// ...
stream.WriteText("text", 1);
// ...
stream.SaveToFile(fileName, 2);
stream.Close();
!INC
EA is shipped with examples, especially in JScript and VBScript, in the "Local Scripts" script group (Tools | Scripting > Scripts).
EA also contains some helpful scripting functions that can be used in other scripts. In order to use these functions, the EAScriptLib MDG Technology must be enabled (Settings | MDG Technologies).
Example scripts on this page: https://github.com/ISO-TC211/UML-Best-Practices/tree/master/Scripts
Library of wrappers and scripts for Enterprise Architect automation using VBScript: https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library. See also this tutorial: http://community.sparxsystems.com/tutorials/919-how-to-use-the-enterprise-architect-vbscript-library.
- EA User Guide (in EA installation):
- section Automation and Scripting | Scripting: information about the GUI for scripting
- section Automation and Scripting | Enterprise Architect Object Model | Reference: provides detailed information on all the objects available in the object model provided by the Automation Interface
- VBScript and JScript Users' Guides and Language References (website)
- Scripting Enterprise Architect (book)
- Microsoft® PowerShell, VBScript, and JScript® Bible
- Introduction to Scripting using Enterprise Architect (webinar)