Invoke COM objects from a javascript?

Help with writing and running scripts

Invoke COM objects from a javascript?

Postby namlook » Wed Jun 13, 2007 7:15 am

Is it not possible to create COM objects with the java script engine that comes with UltraEdit?

Why does not the lines below work?

var obj = WScript.CreateObject("MyComServer.MyObject");
var res = obj.MyMethod("test");

I get this error message:

An error occurred on line 7:
obj = WScript.CreateObject("MyComServer.MyObject");
Script failed.

These lines of code executes just fine when I run the script outside UltraEdit (e.g. with the WScript.exe engine).

Regards
/Thomas
User avatar
namlook
Newbie
 
Posts: 2
Joined: Tue Jun 12, 2007 11:00 pm

Re: Invoke COM objects from a javascript?

Postby Mofi » Wed Jun 13, 2007 7:47 am

Wscript.exe is the Windows Script interpreter and not a JavaScript interpreter. To be more precise, it contains (or runs) a VBScript and a JScript interpreter. JScript is not JavaScript, although similar. You should know that when you are writing Windows scripts. Have you not read the documentation. Well, I have never written a Windows script and so I have also not read the documentation about Windows scripts.

And in help of UltraEdit you can read at top of the page titled Scripting Demo and Tutorial:

Scripting in UltraEdit/UEStudio is enabled through embedding of the JavaScript engine. The scripting engine supports the core functionality of JavaScript 1.7. Further information on JavaScript may be found on the associated Mozilla site (http://developer.mozilla.org/en/docs/JavaScript).
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4066
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Invoke COM objects from a javascript?

Postby namlook » Wed Jun 13, 2007 7:52 am

Mofi, I'm aware of that the scripting engine embedded in UltraEdit supports the core functionality of JavaScript 1.7.

But my question remains: Is is not possible to create COM objects in a script file that is executed from UltraEdit?

Regards
/Thomas
User avatar
namlook
Newbie
 
Posts: 2
Joined: Tue Jun 12, 2007 11:00 pm

Re: Invoke COM objects from a javascript?

Postby Mofi » Wed Jun 13, 2007 8:00 am

The UltraEdit JavaScript engine knows 3 objects: the UltraEdit application object, the UltraEdit document object and since v13.10 the UltraEdit outputWindow object. I'm not a JavaScript expert and I also have never tried to create a new object. I think, it is not possible, but maybe an expert can disprove my statement here.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4066
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Invoke COM objects from a javascript?

Postby cawoodm » Wed Jun 20, 2007 1:02 pm

I get this error message:

An error occurred on line 7:
obj = WScript.CreateObject("MyComServer.MyObject");
Script failed.


Technically the following is correct in any JavaScript environment:
var dict = new ActiveXObject("Scripting.Dictionary");

However it seems UltraEdit does not support it as I get the same error message you!
cawoodm
Basic User
Basic User
 
Posts: 27
Joined: Mon Mar 27, 2006 12:00 am

Re: Invoke COM objects from a javascript?

Postby Jurriaan » Wed Jun 20, 2007 10:57 pm

According to the mozilla foundation, core javascript consist of the following objects:

Array
Boolean
Date
Error
Function
java
JavaArray
JavaClass
JavaObject
JavaPackage
Math
netscape
Number
Object
Packages
RegExp
String
sun

But that is their core. The real core consists of:

Array
Boolean
Date
Error
Function
Math
Number
Object
RegExp
String

Those are the only objects you can trust to be in any javascript enabled program. And UltraEdit added the 3 objects mofi mentioned earlier. ActiveXObject has never and will never be a part of core javascript nor will WScript. They are very platform specific objects, and ActiveXObject will only work in an ActiveX supporting browser (=Internet Explorer).

This is also by design. Javascript does not have the capabilities to access the file system. That you are able to create a new file with javascript in UE is because the developers extended the core javascript with it's own object, which hooks javascript to the file system. As such it would be possible for the developers to create such a hook to COM objects. But it is very unlikely that this will happen any time soon. I think javascript is introduced step by step in UltraEdit.

so yes mofi, it is possbile to create new objects

Code: Select all
var o = new Object();
o.foo = 'hello';
UltraEdit.outputWindow.write(o.foo);


or even complete prototyped classes if you wish to do so.

Code: Select all
function Foo(nr1, nr2)
{
   this.nr1 = parseInt(nr1);
   this.nr2 = parseInt(nr2);
}

Foo.prototype.nr1 = ''; // instance var
Foo.prototype.nr2 = ''; // instance var
Foo.version = '1'; // static var
Foo.prototype.add = function()
{
   return String(this.nr1 + this.nr2)
}

var bar1 = new Foo(10, 10);
var bar2 = new Foo(3, 3);

UltraEdit.outputWindow.write(Foo.version);
UltraEdit.outputWindow.write(bar1.add());
UltraEdit.outputWindow.write(bar2.add());
User avatar
Jurriaan
Basic User
Basic User
 
Posts: 16
Joined: Wed Aug 30, 2006 11:00 pm

Re: Invoke COM objects from a javascript?

Postby Dustin » Mon Aug 27, 2007 6:17 am

Windows has included Windows Scripting Host for scripting applications, the operating system, etc. JScript is among the languages it supports. JScript's syntax is nearly identical to JavaScript. Hence dealing with COM with the JavaScript language syntax is not an issue.

The problem is that UltraEdit doesn't support COM at all. Rather than processing everything internally, it should use the COM system and the WSH script runtime engine. But for some reason it does not. And without a way to exchange data with other processes and such, scripting is severely limited. Honestly I find it to be useless at present. UltraEdit still hasn't caught up w/ Windows 98!
User avatar
Dustin
Newbie
 
Posts: 5
Joined: Sun Mar 19, 2006 12:00 am


Return to Scripts