Best practice ? Read variable from other scripts

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
SL5
Posts: 879
Joined: 12 May 2015, 02:10
Contact:

Re: y.callFunction(

28 Feb 2018, 14:35

FanaticGuru wrote:
SL5 wrote:script hangs at y.callFunction .
I seems its waiting for return value. surprized
Without seeing the callFunction it is hard to say but I imagine that is exactly what is happening. Whether it is a function in the script or a function in a COM object, the thread of execution is not going to proceed beyond the function call until the thread returns from the function.
FG
u probably right. like always ;) the complete stuff is here: https://github.com/sl5net/global-Intell ... htly-Build ( i will update it now)

Code: Select all

class Stuff{
    static abc := 1
    callFunction( name, p* ) { ;allows you to call any function in this script
        abc := func( name )
       %abc%( p* )
        return true
    }
}

;<<<<<<<< Receive_wordlistAddress <<<< 180226200244 <<<<
Receive_wordlistAddress(CopyOfData){ ; keywords to find: y.start y.run  y.func y.callFunction ReceiveWordlistAddress
   lll(A_LineNumber, A_LineFile, "lin1 at Receive_wordlistAddress(CopyOfData= '" . CopyOfData . "' )")
   ; feedbackMsgBox("Receive_wordlistAddress(CopyOfData)",CopyOfData . "`n" . A_ScriptName . "(inc)~" . A_LineNumber)

   if(InStr(CopyOfData,"\_globalWordLists\_global.txt") || InStr(CopyOfData,"\_globalWordListsGenerated\_global.txt")){ ; thats to simple. not load this wordlist 17.02.2018 23:45 18-02-17_23-45
      ;tooltip,thats to simple. not load this wordlist 17.02.2018 23:45 18-02-17_23-45 `n '%wordlist%' = wordlist `n wordlistOLD='%wordlistOLD%' `n ( %A_ScriptName%(inc)~%A_LineNumber% ) `n
       lll(A_LineNumber, A_LineFile, "Receive_wordlistAddress(CopyOfData= '" . CopyOfData . "' ) ==> thats to simple ==> return")
      MainLoop()
      return true
   }

   msg:="wordlistOLD <??> CopyOfData ## " . wordlistOLD . " <??> " . CopyOfData . "`n" . A_ScriptName . "(inc)~" . A_LineNumber
   ;feedbackMsgBox(msg)
   lll(A_LineNumber, A_LineFile, msg)

    if( 1 && CopyOfData && wordlistOLD <> CopyOfData){
      lll(A_LineNumber, A_LineFile, "in Receive_wordlistAddress at wordlistOLD <> CopyOfData")
       msg:="wordlistOLD <> CopyOfData ## " . wordlistOLD . " <??> " . CopyOfData . "`n" . A_ScriptName . "(inc)~" . A_LineNumber
      ; feedbackMsgBox(msg)
        ; lll(A_LineNumber, A_LineFile, msg)
       CloseListBox()
       SuspendOn()

;
    ;feedbackMsgBox("wordlistOLD <> wordlist",wordlistOLD . " <> " . wordlist . "`n" . A_ScriptName . "(inc)~" . A_LineNumber)
         tooltip,LOAD NEW '%CopyOfData%' = wordlist `n wordlistOLD='%wordlistOLD%' `n ( %A_ScriptName%(inc)~%A_LineNumber% ) `n
        ;setGlobalWordlist(wordlistActive)
        ;InitializeListBox()
        ;BlockInput, Send ; Send:  The user's keyboard and mouse input is ignored while a Send or SendRaw is in progress
        InitializeHotKeys()
        DisableKeyboardHotKeys()
        SetBatchLines, -1 ;Change the Running performance speed (Priority changed to High in GetIncludedActiveWindow)
        ;feedbackMsgBox("ReadWordList_AND_setLength()",wordlist . "`n" . activeTitle . " = activeTitle  `n " .  A_ScriptName . "(inc)~" . A_LineNumber)
      Gosub, setWordlistFileUpdatedTime ; 29.04.2017 14:03
        ;prefs_Length := setLength(ParseWordsCount, maxLinesOfCode4length1)
         wordlist := CopyOfData
        wordlistOLD:=wordlist
         RegWrite, REG_SZ, HKEY_CURRENT_USER, SOFTWARE\sl5net, wordlist, %wordlist%
         ;<<<<<<<< wordlistOLD <<<< 180217214329 <<<< 17.02.2018 21:43:29 <<<<
         ; reload ; quick and dirty 17.02.2018 21:30 18-02-17_21-30 <=== todo:
         ;>>>>>>>> wordlistOLD >>>> 180217214336 >>>> 17.02.2018 21:43:36 >>>>

         ; SuspendOn() ; maybe it helps. dont know 17.02.2018 18:285

        ;msg:="wordlistOLD <> CopyOfData ## " . wordlistOLD . " <??> " . CopyOfData . "`n" . A_ScriptName . "(inc)~" . A_LineNumber
        lll(A_LineNumber, A_LineFile, "next: ReadWordList_AND_setLength")
        ReadWordList_AND_setLength()
        lll(A_LineNumber, A_LineFile, "last: ReadWordList_AND_setLength")

         prefs_Length := setLength(ParseWordsCount, maxLinesOfCode4length1)
        lll(A_LineNumber, A_LineFile, "prefs_Length = " . prefs_Length)
         ;Gosub , headerPosition
         RebuildMatchList()

         SuspendOff() ; maybe it helps. dont know 17.02.2018 18:28
        ;MainLoop()
    }
    ; MainLoop()
    ; goto, MainLoopLabel
;
;
;InitializeListBox()
;BlockInput, Send ; Send: The user's keyboard and mouse input is ignored while a Send or SendRaw is in progress
;InitializeHotKeys()
;DisableKeyboardHotKeys()
;SetBatchLines, -1 ;Change the Running performance speed (Priority changed to High in GetIncludedActiveWindow)
;ReadWordList_AND_setLength()
MainLoop()
    return true  ; Returning 1 (true) is the traditional way to acknowledge this message.
}
;




