Author Topic: Add-Ins and Tools EA15/ How to access external File within Add-Ins Event Code  (Read 4698 times)

a.becker-davet

  • EA Novice
  • *
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Hi All,

Is it possible to access an external file within an Add-Ins Event Code (For example, EA_OnPostNewElement) using MS JScript?
If yes, how can I proceed? This access worked if I just use a Script as follows:
   var fso = new ActiveXObject("Scripting.FileSystemObject");
   var a = fso.CreateTextFile("c:\\Test\\testfile1.txt", true);
   a.WriteLine("999");


If it is  not allowed, could you please give me a suggestion to implement this? I just need to have a variable in a File. This variable
should be incremented in the Add-Ins and then written back into the File. Every time that my „Class“ Object is created,
this variable will be copied into a Tagged Value and then the Counter will  be incremented in the File. Thanks ! ;=)

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 11315
  • Karma: +422/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
I think it is allowed, but it might be faster to simply try it.

Geert

a.becker-davet

  • EA Novice
  • *
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Hi Geert,

Thanks for your quick response!

I have tried, but unfortunately it did not work properly.  Here is the code for the Add-Ins "EA_OnPostNewElement":

var elementID, newElement;
elementID = Info.Get("ElementID");
newElement = Repository.GetElementByID(elementID);
Session.Prompt("Entering EA_OnPostNewElement... ",0);



selectedObject = Repository.GetContextObject();
   
var FSREAD = 1;
var n = 0;
// Setup file objects
var fsObject = new ActiveXObject( "Scripting.FileSystemObject" );
var file = fsObject.GetFile( "c:\\Test\\testfile1.txt" );
var inputStream = file.OpenAsTextStream( FSREAD, 0 );

// Read the file a row at a time
while ( !inputStream.AtEndOfStream )
{
   var r = inputStream.ReadLine();
   Session.Output(r);
   n = parseInt(r);
   n++;
}



if (selectedObject.Type == "Requirement")
{   
   Session.Prompt("Exiting from EA_OnPostNewElement because it is a requirement... ",0);
   newElement.Notes = "New =>Test diagram created by the ManageDiagramsExample script";
//   var lwr as EA.TaggedValue;
//   lwr = newElement.TaggedValues.GetAt(0);
//   lwr.Value = "123";
   for ( var i = 0 ; i < newElement.TaggedValues.Count ; i++ )
   {
      var lwr = newElement.TaggedValues.GetAt(i);


      // Extract string's number
      var number = lwr.Value.match(/\d+/) === null ? 0 : lwr.Value.match(/\d+/)[0];

      // Store number's length
      var numberLength = number.length

      // Increment number by n
      number = (parseInt(number) + n).toString();

      // If there were leading 0s, add them again
      while (number.length < numberLength) {
      number = "0" + number;
      }

      lwr.Value = lwr.Value.replace(/[0-9]/g, '').concat(number);


      
      newElement.Notes = "New Test diagram created by the ManageDiagramsExample script";
      lwr.Update();
   }
   
}


It did not come to the code "Session.Prompt("Exiting from EA_OnPostNewElement because it is a requirement... ",0);"
That means that something wrong occurred by the open/read of the file. The same script worked in a simple Script.

Do you have any idea what could happen there?

« Last Edit: June 23, 2021, 01:18:03 am by a.becker-davet »

a.becker-davet

  • EA Novice
  • *
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Debugging the code with printouts, I have realized that the issue occurs at the following code line:
// Setup file objects
var fsObject = new ActiveXObject( "Scripting.FileSystemObject" );


Maybe a "ActiveXObject" is not supported in the Event Handler?
« Last Edit: June 23, 2021, 01:22:48 am by a.becker-davet »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 11315
  • Karma: +422/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
I think you better get sparx support involved. If the exact same code works as a regular script, but not inside a model add-in...

I never use the model add-ins as I have my own add-in (EA-Matic) for that same functionality years before they came up with the model add-ins.

You send a bug report using this link: https://www.sparxsystems.com/support/forms/bug_report.html

Geert

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 7616
  • Karma: +96/-18
    • View Profile
ActiveXObject is JScript. In Javascript, including model add-ins, you need to use COMObject.

Code: [Select]
// Setup file objects
var fsObject = new COMObject( "Scripting.FileSystemObject" );
Eve

support@sparxsystems.com

a.becker-davet

  • EA Novice
  • *
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Hi Eve,

Thank you for your support on this! It is working now ! ;=)


Geert Bellekens

  • EA Guru
  • *****
  • Posts: 11315
  • Karma: +422/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
So I guess the exact same script didn't work outside of the model add-in.

Geert

a.becker-davet

  • EA Novice
  • *
  • Posts: 5
  • Karma: +0/-0
    • View Profile
So I guess the rules outside are not the same ... Just see the contents in the Script Library (JScript-CSV):

      // Setup file objects
      var fsObject = new ActiveXObject( "Scripting.FileSystemObject" );
      var file = fsObject.GetFile( fileName );
      var inputStream = file.OpenAsTextStream( FSREAD, 0 );




Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 7616
  • Karma: +96/-18
    • View Profile
You see the name of that script? "JScript-CSV" That script is intended to run as JScript, it won't work in any place where the Javascript engine is used.
Eve

support@sparxsystems.com