Book a Demo

Author Topic: JavaScript: Writing to File?  (Read 21520 times)

Adam P

  • EA Novice
  • *
  • Posts: 11
  • Karma: +0/-0
    • View Profile
JavaScript: Writing to File?
« on: January 15, 2015, 08:54:54 am »
Is there any way to write to a file using JavaScript / JScript from EA?

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: JavaScript: Writing to File?
« Reply #1 on: January 15, 2015, 09:35:59 am »
Both JScript and JavaScript in EA can create an ActiveX object. For Javascript you'll find an example under the simulation section of the help.

See http://msdn.microsoft.com/en-us/library/aa242706%28v=vs.60%29.aspx for the object to create and how to use it.

Adam P

  • EA Novice
  • *
  • Posts: 11
  • Karma: +0/-0
    • View Profile
Re: JavaScript: Writing to File?
« Reply #2 on: January 15, 2015, 01:06:42 pm »
Thanks, that produces the error "CreateObject is not defined" (and a similar error with ActiveXObject). Is there some package I have to import?

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: JavaScript: Writing to File?
« Reply #3 on: January 15, 2015, 06:45:48 pm »
For JScript you have to use "new ActiveXObject" similar to this:

Code: [Select]
var openFileDialog = new ActiveXObject("MSComDlg.CommonDialog");
For JavaScript use "new COMObject" similar to this:
Code: [Select]
var obj = new COMObject("WMPlayer.OCX");
See also http://www.sparxsystems.com/cgi-bin/yabb/YaBB.cgi?num=1256123373/1#1

Geert

Adam P

  • EA Novice
  • *
  • Posts: 11
  • Karma: +0/-0
    • View Profile
Re: JavaScript: Writing to File?
« Reply #4 on: January 16, 2015, 10:40:59 am »
Thanks again, looks like I'm getting closer. I now get the error "Failed to create object from CLSID". I tried both "Scripting.FileSystemObject" and ""{0D43FE01-F093-11CF-8940-00A0C9054228}" (which is the CLSID for Scripting.FileSystemObject). I also confirmed that the CLSID is in the registry and associated with Scripting.FileSystemObject and scrrun.dll, and that scrrun.dll is in the Windows\System32 folder.

Can someone else drop these lines into a new JavaScript file and see if it works for them?

      var fs = new COMObject ("Scripting.FileSystemObject")
      var fs = new COMObject ("{0D43FE01-F093-11CF-8940-00A0C9054228}")

Any other suggestions from the pros on ways to write to a file from JavaScript or get COM to work?

Thanks again.

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: JavaScript: Writing to File?
« Reply #5 on: January 16, 2015, 11:06:03 am »
It's possible that it's blocked by Windows (because it's a potential security hole in IE) can't remember the details.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: JavaScript: Writing to File?
« Reply #6 on: January 16, 2015, 06:29:01 pm »
I seem to have the same problem in JavaScript, but not in Jscript.

This code works in Jscript
Code: [Select]
function main()
{
      var fso = new ActiveXObject("Scripting.FileSystemObject");
      a = fso.CreateTextFile("c:\\temp\\testfile.txt", true);
      a.WriteLine("This is a test.");
      a.Close();
}

main();

Geert

Adam P

  • EA Novice
  • *
  • Posts: 11
  • Karma: +0/-0
    • View Profile
Re: JavaScript: Writing to File?
« Reply #7 on: January 16, 2015, 06:47:51 pm »
After installing the Microsoft Script Debugger that works.

Thanks again, I really appreciate the help.