Here is part of my script. It runs a program by a hotkey if it's not running or shows/hides its window in other case.
ConsolePath := "cmd.exe"
ConsoleWndClass := "ConsoleWindowClass"
CalculatorPath := "calc.exe"
CalculatorWndClass := "CalcFrame"
#s::
RunOrToggleActive(ConsolePath, ConsoleWndClass)
return
#c::
RunOrToggleActive(CalculatorPath, CalculatorWndClass)
return
RunOrToggleActive(path, wndClass) {
SplitPath, path, process
Process, Exist, %process%
If !ErrorLevel {
Run, %path%
}
else {
ToggleActive(wndClass)
}
}
ToggleActive(wndClass)
{
IfWinNotActive, % "ahk_class " wndClass
{
WinActivate, % "ahk_class " wndClass
}
else
{
WinMinimize, % "ahk_class " wndClass
}
}
Works fine, but the problem with such an approach is that adding new programs and hotkeys is very laborious. Have to add 2 variables, copy hotkey handler code, replace hotkey, replace variables. If I would like to add new hotkey function for each program (like !#s and !#c to run another instance of program even if it's running already) I will have to repeat new code again. My actual script has 7 programs and is very hard to edit.
I'd like it to work like this (half pseudocode):
appDescs := Object()
appDescs.Insert(new ProgramDesc("cmd.exe", "ConsoleWindowClass", "s"))
appDescs.Insert(new ProgramDesc("calc.exe", "CalcFrame", "c"))
#"some key"{
find key in appDescs array and RunOrToggleActive(for correspondig program)
}
!#"some key"{
find key in appDescs array and RunNewInstance(for correspondig program)
}
RunOrToggleActive(programDesc) {
...
}
RunNewInstance(programDesc) {
...
}
Class ProgramDesc {
__New(path, wndClass, key) {
this.path := path, this.wndClass := wndClass, this.key := key
}
}
I don't know how to implement #"some key"{
behavior. If someone would rewrite the code according to the pattern above (or suggest a better one) I'd be very grateful.