Passing variables/values between scripts

Get help with using AutoHotkey and its commands and hotkeys
wKey
Posts: 3
Joined: 08 Jul 2019, 05:08

Passing variables/values between scripts

08 Jul 2019, 05:45

Hi everyone. I followed this tutorial: youtu.be/tkeQq_nfQ5k?t=853

The codes :

Code: Select all

Var1 := 1
objShell := ComObjCreate("WScript.Shell")
objEnv   := objShell.Environment("Volatile")
objEnv.Item("Var1") := Var1
My question is that Can I use this method (ComObj) to passing an array, like Var1 := [1, 2, 3] and how to retrieve these values ?
aifritz
Posts: 250
Joined: 29 Jul 2018, 11:30
Location: Germany

Re: Passing variables/values between scripts

08 Jul 2019, 14:35

I think it's not possible to store arrays directly. You can store them in single variables and push them together to an array if it's needed.

Here the way, it works. But you still need the length of the arrray:

Code: Select all

;set var
Var1 := [11, 22, 33]
objShell := ComObjCreate("WScript.Shell")
objEnv   := objShell.Environment("Volatile")
for k,v in Var1
	objEnv.Item("Var" k) := v
Var1 := objShell := objEnv := "" ;release

Code: Select all

;get var
Var1 := []
objShell := ComObjCreate("WScript.Shell")
objEnv   := objShell.Environment("Volatile")
Loop, 3
	Var1.Push(objEnv.Item("Var" A_Index))
;show array
for k,v in Var1
	msgbox % "Key: " k ", Value: " v
Var1 := objShell := objEnv := "" ;release
Another approach could be to store the length in a seperate var.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Passing variables/values between scripts

08 Jul 2019, 16:01

Not sure if this alternative is acceptable, but with ComObjCreate("HTMLfile") you get access to write and read to "JavaScript arrays".

Code: Select all

Obj := ComObjCreate("HTMLfile")
Obj.Write("<script></script>")
_JS := Obj.parentWindow

_JS.Var1 := [11, 22, 33]
Var2 := _JS.Var1

MsgBox, % Var2[1]
MsgBox, % Var2[2]
MsgBox, % Var2[3]
maybe that's helpful
wKey
Posts: 3
Joined: 08 Jul 2019, 05:08

Re: Passing variables/values between scripts

08 Jul 2019, 19:21

@aifritz Ya, I did mean to send an array directly.

Code: Select all

Loop, 3
	Var1.Push(objEnv.Item("Var" A_Index))
;show array
I see, the array's length is needed for the loop count.

@wolf_II So, ComObj is a disk-based approach, isn't it ? Performance wise, it would not be any better than storing variables in a file ?
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Passing variables/values between scripts

08 Jul 2019, 19:34

I only recently changed my approach to using JavaScript from within AHK. I currently think: No, the COMobject is without writing to file.
I did not give a fileName, it resides in Memory. I might be wrong, though.
I experimented in the past with ActiveX, to be able to look inside and debug JS.code.
Which is slower, and memory-wasting :(

I also realize you want to ultimately pass Data between Scripts, which is interesting. Have you found ("Hidden Gui that allows just that" by FanaticGuru)?
Names may be wrong here, but there is work that's already been done. I will look for a link in the forum.

Or, are you fixed on using COM object, and/or fixed on WScript.Volatile.Environment? I know nothing about that, I would take aifritz's word for granted, who said you at least need one more parameter/variable.

EDIT: GuiVar https://www.autohotkey.com/boards/viewtopic.php?p=16101#p16101
wKey
Posts: 3
Joined: 08 Jul 2019, 05:08

Re: Passing variables/values between scripts

08 Jul 2019, 23:59

I might have skimmed through FanaticGuru's topic before, my initial approach is to use tooltip so it's kinda similar.

Return to “Ask For Help”

Who is online

Users browsing this forum: austin4149, Google [Bot], mikeyww and 34 guests