Best practice ? Read variable from other scripts

Get help with using AutoHotkey and its commands and hotkeys
User avatar
SL5
Posts: 132
Joined: 12 May 2015, 02:10
GitHub: sl5net
Contact:

Best practice ? Read variable from other scripts

10 Feb 2018, 10:06

hi I am looking for the best way to read from another AHK script a variable.

In my particular case, a dynamic AHK (Script 2) is created (with dynamically created include addresses) and then its run (all from Script 1).

Currently Script 1 waits for a return file, which then contains the string.
Is this better about environment variables? Or registry?
Guest

Re: Best practice ? Read variable from other scripts

10 Feb 2018, 10:33

There are various methods, "built-in" method would be
OnMessage() - last example in the docs https://autohotkey.com/docs/commands/OnMessage.htm

Alternatives (there are many more, but these are easy to use):
Use a Hidden Gui https://autohotkey.com/boards/viewtopic.php?t=3275
Talk() https://autohotkey.com/board/topic/9432 ... -and-more/
User avatar
SL5
Posts: 132
Joined: 12 May 2015, 02:10
GitHub: sl5net
Contact:

Re: Best practice ? Read variable from other scripts

14 Feb 2018, 12:42

Thank you all ! :) I will remember all of it :) (y)
I have implemented one of your suggestions here (OnMessage() - last example in the docs https://autohotkey.com/docs/commands/OnMessage.htm):
https://github.com/sl5net/global-Intell ... nc.ahk#L13
User avatar
eventhorizon
Posts: 153
Joined: 27 Oct 2016, 14:22

Re: Best practice ? Read variable from other scripts

14 Feb 2018, 14:30

if you know which variables you will be looking for then one of the simplest ways is to have the script with the variables write them off to an ini or text file and have your search script read them from there. more complex would be to scan the text of the variable source script looking for the variable name like parsing a source file for a compiler or an interpreter and recovering the variable and it's value from there. if the value can vary from run to run then i'd recommend using the ini file method. it's cleaner.
A computer lets you make more mistakes faster than any invention in human history – with the possible exceptions of handguns and tequila.
cocobanana
Posts: 17
Joined: 31 Oct 2017, 05:43

Re: Best practice ? Read variable from other scripts

14 Feb 2018, 15:52

AhkSelf() is also a good solution....

https://github.com/HotKeyIt/ahkdll-v2-release/blob/master/lib/AhkSelf.ahk

or do you want AHK_L only code?

here is an example

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

User avatar
Masonjar13
Posts: 1206
Joined: 20 Jul 2014, 10:16
GitHub: Masonjar13
Location: Не Россия

Re: Best practice ? Read variable from other scripts

14 Feb 2018, 17:53

Similar to AhkSelf(), you could directly use AHK_H.dll to multi-thread. By multi-threading, the main script is able to access the content of and execute functions, subs, and code the child thread(s). I have a class here that does that (v1.1). In terms of best practice, I would certainly recommend multi-threading, much less messy than having to separate processes (imo). In terms of simplicity, definitely an ini/txt file.
cocobanana
Posts: 17
Joined: 31 Oct 2017, 05:43

Re: Best practice ? Read variable from other scripts

14 Feb 2018, 18:59

Masonjar13 wrote:Similar to AhkSelf(), you could directly use AHK_H.dll to multi-thread. By multi-threading, the main script is able to access the content of and execute functions, subs, and code the child thread(s). I have a class here that does that (v1.1). In terms of best practice, I would certainly recommend multi-threading, much less messy than having to separate processes (imo). In terms of simplicity, definitely an ini/txt file.


thumps up for your threadMan.ahk :thumbup:
User avatar
SL5
Posts: 132
Joined: 12 May 2015, 02:10
GitHub: sl5net
Contact:

Re: Best practice ? Read variable from other scripts

14 Feb 2018, 20:40

cocobanana wrote:
Masonjar13 wrote:Similar to AhkSelf(), you could directly use AHK_H.dll to multi-thread. By multi-threading, the main script is able to access the content of and execute functions, subs, and code the child thread(s). I have a class here that does that (v1.1). In terms of best practice, I would certainly recommend multi-threading, much less messy than having to separate processes (imo). In terms of simplicity, definitely an ini/txt file.


thumps up for your threadMan.ahk :thumbup:


interesting.
i have problems with Receive_WM_COPYDATA .
seems like the script often could not find hidden scipts. sometimes it find it sometimes not. i dont understand. it drives me crazy
from https://github.com/sl5net/global-Intell ... id.ahk#L25

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



and sending from here: https://github.com/sl5net/global-Intell ... e.ahk#L345

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



it seems for me that the script sometimes cant find each other.

i hope its safer, more reliable with AHK_H.dll
cocobanana
Posts: 17
Joined: 31 Oct 2017, 05:43

Re: Best practice ? Read variable from other scripts

14 Feb 2018, 20:55

it seems for me that the script sometimes cant find each other.

i hope its safer, more reliable with AHK_H.dll

In my opinion multi-threading works more accurately and it is much simpler to use.
FanaticGuru
Posts: 900
Joined: 30 Sep 2013, 22:25

Re: Best practice ? Read variable from other scripts

15 Feb 2018, 00:37

After I discovered Lexikos' ObjRegisterActive, everything else including my own hidden GUI solution seems like a hack.

COM was specifically designed for applications to communicate with each other and ObjRegisterActive provides a way to tap into that.

Example:

Code: [Select all] [Download] (ObjReg1.ahk)GeSHi © Codebox Plus

;ObjReg1
#Persistent

; Create an Object
x := {}, x.Name := "Bob"

; Register Object
ObjRegisterActive(x, "{93C04B39-0465-4460-8CA0-7BFFF481FF98}")

Code: [Select all] [Download] (ObjReg2.ahk)GeSHi © Codebox Plus

; ObjReg2

; Connect to Registered Object
y := ComObjActive("{93C04B39-0465-4460-8CA0-7BFFF481FF98}")

MsgBox % y.Name
Run the first script then run the second script and it will display "Bob".

This is a simple object, The object can be very complex with tons of data in it.

And it is not just accessible from one script to another, it is accessible to pretty much every application running that can access COM objects. Excel for example could access my COM Object created by AHK. Every program can read the object and every program can change the object.

You have to have ObjRegisterActive but it is a relatively small function:

Code: [Select all] [Expand] [Download] (Untitled.ahk)GeSHi © Codebox Plus



FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

Google Search, Dictionary, Thesaurus - Quickly Get Information from Specific Web Resources

[Function] Timer - Create and Manage Timers
User avatar
SL5
Posts: 132
Joined: 12 May 2015, 02:10
GitHub: sl5net
Contact:

Re: Best practice ? Read variable from other scripts

15 Feb 2018, 03:24

FanaticGuru wrote:After ... ObjRegisterActive, everything else .... seems like a hack.
COM was specifically designed for applications to communicate with each other and ObjRegisterActive provides a way to tap into that.

Example: ...

What's really handy for this script is calling a function from the other script. So I want to try it for a while, with this technique or something similar:
https://github.com/sl5net/global-Intell ... e.ahk#L345

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


Sometimes it works. And then I find it really, very fast and very practical. If it was somehow just a mistake of mine (that would be nice). Really handy calling a function in a foreign script.
User avatar
nnnik
Posts: 2492
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Best practice ? Read variable from other scripts

15 Feb 2018, 03:37

Yeah OnMessage is nothing more but a bad hack compared to ObjRegisterActive. You can share an object between scripts and an Object is Data + Functions.
And you could introduce a method to the object that lets you call another scripts function.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Code: [Select all] [Download] GeSHi © Codebox Plus

y := ComObjActive("{93C04B39-0465-4460-8CA0-7BFFF481FF98}")
y.callFunction( "function", "Hello World!" ) ;will call the function Hello World of the other script

Compared to COM the SendMessage method is nothing but a bad hack. There are no advantages to it.
Recommends AHK Studio
User avatar
SL5
Posts: 132
Joined: 12 May 2015, 02:10
GitHub: sl5net
Contact:

Re: Best practice ? Read variable from other scripts

15 Feb 2018, 04:40

nnnik wrote:Yeah OnMessage is nothing more but a bad hack compared to ObjRegisterActive. You can share an object between scripts and an Object is Data + Functions.
...
y := ComObjActive("{93C04B39-0465-4460-8CA0-7BFFF481FF98}")
y.callFunction( "function", "Hello World!" ) ;will call the function Hello World of the other script ...


:bravo: :dance: yes it works. And pretty fast. :D
( https://github.com/sl5net/global-Intell ... nc.ahk#L12 )
Will it be even faster, if not access to all functions is possible, only to my string-transfer function?
User avatar
nnnik
Posts: 2492
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Best practice ? Read variable from other scripts

15 Feb 2018, 08:00

Yes indeed you can do that. In fact I created a method named callFunction in the object Stuff.
Any method you create in this object is available to the other script like the method callFunction.
You could remove the method callfunction and add another method that only calls a specific function.
That way you already limit which functions can be called.

Of course most of the time everything is possible - you just need to find out how to make it possible.
Recommends AHK Studio
User avatar
SL5
Posts: 132
Joined: 12 May 2015, 02:10
GitHub: sl5net
Contact:

Re: Best practice ? Read variable from other scripts

15 Feb 2018, 08:47

nnnik wrote:...
You could remove the method callfunction and add another method that only calls a specific function.
That way you already limit which functions can be called...

great thanks, and here it is:

Code: [Select all] [Download] GeSHi © Codebox Plus

fnReceive_wordlistAddress := Func("Receive_wordlistAddress").Bind(1)
ObjRegisterActive(fnReceive_wordlistAddress, "{93C04B39-0465-4460-8CA0-7BFFF481FF98}") ; Receive_wordlistAddress(CopyOfData){

this seems working. if its faster? i dont know.
function need to be a object. therfore i created fnReceive_wordlistAddress

By the way. this long number.
After some research, I suspect it is CLSID. unique to an application, not to your computer.
But i didnt find this number by web-searchengine. hmmm
User avatar
nnnik
Posts: 2492
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Best practice ? Read variable from other scripts

15 Feb 2018, 13:12

You can set it to any other CLSID.
Actually I think it is better to share 1 object and put all the functions inside there as methods.
II don't know if it is faster.
Recommends AHK Studio
FanaticGuru
Posts: 900
Joined: 30 Sep 2013, 22:25

Re: Best practice ? Read variable from other scripts

15 Feb 2018, 13:36

SL5 wrote:
nnnik wrote:...
You could remove the method callfunction and add another method that only calls a specific function.
That way you already limit which functions can be called...

great thanks, and here it is:

Code: [Select all] [Download] GeSHi © Codebox Plus

fnReceive_wordlistAddress := Func("Receive_wordlistAddress").Bind(1)
ObjRegisterActive(fnReceive_wordlistAddress, "{93C04B39-0465-4460-8CA0-7BFFF481FF98}") ; Receive_wordlistAddress(CopyOfData){

this seems working. if its faster? i dont know.
function need to be a object. therfore i created fnReceive_wordlistAddress

By the way. this long number.
After some research, I suspect it is CLSID. unique to an application, not to your computer.
But i didnt find this number by web-searchengine. hmmm
A Class is really the way to go. A Class is basically a structure for storing Functions. They are called methods when they are in a class but they still work like functions. This is much more "best practices".

Script to create class:

Code: [Select all] [Expand] [Download] (Untitled.ahk)GeSHi © Codebox Plus


Script to use class:

Code: [Select all] [Download] (Untitled.ahk)GeSHi © Codebox Plus

; ObjReg4

Math := ComObjActive("{93C04B39-0465-4460-8CA0-7BFFF481FF98}")

MsgBox % Math.Add5(6)
MsgBox % Math.Mult3(6)
MsgBox % Math.CircleArea(6)
By doing it this way you are also entering the world of conformity and doing things the same way as most other Windows applications.

Speed wise it should be pretty quick. There is some overhead to register the object and to connect to the object but that only needs to be done once in a script, after that actually using the object should be fast.

As for the long random looking string of characters, that is pretty much what it is: a random id string. It is in the proper form to be an GUID. Every application should have its own unique GUID so that if two developers publish applications, users will know what type application object they are connecting to by its GUID.

Every COM program has one, Excel, Outlook, AutoCAD, etc. Now those programs modify the Windows Registry to create an alias for that long random string that is more user friendly like ComObjActive("Excel.Application") but that "Excel.Application" is just an alias for a GUID string.

Here is a link to CreateGUID to make your own but really you can just use the one in any of these examples if you are not going to publish your script for wide distribution.
http://goo.gl/obfmDc

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

Google Search, Dictionary, Thesaurus - Quickly Get Information from Specific Web Resources

[Function] Timer - Create and Manage Timers

Return to “Ask For Help”

Who is online

Users browsing this forum: boiler, MaxAstro, Spikea, TAC109 and 52 guests