Avatar of WeThotUWasAToad
 asked on

Writing AutoHotkey scripts for a series of IF queries


What is the general format or syntax for writing a series of IF queries in AutoHotkey?

I'm very familiar with the syntax for doing the same thing in Excel. For example:


Open in new window

In the above formula, once a particular Criteria is found to be true, the corresponding Action is executed and the formula stops. If none of the Criteria are true, the formula sends a backtick ("`").

Note: I am a quadriplegic with paralyzed hands. As a result, I have AHK scripts written for almost every conceivable action and generally have several hundred of them, each with its own hotkey, running at the same time. That creates the problem of having and remembering a large number of hotkeys.

What I would like to do is, for a given Hotkey, set up multiple Actions with the Action being executed dependent on which window is active.

So for example, let's say the Hotkey is F2, the script would look something like this (I know the following is not a real script but hopefully it conveys what I am after):

SetTitleMatchMode, 2
IfWinActive, Excel
   GoSub, ExecuteActionsA
IfWinActive, PowerPoint
   GoSub, ExecuteActionsB
IfWinActive, Chrome
   GoSub, ExecuteActionsC
IfWinActive, RingCentral
   GoSub, ExecuteActionsB

Open in new window

In this way, and assuming I can get a given Hotkey's script to stop once it finds the active window, I could have a single hotkey trigger multiple different macros depending simply on which window is currently active.

ChromeMicrosoft ExcelVisual Basic ClassicAutoHotkeyMicrosoft Office

Avatar of undefined
Last Comment
Joe Winograd

8/22/2022 - Mon
Joe Winograd

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Bill Prew

First, let me say I am not an AHK person, so can only speak a little hypothetically.  However, another conceptual approach would be to drive the decision matrix you are descripting with data, rather than a bunch of IF statements.

So, what I'm imagining is a "table" (array) of data in AHK that has three columns, KEY, WINDOW, ACTION.  Then when a key is pressed the handler for that keypress is activated in AHK and it looks up the key and current window in this table, and then invokes the specified action.  Seems like it might be easier to implement and maintain, but again, I can't propose actual AHK code and can't speak to any limitations that would make this difficult.

I'm sure Joe can add some thoughts.

Joe Winograd

Hi Bill,
That's an interesting idea and doable in AutoHotkey. That said, the so-called #IfWin directives are in the language specifically to make it easy to create context-sensitive hotkeys (hotstrings, too). I'd be inclined to go that way, but your idea certainly has merit. Regards, Joe

Thanks for the solution Joe.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Joe Winograd

You're welcome, Steve, always a pleasure to help you. Hope you're doing well and staying safe. Regards, Joe