Can I Call A Label From A Different AHK Script? Topic is solved
Can I Call A Label From A Different AHK Script?
My goal is to allow ScriptA to execute a Label from ScriptB, without having to duplicate the Label code from ScriptB inside ScriptA. I'm trying to keep what I am working on as clean as possible. Just not sure if what I want here is possible. Ideally, it would work like this:
1. Start ScriptA
2. In the auto-execute section of Script A is some sort of command to auto-load ScriptB. ScriptB utilizes a hotkey, so it's loading doesn't produce any immediate effects.
3. Within ScriptA I specify "Gosub, ScriptB_label" and ScriptA is able to run said label from ScriptB. When the label is finished, the script returns to executing ScriptA code, just like you would normally expect with labels and the Gosub command.
Note: I tried utilizing #Include, but afterwards the hotkey in ScriptA no longer functions. #Include effectively breaks ScriptA. I found this information on the #Include page: "A script behaves as though the included file's contents are physically present at the exact position of the #Include directive (as though a copy-and-paste were done from the included file). Consequently, it generally cannot merge two isolated scripts together into one functioning script.".
So maybe #Include isn't the command I am looking for . Any other possibilities?
1. Start ScriptA
2. In the auto-execute section of Script A is some sort of command to auto-load ScriptB. ScriptB utilizes a hotkey, so it's loading doesn't produce any immediate effects.
3. Within ScriptA I specify "Gosub, ScriptB_label" and ScriptA is able to run said label from ScriptB. When the label is finished, the script returns to executing ScriptA code, just like you would normally expect with labels and the Gosub command.
Note: I tried utilizing #Include, but afterwards the hotkey in ScriptA no longer functions. #Include effectively breaks ScriptA. I found this information on the #Include page: "A script behaves as though the included file's contents are physically present at the exact position of the #Include directive (as though a copy-and-paste were done from the included file). Consequently, it generally cannot merge two isolated scripts together into one functioning script.".
So maybe #Include isn't the command I am looking for . Any other possibilities?
Re: Can I Call A Label From A Different AHK Script?
ScriptA:
ScriptB:
Everything works.
Code: Select all
#Include ScriptB.ahk
gosub myLabel
return
F4::MsgBox, F4
Code: Select all
myLabel:
MsgBox, aaa
return
F3::MsgBox, F3
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.
Re: Can I Call A Label From A Different AHK Script?
If you include this ScriptB at the beginning of this ScriptA, you will run into a problem. gosub myLabel will be unreachable and won't get executed. There are two returns above it - one is explicit (end of the labeled subroutine), one implicit (of the one-liner hotkey F3).amateur+ wrote: ↑20 Jan 2022, 15:09ScriptA:ScriptB:Code: Select all
#Include ScriptB.ahk gosub myLabel return F4::MsgBox, F4
Everything works.Code: Select all
myLabel: MsgBox, aaa return F3::MsgBox, F3
Of course, in this simple case, the flow of execution will run into the labeled subroutine anyway, because it is now at the top of the auto-execution section (while the gosub is still unreachable), but this example is still misleading.
Re: Can I Call A Label From A Different AHK Script? Topic is solved
@gregster, you're right, it was a mistake. What about this one?
ScriptA:
ScriptB:
ScriptA:
Code: Select all
gosub F3
return
F4::MsgBox, F4
#Include ScriptB.ahk
Code: Select all
F3::MsgBox, F3
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.
Re: Can I Call A Label From A Different AHK Script?
Yes, this will work (jumps to normal labels should also work).
It is important to remember that including is like copy&pasting the to be included code into the including script, at the exact spot where the #include directive is located.
It is important to remember that including is like copy&pasting the to be included code into the including script, at the exact spot where the #include directive is located.
Re: Can I Call A Label From A Different AHK Script?
If you just want to call something from another running script, you can use ObjRegisterActive() for one script to call a method (function in a class) from another script. For example:
Run this script:
Then run this separate script that passes a parameter to the method in the other script for it to act upon:
Run this script:
Code: Select all
#Persistent
ObjRegisterActive(MyObject, "{8B3BC8D2-01FB-4C5B-BCCC-909D81C76A3E}")
return
class MyObject {
DisplayMessage(Message) {
MsgBox, % Message
}
}
/*
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
}
Then run this separate script that passes a parameter to the method in the other script for it to act upon:
Code: Select all
#Persistent
x := ComObjActive("{8B3BC8D2-01FB-4C5B-BCCC-909D81C76A3E}")
x.DisplayMessage("Hello, world!")
Re: Can I Call A Label From A Different AHK Script?
Thank you amateur+ for this script. Thank you gregster for helping make it all it can be. My script is working as desired now. Thank you!
boiler, your script is a little harder for me to understand. I plan on playing with it later tonight and seeing if I can get it integrated with my script. Thank you very much for sharing this with me. I really appreciate the remarks inside to help me understand how to use it. This is awesome. Thank you as well!
boiler, your script is a little harder for me to understand. I plan on playing with it later tonight and seeing if I can get it integrated with my script. Thank you very much for sharing this with me. I really appreciate the remarks inside to help me understand how to use it. This is awesome. Thank you as well!