Pass an Object Between Two Scripts

Get help with using AutoHotkey and its commands and hotkeys
Posts: 193
Joined: 24 Jun 2015, 23:38

Pass an Object Between Two Scripts

07 Oct 2016, 14:59

I'm looking for a good way to transfer a block of settings between two separate scripts (script1 and script2).

This is currently a one-way transfer and could happen via commandline (when starting script2) or using a message handler (after script2 is started).

I could also possibly write an INI file from script1 and read it from script2 but that seems clunky.

Is it possible to pass an object (with all of the data populated in that object) between script1 and script2? Maybe a way to flatten an object and send it as a string using WM_COPYDATA? And then unflatten it? The object definition could be in a shared library but the scripts would be run separately. Or maybe I am thinking of a struct and that is not the right terminology. Some way to pass a block of settings in different object members though.

Now that I say that... I remember seeing something about making one script accessible to another via COM... maybe that would work also. I don't recall where that reference was and may need to dig it up. Depending on how complicated it is though it might be overkill (or not, don't really know).

Looking for ideas going in to try and keep it simple, before resorting to brute force attempts.
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Pass an Object Between Two Scripts

07 Oct 2016, 15:39

Here's one idea. It's similar to your INI option: save an object as a JSON string.

This one is easy to use IMO (but there are other more-recent implementations if you search): ... e-strings/
For other options search this forum for "JSON" or "Serialize."

The idea is: 1) use the JSON_FromObj() function to convert an object to a string. 2) Write the string to a file, or pass it via command line etc. 3) Use JSON_ToObj() to load the object.

These comments by Linear Spoon and HotKeyIt may also help: ... /?p=603556
Posts: 193
Joined: 24 Jun 2015, 23:38

Re: Pass an Object Between Two Scripts

07 Oct 2016, 16:15

These are both great suggestions with solutions I hadn't seen before... thanks.
User avatar
Posts: 1514
Joined: 20 Jul 2014, 10:16
GitHub: Masonjar13
Location: Не Россия

Re: Pass an Object Between Two Scripts

07 Oct 2016, 16:47

It should also be possible to push the reference to a new thread, if it's possible for you to multi-thread them in your situation, using ahkassign. Being under the same process, I presume it won't have any issues. However, it would also mean that both threads would use the same instance; modifying it on either script would affect both scripts. You could get around that by cloning the object, then pushing the reference to the clone.
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
Posts: 6855
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Pass an Object Between Two Scripts

07 Oct 2016, 17:46

See ObjRegisterActive.

Code: Select all

config := {foo: "bar"}
ObjRegisterActive(config, "{81FC0693-5C1D-4C2F-A7A6-DBE9EAB97E8E}")

Code: Select all

config := ComObjActive("{81FC0693-5C1D-4C2F-A7A6-DBE9EAB97E8E}")
MsgBox %
The GUID is one of your choosing. I'd suggest randomly generating one (once, when you write the script) to avoid conflicts.

The object is not copied between processes - calls are forwarded to the host process, so the host must not exit until the client is finished with the object. The same goes for any objects returned by the object.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bad husband, boiler, malcev, tee_rock and 129 guests