[V2] Overwrite ahk functions??? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

[V2] Overwrite ahk functions???

02 Dec 2023, 09:44

Is it possible to change the behaviour of native ahk functions, but still have access to the original functions?

On the forum you can find examples how to overwrite or modify existing classes like Gui.
viewtopic.php?f=83&t=86124&hilit=gui+ext+v2

It is possible to overwrite original ahk functions, but can we still use the native functions?

Example use case: Let the WinGetPos return empty variables if the window is not found.
(Similar to V1 behaviour)

This example below does not work, as the recusive limit is exeeding. :think:

Code: Select all

#Requires AutoHotkey v2.0

WinGetPos(&X, &Y,&Width, &Height,"ahk_exe notepad.exe")
MsgBox(x)

WinGetPos(&X:="", &Y:="", &Width:="", &Height:="", WinTitle:="", WinText:="", ExcludeTitle:="", ExcludeText:=""){
    try{
        WinGetPos(&X, &Y, &Width, &Height, WinTitle, WinText, ExcludeTitle, ExcludeText)
     }catch {
        X:=""
        Y:=""
        Width:=""
        Height:=""
     }
}
User avatar
mikeyww
Posts: 27010
Joined: 09 Sep 2014, 18:38

Re: [V2] Overwrite ahk functions???

02 Dec 2023, 09:49

I do not believe so, because if your script defines a function, when you call it, your script's function by that name will be called. What you describe is not necessary because you can give your own function a different name.

Code: Select all

#Requires AutoHotkey v2.0

SendEx(3)

SendEx(str) {
 Send str 45
}
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: [V2] Overwrite ahk functions???

02 Dec 2023, 09:54

Is it maybe possible to first create a clone of copy of the function and use that clone in the new function?
User avatar
boiler
Posts: 16996
Joined: 21 Dec 2014, 02:44

Re: [V2] Overwrite ahk functions???

02 Dec 2023, 10:05

Some languages feature function overloading, which allows multiple functions with the same name to be defined, distinguished by their differing parameter lists, but this has not been implemented in AHK.
User avatar
andymbody
Posts: 906
Joined: 02 Jul 2017, 23:47

Re: [V2] Overwrite ahk functions???

02 Dec 2023, 11:14

AHK_user wrote:
02 Dec 2023, 09:54
Is it maybe possible to first create a clone of copy of the function and use that clone in the new function?
I think that approach would be very costly in regards to code duplication. I think there are better ways to accomplish your goal.

One possible way to come close to simulating function-overloading would be... to simply use a "flag" parameter when calling the function, then having the function perform differently depending on the flag that is set. This would help reduce code duplication compared to the approach you mentioned or using multiple functions with slightly different names.

AHK also supports Variadic Functions which allows you to pass multiple parameters that are not "pre-declared" by the function itself. This can come in handy for simulating function-overloading as well.
ntepa
Posts: 429
Joined: 19 Oct 2022, 20:52

Re: [V2] Overwrite ahk functions???  Topic is solved

02 Dec 2023, 15:56

AHK_user wrote:
02 Dec 2023, 09:44
Is it possible to change the behaviour of native ahk functions, but still have access to the original functions?
It's possible:

Code: Select all

WinGetPos.DefineProp("Call", {Call:_WinGetPos})
_WinGetPos(this, &X?, &Y?, &W?, &H?, args*) {
    try {
        (Func.Prototype.Call)(this, &X, &Y, &W, &H, args*)
    } catch {
        X := Y := W := H := ""
    }
}

WinGetPos(&X, &Y, &Width, &Height, "ahk_exe notepad.exe")
MsgBox X
User avatar
andymbody
Posts: 906
Joined: 02 Jul 2017, 23:47

Re: [V2] Overwrite ahk functions???

02 Dec 2023, 16:38

ntepa wrote:
02 Dec 2023, 15:56
It's possible:
oh, wow... new toys... Thank you! Do these look correct? They seem to act correct...

Alter normal behavior?

Code: Select all

MsgBox.DefineProp("Call", {Call:_MsgBox})
_MsgBox(this, Text := "", Title := "", Options := "")
{
    try {
		Title := "Luke...", Text := "I am your father!"
        (Func.Prototype.Call)(this, Text, Title, Options)
    }
	catch {
		(Func.Prototype.Call)(this, "Error", "Error", "")
    }
}
MsgBox("Who are you?",, 48)	; alter normal behavior

Define additional behavior while still retaining normal behavior?

Code: Select all

MsgBox.DefineProp("Hello", {Call:_Hello})
_Hello(this)
{
    try {
		Title := Text := "Hello World!"
        (Func.Prototype.Call)(this, Text, Title, "")
    }
	catch {
		(Func.Prototype.Call)(this, "Error", "Error", "")
    }
}
MsgBox.Hello					; added behavior
MsgBox "I am a normal msgbox"	; retain normal behavior
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: [V2] Overwrite ahk functions???

02 Dec 2023, 18:20

Thanks! This was what I was looking for. Amazing what V2 can do.

Of course it is not the best practice, that would be to just name it different but so cool that this is possible.

:D :bravo:
vmech
Posts: 357
Joined: 25 Aug 2019, 13:03

Re: [V2] Overwrite ahk functions???

02 Dec 2023, 19:46

