Author Topic: Migrate from JScript to JavaScript  (Read 10944 times)

Helmut Ortmann

  • EA User
  • **
  • Posts: 970
  • Karma: +42/-1
    • View Profile
Migrate from JScript to JavaScript
« on: January 07, 2022, 10:44:58 pm »
Hello,

is there an easy way to migrate a great bunch of code from JScript to JavaScript?

Can I call JavaScript from JScript within EA?

Thanks and best regards,

Helmut
Coaching, Training, Workshop (Addins: hoTools, Search&Replace, LineStyle)

Sunshine

  • EA Practitioner
  • ***
  • Posts: 1311
  • Karma: +120/-10
  • Its the results that count
    • View Profile
Re: Migrate from JScript to JavaScript
« Reply #1 on: January 09, 2022, 09:49:38 am »
I was looking at this problem about 6 months ago when working for another company. Came across https://support.smartbear.com/testcomplete/docs/scripting/specifics/javascript-for-jscript-users.html which provided some pointers.
The other option I considered was using regular expressions to parse the files and convert the differences between JScript and JavaScript. The two main things IIRC in my code were dealing with active x objects and enumerator for navigating collections. I ended up writing some code using regular expressions to convert using to ActiveXObject. Sorry don't have a copy as I've changed companies.
Can't remember for sure but think I might have used this. The Demo doesn't seem to work now so maybe browser dependent. Says something about  IE6. Think it worked for me with IE 10 or 11 can't remember now and don't have that available anymore at my new company nor my home machine.
https://slingfive.com/pages/code/scriptConverter/default.html
Happy to help
:)

Helmut Ortmann

  • EA User
  • **
  • Posts: 970
  • Karma: +42/-1
    • View Profile
Re: Migrate from JScript to JavaScript
« Reply #2 on: January 10, 2022, 07:46:46 am »
Hello Sunshine,

Thanks for your valuable pointers.

The address https://slingfive.com/pages/code/scriptConverter/default.html seems not to work (service is unavailable).


Best regards,

Helmut
Coaching, Training, Workshop (Addins: hoTools, Search&Replace, LineStyle)

Sunshine

  • EA Practitioner
  • ***
  • Posts: 1311
  • Karma: +120/-10
  • Its the results that count
    • View Profile
Re: Migrate from JScript to JavaScript
« Reply #3 on: January 10, 2022, 08:09:17 pm »
You are right that link used to point to a page to run an example vbscript to jscript converter but now get 403 access denied so the admin must have removed public access.

Thought the code might help get you started.

The js code underneath can still be accessed so try these links and hurry before they deny access to these too.
https://slingfive.com/pages/code/scriptConverter/scriptConverter.js
https://slingfive.com/pages/code/scriptConverter/vbs2js_conversions.js
https://slingfive.com/pages/code/scriptConverter/js2vbs_conversions.js

Not sure if its written in JScript (proabably) or JavaScript.



Happy to help
:)

shimon

  • EA User
  • **
  • Posts: 133
  • Karma: +5/-0
    • View Profile
Re: Migrate from JScript to JavaScript
« Reply #4 on: January 22, 2025, 02:06:29 am »
Hi,

Does anybody have experience using ChatGPT for these conversions?

Thanks,
Shimon

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13303
  • Karma: +557/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Migrate from JScript to JavaScript
« Reply #5 on: January 22, 2025, 02:17:55 am »
Does anybody have experience using ChatGPT for these conversions?
I've done some experiments (mainly to translate vbscripts to javascript) and using an AI works mostly, but you'll still need some manual work.
The downside is that the code generated by such an AI always looks plausable, so it's hard to spot the errors.

I also tried https://www.codeporting.ai/ and I was very happy with the results, especially since you can include additional instructions (like leaving the "as EA.Element" and the likes)

Geert

shimon

  • EA User
  • **
  • Posts: 133
  • Karma: +5/-0
    • View Profile
Re: Migrate from JScript to JavaScript
« Reply #6 on: January 22, 2025, 02:21:50 am »
Thanks Geert,
Have a great day,
Shimon

shimon

  • EA User
  • **
  • Posts: 133
  • Karma: +5/-0
    • View Profile
Re: Migrate from JScript to JavaScript
« Reply #7 on: February 20, 2025, 04:21:15 pm »

I also tried https://www.codeporting.ai/ and I was very happy with the results, especially since you can include additional instructions (like leaving the "as EA.Element" and the likes)

Geert

Hi Geert,
Do you have the inline instructions you used documented?
I managed to get this:
let selectedConnector; // as EA.Connector

It's better than nothing, but still leaves another step to the process.
Thanks,
Shimon


Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13303
  • Karma: +557/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Migrate from JScript to JavaScript
« Reply #8 on: February 20, 2025, 07:28:44 pm »
This is the script (including AI instructions I was using to test the conversions)

for the as EA.Element this was
'<ai>translate statements such as "dim myElement as EA.Element" as "var myElement as EA.Element". The "as EA.xxx" should always stay, and the variables should be declared in a separate line if there is a "as EA.xxx" statement present</ai>'

The weird thing with those AI things is that is feels like it is non deterministic. Sometimes the result is different if you run the same input a second time.

