AutoHotkey choking on same-line curly brace for compound if statements

别来无恙 提交于 2019-12-11 02:44:42

问题


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
    }


来源:https://stackoverflow.com/questions/1828197/autohotkey-choking-on-same-line-curly-brace-for-compound-if-statements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!