Send variables to another script Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
burton2
Posts: 21
Joined: 18 Sep 2017, 10:20

Send variables to another script

16 Nov 2017, 09:39

I have searched for an answer but im coming up short.

My main script

Code: Select all

Var1 = Script1Var
Var2 = Script2Var

RunWait Script1.ahk
RunWait Script2.ahk

Msgbox %Var1% %Var2%
Script 1

Code: Select all

Gui, 1:  Add, Edit , vInput
Gui, 1: Add, Picture, gOK, path.png
Gui , 1: Show

OK:
{
Gui, , Submit, NoHide
}
Exitapp
Script 2

Code: Select all

Gui, 2:  Add, Edit , vInput2
Gui, 2: Add, Picture, gOK, path.png
Gui , 2: Show

OK:
{
Gui, , Submit, NoHide
}
Exitapp
How do I pass a variable from Script 1 into my main script and store it. Then pass a variable from Script 2 and store it in another variable?
User avatar
Gio
Posts: 1247
Joined: 30 Sep 2013, 10:54
Location: Brazil

Re: Send variables to another script  Topic is solved

16 Nov 2017, 10:27

Hello burton2.

Welcome to the AutoHotkey community forums.

This seemingly simple matter is not as straightforward to solve as it may seem at first. From the standpoint of windows, processes run independently and cannot freely mess around with the memory values in other processes (for security reasons). Even the reading of said values by other processes is forbidden at first (and that is not to say how you would know where the variable contents are located in the memory space of the script).

That being said, you do have options to tackle this issue. You can, in example, have one of the scripts write information to a file and than have the other one read it. Timing is a key factor here, but Loops, SetTimer, SendMessage, OnMessage() and other commands are options of available tools for this. Another option is to use a database, which is the standard if you want to create an application for multiple users and multiple computers.

