Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save a file from Enterprise Architect Script

I'm tring to create a script thats shows a dialog where I can select a path to save a file. The following got it almost, but this is to open a file, not save it.

var filePath = OpenCSVFileDialog();
var fileName = GetFilenameFromPath(filePath);    


function OpenCSVFileDialog()
{
    var Project;
    var Filename, FilterString, Filterindex, Flags, InitialDirectory, OpenorSave, filepath;

    Filename = "";
    FilterString = "CSV Files (*.csv)|*.csv|All Files (*.*)|*.*||";
    Filterindex = 1;
    Flags = 0;
    InitialDirectory = "";
    OpenorSave = 0;

    Project = Repository.GetProjectInterface();
    filepath = Project.GetFileNameDialog(Filename, FilterString, Filterindex, 
                                         Flags, InitialDirectory, OpenorSave);

    return filepath;
}

function GetFilenameFromPath(filePath)
{
    var bsindex, fileName;

    // find the last backspace in the file path
    bsindex = filePath.lastIndexOf("\\");

    if (bsindex > 0)
    {
        // get the name of the file only - minus the directory path
            fileName = filePath.substring(bsindex+1, filePath.length);
    }
    else
    {
        fileName = filePath;
    }

    return fileName;
}
like image 754
byandreee Avatar asked Nov 25 '25 07:11

byandreee


2 Answers

To get the file path, use the Jscript-Dialog script available in the EAScriptLib Script group, it will prevent you from rewriting the whole code for getting the dialog.

(To reference another script, use !INC, in this case, put !INC EAScriptLib.JScript-Dialog to the top of your script)

Call DLGSaveFile(filterString,filterIndex) and provide:

  • the filter string, in your case its CSV Files (*.csv)|*.csv|All Files (*.*)|*.*||
  • The index of the filter(in the previous point) you want to use, which you are already providing

It will return the path of the file.

You can use Project.GetFileNameDialog, it's the same thing but with more parameters also, here's a link

If you use the CSV library to create your CSV file, then your file should be exported once you call the CSVEExportFinalize() function. You must have called CSVEExportInitialize(filepath,columns,exportcolumsHeadings) first

For any file, it can be done with JScript and VBScript, but not javascript

JScript

var fso = new ActiveXObject("Scripting.FileSystemObject");
var a = fso.CreateTextFile("c:\\testfile.txt", true);
a.WriteLine("This is a test.");
a.Close();

VB

Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("c:\testfile.txt", True)
MyFile.WriteLine("This is a test.")
MyFile.Close

Microsoft Reference

like image 61
Mart10 Avatar answered Nov 28 '25 17:11

Mart10


GetFileNameDialog is a new function in V13. Try different parameters for the OpenOrSave parameters (the docu might be flawed as well). Sparx always ships banana software! If it doesn't work either, send a bug report.

Alternatively (preferred!) use the operation pointed out by @Hue.

like image 22
qwerty_so Avatar answered Nov 28 '25 17:11

qwerty_so



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!