;<<<<<<<< MainLoop <<<< 180208192114 <<<< 08.02.2018 19:21:14 <<<<
;<<<<<<<< MainLoop <<<< 180208192114 <<<< 08.02.2018 19:21:14 <<<<
;<<<<<<<< MainLoop <<<< 180208192114 <<<< 08.02.2018 19:21:14 <<<<
;<<<<<<<< MainLoop <<<< 180208192114 <<<< 08.02.2018 19:21:14 <<<<
;<<<<<<<< MainLoop <<<< 180208192114 <<<< 08.02.2018 19:21:14 <<<<
MainLoop(){
lll(A_LineNumber, A_LineFile, "lin1 at MainLoop")
   global g_TerminatingEndKeys
   Loop 
   { 

      ;If the active window has changed, wait for a new one
      IF !( ReturnWinActive() ) 
      {
         Critical, Off
         GetIncludedActiveWindow()
      } else {    
         Critical, Off
      }
   
      ;Get one key at a time 
      Input, InputChar, L1 V I, {BS}%g_TerminatingEndKeys%
   
      Critical
      EndKey := ErrorLevel
   
      ProcessKey(InputChar,EndKey)
   } ; EndOf: Loop
} 
;>>>>>>>> MainLoop >>>> 180228203405 >>>> 28.02.2018 20:34:05 >>>>
;>>>>>>>> MainLoop >>>> 180228203405 >>>> 28.02.2018 20:34:05 >>>>
;>>>>>>>> MainLoop >>>> 180228203405 >>>> 28.02.2018 20:34:05 >>>>
;>>>>>>>> MainLoop >>>> 180228203405 >>>> 28.02.2018 20:34:05 >>>>
;>>>>>>>> MainLoop >>>> 180228203405 >>>> 28.02.2018 20:34:05 >>>>



Attachments
setWL2ahkGlobal.ahk
(3.13 KiB) Downloaded 23 times
User avatar
FanaticGuru
Posts: 1907
Joined: 30 Sep 2013, 22:25

Re: Best practice ? Read variable from other scripts

28 Feb 2018, 15:06

If you really need multiple scripts to work together like some kind of package, you might want to look into LoadFile in the thread below:
https://autohotkey.com/boards/viewtopic.php?f=6&t=6194

I have not used it but it looks pretty nifty. It expands on ObjRegisterActive and allows one process to create children processes. Basically a parent script runs children scripts.

The parent script can then access all functions and variables of the children scripts in a very easy and straightforward way which seems like what you are trying to do.

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
SundayProgrammer
Posts: 143
Joined: 25 Dec 2020, 12:26

Re: Best practice ? Read variable from other scripts

09 Oct 2021, 00:21

nnnik wrote:
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

#Persistent
ObjRegisterActive(Stuff, "{93C04B39-0465-4460-8CA0-7BFFF481FF98}")