Curiously enogth, Joe Glines is going to tackle this issue in a webinar on November 21st. You are free to join it. (check the post here: https://autohotkey.com/boards/viewtopic ... 38#p182638).

If you need further advice on how to implement a routine to share information via a file, or a starting kick on how to set up an access file database, feel free to ask.

Best wishes.
burton2
Posts: 21
Joined: 18 Sep 2017, 10:20

Re: Send variables to another script

16 Nov 2017, 11:10

Thanks @Gio

I guess that's why I couldn't find much info on it.

FileAppend and a process check loop will work fine for me.

Iv signed up for the seminar but I have a suspicion its out of my league. Im still at the stage where you take other peoples code, butcher it a bit and somehow it works.
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Send variables to another script

16 Nov 2017, 12:48

burton2 wrote:How do I pass a variable from Script 1 into my main script and store it. Then pass a variable from Script 2 and store it in another variable?
Below is a link to a library of functions called GuiVar that allows you to pass information from one script to another.
https://autohotkey.com/boards/viewtopic ... 101#p16101

It allows you to basically create a "variable" that both scripts can access with functions.

Code: Select all

;Script 1
X := "Some info"
GuiVar_Set("AnyNameYouWant", X)

;Script 2
Y := GuiVar_Get("AnyNameYouWant")
MsgBox % Y ; will display "Some info"

;Script 3
Z := GuiVar_Get("AnyNameYouWant")
MsgBox % Z ; will display "Some info"
Class versions would be like this:

Code: Select all

; Script 1
X := new GuiVar("AnyNameYouWant")
X.Value := "Some info"

; Script 2
Y := new GuiVar("AnyNameYouWant")
MsgBox % Y.Value ; Some info
Y.Value := "Other info"

; Script 3
Z := new GuiVar("AnyNameYouWant")
MsgBox % Z.Value ; Other info (assuming Script 3 runs after Script 2 changes it)
FG
Last edited by FanaticGuru on 16 Nov 2017, 20:11, edited 1 time in total.
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
teadrinker
Posts: 4325
Joined: 29 Mar 2015, 09:41
Contact:

Re: Send variables to another script

16 Nov 2017, 19:54

One more simple example:
Main.ahk

Code: Select all

OnMessage(0x4A, "WM_COPYDATA_READ")
RunWait, %A_ScriptDir%\Script1.ahk
RunWait, %A_ScriptDir%\Script2.ahk

MsgBox, % "Script1Var = " . Script1Var
MsgBox, % "Script2Var = " . Script2Var
Return

WM_COPYDATA_READ(wp, lp)  {
   global Script1Var, Script2Var
   data := StrGet(NumGet(lp + A_PtrSize*2), "UTF-16")
   RegExMatch(data, "s)(.*)\|(\d+)", match)
   Script%match2%Var := match1
}
Script1.ahk

Code: Select all

Gui, Add, Edit, vInput1
Gui, Add, Button, gOK, Send Edit-content to Main.ahk
Gui, Show
Return

OK()  {
   GuiControlGet, Input1
   Input1 .= "|1"
   VarSetCapacity(message, size := StrPut(Input1, "UTF-16")*2, 0)
   StrPut(Input1, &message, "UTF-16")
   VarSetCapacity(COPYDATASTRUCT, A_PtrSize*3)
   NumPut(size, COPYDATASTRUCT, A_PtrSize, "UInt")
   NumPut(&message, COPYDATASTRUCT, A_PtrSize*2)
   DetectHiddenWindows, On
   SetTitleMatchMode, 2
   SendMessage, WM_COPYDATA := 0x4A,, &COPYDATASTRUCT,, Main.ahk ahk_class AutoHotkey
   Exitapp
}
Script2.ahk

Code: Select all

Gui, Add, Edit, vInput2
Gui, Add, Button, gOK, Send Edit-content to Main.ahk
Gui, Show
Return

OK()  {
   GuiControlGet, Input2
   Input2 .= "|2"
   VarSetCapacity(message, size := StrPut(Input2, "UTF-16")*2, 0)
   StrPut(Input2, &message, "UTF-16")
   VarSetCapacity(COPYDATASTRUCT, A_PtrSize*3)
   NumPut(size, COPYDATASTRUCT, A_PtrSize, "UInt")
   NumPut(&message, COPYDATASTRUCT, A_PtrSize*2)
   DetectHiddenWindows, On
   SetTitleMatchMode, 2
   SendMessage, WM_COPYDATA := 0x4A,, &COPYDATASTRUCT,, Main.ahk ahk_class AutoHotkey
   Exitapp
}
teadrinker
Posts: 4325
Joined: 29 Mar 2015, 09:41
Contact:

Re: Send variables to another script

16 Nov 2017, 22:18

FanaticGuru wrote:But the method I decided to go with is to create a hidden Gui that acts like a variable.
You could use ShellBrowserWindow object instead of a hidden window. Through this object you may send even objects from one script to another:
Main.ahk

Code: Select all

myContainer := new GlobalContainer("MyStorage")
OnExit( Func("Exit").Bind(myContainer) )
myContainer.Connect( Func("GetData") )

RunWait, %A_ScriptDir%\Script1.ahk

GetData(container, property)  {
   obj := container[property]
   Loop % obj.MaxIndex()
      str .= obj[A_Index]
   MsgBox, % str
}

Exit(container)  {
   container.Quit()
}

class GlobalContainer  {
   __New(name)  {
      for wnd in ComObjCreate("Shell.Application").Windows  {
         if (wnd.GetProperty("container_name") = name)
            this._obj := wnd
      }
      if !this._obj  {
         this._obj := ComObjGet("new:{C08AFD90-F2A1-11D1-8455-00A0C91F3880}")
         this._obj.PutProperty("container_name", name)
      }
   }
   
   __Set(prop, value)  {
      if prop not in _obj,_userFunc
         Return this._obj.PutProperty(prop, value)
   }
   
   __Get(prop)  {
      if (prop != "_obj")
         Return this._obj.GetProperty(prop)
   }
   
   Connect(userFunc)  {
      this._userFunc := userFunc.Bind(this)
      ComObjConnect(this._obj, this)
   }
   
   PropertyChange(prop, obj)  {
      this._userFunc.Call(prop)
   }
   
   Quit()  {
      this._obj.Quit()
   }
}
Script1.ahk

Code: Select all

myContainer := new GlobalContainer("MyStorage")
myContainer.arr := ["Hello, ", "World!"]

class GlobalContainer  {
   __New(name)  {
      for wnd in ComObjCreate("Shell.Application").Windows  {
         if (wnd.GetProperty("container_name") = name)
            this._obj := wnd
      }
      if !this._obj  {
         this._obj := ComObjGet("new:{C08AFD90-F2A1-11D1-8455-00A0C91F3880}")
         this._obj.PutProperty("container_name", name)
      }
   }
   
   __Set(prop, value)  {
      if prop not in _obj,_userFunc
         Return this._obj.PutProperty(prop, value)
   }
   
   __Get(prop)  {
      if (prop != "_obj")
         Return this._obj.GetProperty(prop)
   }
   
   Connect(userFunc)  {
      this._userFunc := userFunc.Bind(this)
      ComObjConnect(this._obj, this)
   }
   
   PropertyChange(prop, obj)  {
      this._userFunc.Call(prop)
   }
   
   Quit()  {
      this._obj.Quit()
   }
}
:)
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Send variables to another script

18 Nov 2017, 02:38

teadrinker wrote:
FanaticGuru wrote:But the method I decided to go with is to create a hidden Gui that acts like a variable.
You could use ShellBrowserWindow object instead of a hidden window. Through this object you may send even objects from one script to another:
Interesting!

lexikos' ObjRegisterActive method is also very cool. It skips the using an intermediary COM object to pass the information through and just makes the obj into a COM object that can then be used by lots of other apps.

Too bad none of this was around when I wrote GuiVar. I thought I was very clever at the time to avoid the SendMessage / OnMessage approach. GuiVar is still nice and simple.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
tester
Posts: 84
Joined: 10 Jun 2021, 23:03

Re: Send variables to another script

16 Nov 2022, 20:54

Code: Select all

this._obj := ComObjGet("new:{C08AFD90-F2A1-11D1-8455-00A0C91F3880}")
I found this useful, thanks. It might be worth noting that set property values and callbacks will be lost after Explorer is restarted.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 238 guests