BoundFunc Object Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

BoundFunc Object

Post by Ecimeric » 14 Jun 2021, 17:38

Code: Select all

NotepadMin := Func("Run").Bind("notepad.exe", , "Min")

Run(Target, WorkingDir, Options) {
    Run, % Target, % WorkingDir, % Options
}
Why does Notepad not open?
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: BoundFunc Object  Topic is solved

Post by HotKeyIt » 14 Jun 2021, 18:00

Because second parmeter is not optional!

Code: Select all

NotepadMin := Func("Run").Bind("notepad.exe", , "Min")
%NotepadMin%()
Run(Target, WorkingDir:="", Options:="") {
    Run, % Target, % WorkingDir, % Options
}
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: BoundFunc Object

Post by swagfag » 14 Jun 2021, 18:14

because in v1, .Bind() and calling function objects with parameters works a bit differently than how ud intuitively assume they should
bound funcs wrote:When the BoundFunc is called, it calls the function or method to which it is bound,
  1. passing any bound parameters (thats "notepad.exe", nothing and "Min" in ur script)
  2. followed by any which were passed by the caller.
    if we assume in ur code u tried to do:

    Code: Select all

    NotepadMin := Func("Run").Bind("notepad.exe", , "Min")
    %NotepadMin%("C:\some_starting_directory")
    it would be the same as trying to call the function Run like so:

    Code: Select all

    Run("notepad.exe", , "Min", "C:\some_starting_directory")
    and for that to work, ud have to have declared a function Run in ur code which allows for passing in 4 parameters, the 2nd, 3rd and 4th of which must be optional, ie:

    Code: Select all

    Run(arg1, arg2 := "", arg3 := "", arg4 := "")
    since there isnt one such function declared in ur script, the call fails silently
so if what u want to do is only have to specify the starting dir when making calls, to fix this u can either:
  1. bind only "notepad.exe"(but that then would require passing WorkingDir along with Options in each call)
  2. bind only "notepad.exe" and make Options an optional parameter defaulting to something(in which case ud be required to pass WorkingDir along with Options only in those calls where Options differs from the default)
  3. swap WorkingDir and Options around, ie

    Code: Select all

    NotepadMin := Func("Run").Bind("notepad.exe", "Min")
    Run(Target, Options, WorkingDir) { }

needless to say it works the intuitive way in v2, but im gonna say it anyway
Post Reply

Return to “Ask for Help (v1)”