function( text ) {
	Msgbox % text
}
class Stuff{
	static abc := 1
	callFunction( name, p* ) { ;allows you to call any function in this script
		abc := func( name )
		%abc%( p* )
	}
}

Code: Select all

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.
evolved it a bit.

Code: Select all

#Persistent
ObjRegisterActive(agent, "{93C04B39-0465-4460-8CA0-7BFFF481FF98}")

greeting = hello

Class agent {
	Call(name, p*) { ;allows you to call any function in this script
		If InStr(name, ".")
		{	Loop, Parse, name, .
				m%A_Index% := A_LoopField, segc := A_Index
			If segc = 2
				Return %m1%[m2](p*)
			Else If segc = 3
				Return %m1%[m2][m3](p*)
			Else Return "Error: Segments More Than Allowed."
		}Return %name%(p*)
	}
}
var(name) {
	global
	local v, m1, m2, m3, segc
	If SubStr(name, 1, 4) = "Get:"
	{	name := Trim(SubStr(name, 5))
		If InStr(name, ".")
		{	Loop, Parse, name, .
				m%A_Index% := A_LoopField, segc := A_Index
			If not (StrLen(m1) and StrLen(m2))
				v = Error: Invalid Parameter "%name%"
			Else If segc = 2
				v := %m1%("Get:" m2)
			Else v = Error: Segments More Than Allowed.
		}Else v := %name%
	}Else If InStr(name, ".")
	{	Loop, Parse, name, .
			m%A_Index% := A_LoopField, segc := A_Index
		If segc = 2
			v := %m1%[m2]
		Else If segc = 3
			v := %m1%[m2][m3]
		Else v = Error: Segments More Than Allowed.
	}Else v := %name%
	Return v
}
fun(text, p1="", p2="", p3="", p4="", p5="") {
	static t := "bla"
	If SubStr(text, 1, 4) = "Get:"
	{	v := Trim(SubStr(text, 5)), v := %v%
		Return v
	}
	MsgBox, %text%`t%p1%`t%p2%`t%p3%`t%p4%`t%p5%
}
abc(p) {
	Return "[" p "]"
}
Class dummy {
	static t := "blabla"
	abc(p) {
		Return "<" p ">"
	}
	Class dummy {
		static t := "blablabla"
		abc(p) {
			Return "(" p ")"
		}
	}
}
/*
    ObjRegisterActive(Object, CLSID, Flags:=0)
    
        Registers an object as the active object for a given class ID.
        Requires AutoHotkey v1.1.17+; may crash earlier versions.
    
    Object:
            Any AutoHotkey object.
    CLSID:
            A GUID or ProgID of your own making.
            Pass an empty string to revoke (unregister) the object.
    Flags:
            One of the following values:
              0 (ACTIVEOBJECT_STRONG)
              1 (ACTIVEOBJECT_WEAK)
            Defaults to 0.
    
    Related:
        http://goo.gl/KJS4Dp - RegisterActiveObject
        http://goo.gl/no6XAS - ProgID
        http://goo.gl/obfmDc - CreateGUID()
*/
ObjRegisterActive(Object, CLSID, Flags:=0) {
    static cookieJar := {}
    if (!CLSID) {
        if (cookie := cookieJar.Remove(Object)) != ""
            DllCall("oleaut32\RevokeActiveObject", "uint", cookie, "ptr", 0)
        return
    }
    if cookieJar[Object]
        throw Exception("Object is already registered", -1)
    VarSetCapacity(_clsid, 16, 0)
    if (hr := DllCall("ole32\CLSIDFromString", "wstr", CLSID, "ptr", &_clsid)) < 0
        throw Exception("Invalid CLSID", -1, CLSID)
    hr := DllCall("oleaut32\RegisterActiveObject"
        , "ptr", &Object, "ptr", &_clsid, "uint", Flags, "uint*", cookie
        , "uint")
    if hr < 0
        throw Exception(format("Error 0x{:x}", hr), -1)
    cookieJar[Object] := cookie
}

Code: Select all

y := ComObjActive("{93C04B39-0465-4460-8CA0-7BFFF481FF98}")
y.call("fun", y.call("var", "greeting")
	, y.call("abc", "foobar")
	, y.call("dummy.abc", "world")
	, y.call("dummy.dummy.abc", y.call("var", "get:fun.t"))
	, y.call("dummy.abc", y.call("var", "dummy.t"))
	, y.call("abc", y.call("var", "dummy.dummy.t")))
and the above example will show "hello [foobar] <world> (bla) <blabla> [blablabla]"

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 178 guests