Why does javascript "Object.keys()' not work with Autohotkey? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
gues747747474

Why does javascript "Object.keys()' not work with Autohotkey?

21 Jul 2021, 20:25

Code: Select all

JS_Products_Oject = 
(join`r`n
{
"grogu":{"price":50,"Iva":10},

"strela":{"price":100,"Iva":15},

"zzz":"End"}
)

js_code = 
(join`r`n

Products = %JS_Products_Oject%;

Products_Keys = Object.keys(Products).join("|");

)

	;ComObjError(false)		;turn error message box off

SC := ComObjCreate("ScriptControl")
SC.Language := "JScript"

	;SC.alert("hh")		;"alert()" does not work

SC.Eval(js_code)

MsgBox, % ""
. SC.Eval(Products_Keys)               "`r`n`r`n"
Last edited by gregster on 21 Jul 2021, 23:58, edited 1 time in total.
Reason: Topic moved from 'Forum Issues'.
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Why does javascript "Object.keys()' not work with Autohotkey?

23 Jul 2021, 03:58

If you load a JavaScript engine of the appropriate version, it will work.

ScriptControl is very old.

There are at least a few ways to load a newer engine:
  • Create an InternetExplorer.Application with ComObjCreate and write a document/script into it.
  • Create a htmlfile with ComObjCreate and write a document/script into it.
  • Create a GUI with an ActiveX WebBrowser control... and write a document/script into it.
  • Use JsRT.ahk and ActiveScript.ahk. Usage is shown on GitHub (use either JsRT.IE11 or JsRT.Edge).
  • Use JsRT or ChakraCore via DllCall (ChakraCore is open source and probably more up to date).
User avatar
Chunjee
Posts: 1422
Joined: 18 Apr 2014, 19:05
Contact:

Re: Why does javascript "Object.keys()' not work with Autohotkey?  Topic is solved

23 Jul 2021, 20:23

I tried JsRT.ahk/ActiveScript.ahk on my win10 system and still got no dice:

Code: Select all

js := new JsRT.Edge()

code =
(
var Products = {"grogu":{"price":50,"Iva":10},
"strela":{"price":100,"Iva":15},
"zzz":"End"}

var Product_keys = Object.keys(Products)
var stringVals = [1,2,3].join("|");
)
js.Exec(code)
js.Product_keys
; => {}
js.stringVals
; => "1|2|3"
Its like Object doesn't exist.

However if javascript is not crucial to this flow; https://biga-ahk.github.io/biga.ahk/#/?id=keys may be able to produce the same data:

Code: Select all

A := new biga() ; requires https://www.npmjs.com/package/biga.ahk

products := {"grogu": {"price": 50, "Iva": 10}
, "strela": {"price":100, "Iva":15}
, "zzz": "End"}

A.keys(products)
; => ["grogu", "strela", "zzz"]
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Why does javascript "Object.keys()' not work with Autohotkey?

23 Jul 2021, 20:58

I only went as far as

Code: Select all

#Include <ActiveScript>
#Include <JsRT>

script := new JsRT.IE  ; IE11 feature set.
; script := new JsRT.Edge  ; Edge feature set.

MsgBox % script.eval("typeof Object.keys")
Which shows "function" in either case.

Code: Select all

#Include <ActiveScript>
#Include <JsRT>

script := new JsRT.IE  ; IE11 feature set.
; script := new JsRT.Edge  ; Edge feature set.

MsgBox % script.eval("JSON.stringify(Object.keys({a:1,b:2}))")
Shows ["a","b"].

Object is defined even with the version of JScript used by ScriptControl.
Chunjee wrote:js.Product_keys
; => {}
How did you determine that? Product_keys contains a JScript Array of three items, not a string. Why are you using join() in one case and not the other?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb, mikeyww and 325 guests