andymbody wrote:
02 Dec 2023, 16:38
oh, wow... new toys...
_MsgBox(this, params*) is enough.
Please post your script code inside [code] ... [/code] block. Thank you.
User avatar
andymbody
Posts: 906
Joined: 02 Jul 2017, 23:47

Re: [V2] Overwrite ahk functions???

02 Dec 2023, 20:58

vmech wrote:
02 Dec 2023, 19:46
_MsgBox(this, params*) is enough.
Ok... thanks!
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: [V2] Overwrite ahk functions???

03 Dec 2023, 05:00

I improved error handling so it would only react on the "Target window not found".
In other cases it would just display the error as normal, not mentioning the modified function.

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance force
; example effort to try to overwrite original ahk code

WinGetPos.DefineProp("Call", {Call:_WinGetPos})

_WinGetPos(this, &X?, &Y?, &W?, &H?, args*) {
    try {
        (Func.Prototype.Call)(this, &X, &Y, &W, &H, args*)
    } catch as Err {
      if (Err.Message = "Target window not found."){
         X := Y := W := H := ""
      } else {
         Throw ValueError(Err.Message,-1,Err.Extra)
      }
    }
}

WinGetPos(&X, &Y,&Width, &Height,"ahk_exe Notepad.exe")

MsgBox("x:" x)
lexikos
Posts: 9593
Joined: 30 Sep 2013, 04:07
Contact:

Re: [V2] Overwrite ahk functions???

09 Dec 2023, 00:13

@AHK_user It is better to catch TargetError than to inspect the Message. The documentation says "A TargetError is thrown if the window could not be found." It does not say what message the TargetError will have.
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: [V2] Overwrite ahk functions???

09 Dec 2023, 06:58

lexikos wrote:
09 Dec 2023, 00:13
@AHK_user It is better to catch TargetError than to inspect the Message. The documentation says "A TargetError is thrown if the window could not be found." It does not say what message the TargetError will have.
@lexikos: I am not that familiar with the error handling.

Could you give an example, or is the following code ok?

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance force
; example effort to try to overwrite original ahk code

WinGetPos.DefineProp("Call", {Call:_WinGetPos})

_WinGetPos(this, &X?, &Y?, &W?, &H?, args*) {
  try {
    (Func.Prototype.Call)(this, &X, &Y, &W, &H, args*)
  } catch as TargetError {
    X := Y := W := H := ""
  } catch as Err{
    Throw ValueError(Err.Message,-1,Err.Extra)
  }
}

WinGetPos(&X, &Y,&Width, &Height,"ahk_exe Notepad.exe")

MsgBox("x:" x))
lexikos
Posts: 9593
Joined: 30 Sep 2013, 04:07
Contact:

Re: [V2] Overwrite ahk functions???

15 Dec 2023, 21:25

I did not write catch as TargetError. I wrote catch TargetError. The purpose should become clear if you read the documentation for the ErrorClass parameter of Catch.
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: [V2] Overwrite ahk functions???

16 Dec 2023, 05:39

@lexikos : So like this?

Could you explain the difference?
It seems TargetError has the same properties.

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance force
; example effort to try to overwrite original ahk code
WinGetPos.DefineProp("Call", {Call:_WinGetPos})

_WinGetPos(this, &X?, &Y?, &W?, &H?, args*) {
  try {
    (Func.Prototype.Call)(this, &X, &Y, &W, &H, args*)
  } catch TargetError as Err {
    if (Err.Message = "Target window not found."){
      X := Y := W := H := ""
    } else {
        Throw ValueError(Err.Message,-1,Err.Extra)
    }
  }

}
Marium0505
Posts: 40
Joined: 11 May 2020, 20:45

Re: [V2] Overwrite ahk functions???

16 Dec 2023, 07:38

AHK_user wrote:
16 Dec 2023, 05:39
@lexikos : So like this?

Could you explain the difference?
It seems TargetError has the same properties.

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance force
; example effort to try to overwrite original ahk code
WinGetPos.DefineProp("Call", {Call:_WinGetPos})

_WinGetPos(this, &X?, &Y?, &W?, &H?, args*) {
  try {
    (Func.Prototype.Call)(this, &X, &Y, &W, &H, args*)
  } catch TargetError as Err {
    if (Err.Message = "Target window not found."){
      X := Y := W := H := ""
    } else {
        Throw ValueError(Err.Message,-1,Err.Extra)
    }
  }

}
See the ErrorClass Parameter for Catch, it means it will only catch errors of the TargetError type.
https://www.autohotkey.com/docs/v2/lib/Catch.htm#Parameters
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: [V2] Overwrite ahk functions???

16 Dec 2023, 15:27

OK, thanks for the explanation, I never done a lot of error handling.
This is the final proper code:

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance force
; example effort to try to overwrite original ahk code
WinGetPos.DefineProp("Call", {Call:_WinGetPos})

_WinGetPos(this, &X?, &Y?, &W?, &H?, args*) {
  try {
    (Func.Prototype.Call)(this, &X, &Y, &W, &H, args*)
  } catch TargetError {
      X := Y := W := H := ""
  } catch as Err {
    Throw ValueError(Err.Message,-1,Err.Extra)
  }
}

WinGetPos(&X, &Y,&Width, &Height,"ahk_exe Notepad.exe")

MsgBox("x:" x)

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: CraigM, Google [Bot], Smile_, vmech and 57 guests