Code: [Select]
'<ai>technology stack: Enterprise Architect</ai>'



'<ai>"msgbox" should be translated to Session.Prompt(text, prompt) the possible values for prompt are: promptOK, promptYESNO, promptYESNOCANCEL,promptOKCANCEL and there are no other parameters allowed</ai>
'<ai>if now() is used in a string, translate it to new Date().toLocaleString()</ai>


'[path=\Projects\Project H\Temp]
'[group=Temp]
option explicit

'<ai>Keep all !INC statements unchanged except for "EAConstants-VBScript" which should be translated to "EAConstants-JavaScript"</ai>
!INC Local Scripts.EAConstants-VBScript


'
' Script Name: SampleScriptForTestingConversion
' Author: Geert Bellekens
' Purpose: Contains a rich set of constructs so that we can test the translation
' Date: 2025-01-22
'
const outPutName = "Cleanup Reporting Model"
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

sub Main()

'create output tab
Repository.CreateOutputTab outPutName
Repository.ClearOutput outPutName
Repository.EnsureOutputVisible outPutName
   '<ai>translate statements such as "dim myElement as EA.Element" as "var myElement as EA.Element". The "as EA.xxx" should always stay, and the variables should be declared in a separate line if there is a "as EA.xxx" statement present</ai>'
dim selectedPackage as EA.Package
set selectedPackage = Repository.GetTreeSelectedPackage
if not selectedPackage is nothing then
'report progress
Repository.WriteOutput outPutName, now() & " Starting " & outPutName & " for package '" & selectedPackage.Name & "'"  , 0
'do the actual work
doTheActualWork selectedPackage
'report progress
Repository.WriteOutput outPutName, now() & " Finished " & outPutName & " for package '" & selectedPackage.Name & "'"  , 0
else
Msgbox "Select a package in the project browser before running this script",vbOKOnly+vbExclamation,"No Package Selected!"
end if
end sub

function doTheActualWork(package)
'write to a file
writeToFile "c:\temp\testFile.txt", "Test contents"
'read a file
dim readText
readText = readFile("c:\temp\testFile.txt")
Repository.WriteOutput outPutName, now() & " Read the following '" & readText & "'"  , 0
'create a dictionary
'create an arraylist
dim arrayList
    '<ai>translate CreateObject("System.Collections.ArrayList") as New Array(), for other CreateObject(name) statements use new COMObject(name)</ai>'
set arrayList = CreateObject("System.Collections.ArrayList")
arrayList.Add "value1"
'parse an xml file
'create an excel file

end function

function readFile(path)
Dim fso
dim fsoFile
dim ts
dim contents
Set fso = CreateObject("Scripting.FileSystemObject")
if fso.FileExists(path) then
set fsoFile = fso.GetFile(path)
set ts = fsoFile.OpenAsTextStream(ForReading, TristateUseDefault)
If Not ts.AtEndOfStream Then
contents = ts.ReadAll
else
contents = ""
end if
end if
'return contents
readFile = contents
end function

function writeToFile(path, contents)
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
'then create file
Set MyFile = fso.CreateTextFile(path, True, False) 'second true for unicode
MyFile.Write(contents)
MyFile.Close
end function

main

Geert

shimon

  • EA User
  • **
  • Posts: 133
  • Karma: +5/-0
    • View Profile
Re: Migrate from JScript to JavaScript
« Reply #9 on: February 20, 2025, 09:47:48 pm »
Hi Geert,
Thanks a lot.
I see that you also address the issue of msgBox, which I also just got stuck on.
All in all, it does a pretty decent job.
I hope that I learn as I go, and it doesn't cause me to be lazy.
All the best,
Shimon

shimon

  • EA User
  • **
  • Posts: 133
  • Karma: +5/-0
    • View Profile
Re: Migrate from JScript to JavaScript
« Reply #10 on: February 26, 2025, 08:17:34 pm »
Hi,
I am trying to port my scripts to Javascript, so that they can be invoked by a Model Addin, which does not necessitate installation.  I didn't get there yet.
There is a very good demonstration by our member  Guillaume . This is the link.
https://www.umlchannel.com/en/enterprise-architect/item/359-enterprise-architect-model-based-addin

I might post some suggestions for newbies like me.

In any case, these addins (and Classes called by these addins) do not allow the use of as EA.Element ( or any other as EA.xxxxx). Maybe there is an Import that I am missing in my installation.
So maybe the ; // as EA.xxxxx is not such a bad result, after all.

As far as I see now, the statement:
!INC Local Scripts.EAConstants-JScript causes an error, so some the use of any constants that we may be used to using is going to cause trouble. This includes the return type of an InputBox.
These must be some easy way to declare these constants in the Model Addin environment (or any other place in the Model) and use them via creating a Dependency. If I come across a solution, I'll  share it here.


Shimon

« Last Edit: February 26, 2025, 08:50:29 pm by shimon »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13303
  • Karma: +557/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Migrate from JScript to JavaScript
« Reply #11 on: February 26, 2025, 08:53:02 pm »
The "as EA.Element" parts are in fact invalid syntax for vanilla VBScript (and I suspect Javascript as well)

In the regular scripts, EA strips those statements from the scripts before executing.
I guess they didn't implement the same strategy in the model-addins.

Geert