Hotkey if statement using multiple conditionals

Jeff Axelrod picture Jeff Axelrod · Mar 26, 2012 · Viewed 9.2k times · Source

The following script works to open Firefox's location/"awesome" bar from anywhere using control-l, except when using Acrobat/Adobe reader. This is because control-l in Acrobat goes full screen. It works, but it's ugly and uses nested #ifWinNotActive.

#IfWinNotActive, ahk_class MozillaWindowClass
#IfWinNotActive, ahk_class ahk_class AcrobatSDIWindow
^l::
WinActivate, ahk_class MozillaWindowClass
Send, ^l
return
#IfWinNotActive
#IfWinNotActive

The below code replacement doesn't work. Autohotkey doesn't complain with errors, but ignores the !WinActive conditionals and furthermore appears to become caught in an infinite loop. Any ideas why? (I tried the return statement both before and after the closing bracket.)

^l::
if (!WinActive(ahk_class,MozillaWindowClass)) and (!WinActive(ahk_class,AcrobatSDIWindow)) {
   WinActivate, ahk_class MozillaWindowClass
   Send, ^l
}
return

Answer

Honest Abe picture Honest Abe · Mar 26, 2012

With the WinActive function you need quotes around ahk_class MozillaWindowClass
and you don't need the comma. The infinite loop could be resolved by adding a hook $.

$^l::
if (!WinActive("ahk_class MozillaWindowClass"))
   and (!WinActive("ahk_class AcrobatSDIWindow")) 
{
   WinActivate, ahk_class MozillaWindowClass
   Send, ^l
} else
   Send, ^l
Return

However, writing it this way is only necessary if you are using AutoHotkey basic, which is out of date.
Unless you have a legitimate reason for not upgrading to AutoHotkey_L (which is unlikely)
you can accomplish what you were trying in the first example with the #If directive.

#If !WinActive("ahk_class CalcFrame") && !WinActive("ahk_class Notepad")

^l::
Run, notepad
Winwait, ahk_class Notepad
Send, test
Return

f1::traytip,, test

#If

In this example Ctrl+L and F1 will only work as coded if
calculator and/or notepad are not currently active,
otherwise they act as they normally would.

For anyone not familiar with AutoHotkey shorthand, ! means not.