AutoHotkey choking on same-line curly brace for co

2019-07-23 21:43发布

问题:

I have a problem where AutoHotkey tells me there is a missing { in front of an 'else' where I think my Code is perfectly fine. (It worked up until I changed the window-related if's from Pidgin to qutIM)

^!p::
   IfWinExist ahk_class QWidget, ,qutIM {  ;if there is a qutIM-window other than the buddy-list...
      IfWinNotActive ahk_class QWidget, , qutIM {  ;ans it is not active...
         WinActivate
      } else {  ;the closing bracket in front of the else here puts AHK off...
         WinMinimize
      } 
   } else {  ;do some stuff with the buddy-list
      ; [...]
   } 
return

I fear I'm overlooking something stupid, but I cannot seem to get this working.

回答1:

If I am not mistaken, the One True Brace style is usable only with pure If statements, not with compounds like IfWinExist.

From the documentation for if-expressions:

The One True Brace (OTB) style may optionally be used with if-statements that are expressions (but not traditional if-statements).

Ie. you have to use WinExist() form, not IfWinExist.



回答2:

As PhiLho stated, the The One True Brace (OTB) style can't be used with compound if-statements.

While there isn't a direct function for WinNotActive(), you can use ! as a modifier for the same effect.

^!p::
   if WinExist("ahk_class QWidget", , "qutIM") {
      if !WinActive("ahk_class QWidget", , "qutIM") {
         WinActivate
      } else {
         WinMinimize
      } 
   } else {
      ; [...]
   } 
return


回答3:

Since I don't have the app you're testing it on I'm not really sure what you're trying to get it to do but this might be another way to go:

^!p::
IfWinExist, ahk_class Notepad ; if there is a qutIM-window other than the buddy-list
    {
    WinActivate
    Exists=True
    }
else ;the closing bracket in front of the else here puts AHK off...
    {
    WinMinimize
    Exists=False
    }
If Exists=True 
    MsgBox, do some stuff with the buddy-list ; dummy code
Else
    {
    Msgbox, Exiting App ; dummy code
    ExitApp
    }