How to run a function with a shared name

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
BNOC
Posts: 2
Joined: 21 Apr 2021, 10:51

How to run a function with a shared name

22 Apr 2021, 04:40

Hi, I'm new to ahk and have built a simple GUI with a dropdown that shows the various ahk scripts I have in a directory, along with a button to then execute that script.

Now, my issue is that I want to share a method name in each of my script files so that I don't have to check which script is running and set a particular method name for it, i.e


Code: Select all

gui.ahk

Gui, Add, Button, x y w h gRun_Button_Label, Run Script

Run_Button_Label:
    Gui, Submit, NoHide

    ; Get the script we want to run and run it
    ScriptToRun := GetScriptToRun(ScriptNameToRun)    

    ; Figure out logic to kill scripts that are running
    
    ; Shared main method name so all scripts can be ran
    Run()

    return

Code: Select all

script1.ahk

Run() {
	Send 1
}

Code: Select all

script2.ahk

Run() {
	Send 2
}
As you can see, I have a few issues:

1. I have to use #Include on the script files so using more than 2 with a Run() function will throw errors I imagine - I haven't checked but would make sense.
2. I don't know how to close the currently run script
3. I don't know how I'd go about sharing a function name so I can use it in that sense

Any help would be greatly appreciated, thanks a lot!
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: How to run a function with a shared name

22 Apr 2021, 05:42

Code: Select all

#Include E:\data\temp2\run.ahk ; Use your own path
Gui, Add, Button, Default, Run
Gui, Show
Return

ButtonRun:
Gui, Submit
Run()
ExitApp
BNOC
Posts: 2
Joined: 21 Apr 2021, 10:51

Re: How to run a function with a shared name

22 Apr 2021, 06:09

Hi, thanks for the response but not quite what I'm looking for - Hopefully I can explain it better this time around.

Consider this based on your code:

My GUI to execute the Run() method - Note I've included 2 ahk files that both contain a Run() method

Code: Select all

#SingleInstance, force
#Warn

#Include C:\Users\SeanKeenan\Desktop\AHK\test1.ahk
#Include C:\Users\SeanKeenan\Desktop\AHK\test2.ahk 
Gui, Add, Button, gRun_Button_Label, Run
Gui, Show
Return

Run_Button_Label:
Gui, Submit
Run()
ExitApp
Test1.ahk

Code: Select all

#SingleInstance, force

Run() {
    loop {
        Send 1
        Sleep, 1000
    }
}
Test2.ahk

Code: Select all

#SingleInstance, force

Run() {
    loop {
        Send 2
        Sleep, 1000
    }
}
Is it possible for me to execute Test1.ahk Run() method, then when I click the button to run Test2.ahk, Test1.ahk exits and the Run() method on Test2.ahk starts being executed?

thank you
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to run a function with a shared name

22 Apr 2021, 06:30

mikeyww wrote:
22 Apr 2021, 05:42
code
what is this meant to solve exactly?
u say u want to run auxiliary scripts and have the ability to stop them on-demand, which means they'd have to be run as separate ahk instances(otherwise u wouldnt be able to stop them whenever u wanted to, since ahk is single threaded), so u can pretty much forget about anything having to do with #Include. now, u have a couple of options:
  1. and by far the easiest one - dont use functions(or do, but then ud have to not forget to call them) in ur auxiliary scripts, instead code everything into the scripts' auto-execute sections and have ur main script simply Run them. Whatever is in the autoexec section is then automatically executed(duh). The auxiliary script can be killed with Process Close and the ProcessID ud get back from Run
  2. implement any of IPC Mechanisms(eg OnMessage() monitor rigged to call ur function + PostMessage from main) into each of ur auxiliary scripts. Run the scripts, invoke the mechanism from main whenever u need to
  3. start using ahk_h, which already has 2. implemented for u: AhkThread("path/to/script.ahk") + .AhkExec()/.AhkFunction()/.AhkPostFunction()/.AhkTerminate()
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: How to run a function with a shared name

22 Apr 2021, 07:20

@swagfag has some useful ideas here. I am just demonstrating how you could use a function in an included script.

Code: Select all

#Include E:\data\temp2\run.ahk ; Use your own path
Gui, Add, Radio, vnumber, 1
Gui, Add, Radio,        , 2
Gui, Add, Button, Default, Run
Gui, Show
Return

ButtonRun:
Gui, Submit
Run(number)
ExitApp
run.ahk:

Code: Select all

Run(number) {
 Send %number%
}
You can have as many functions as you want, but with different names. If your scripts are similar, you can use a parameter instead (example above).

An alternative to Process, Close could be to set a variable that tells the function when to Return. Example is below.

Code: Select all

Global go
Gui, Add, Radio, vnumber, 1
Gui, Add, Radio,        , 2
Gui, Add, Button, Default, Run
Gosub, F3

F3::
go := False
Gui, Show
Return

ButtonRun:
Gui, Submit
go := True
run(number)
Return

run(number) {
 Loop, 100 {
  Send %number%
  Sleep, 150
 } Until !go
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 243 guests