PrevActiveClass
is assigned a value, the following code won't activate PrevActiveClass
(although the syntax works for assignment):
PrevActiveClass ? WinActivate ahk_class %PrevActiveClass%
But the following will activate PrevActiveClass
:
if (PrevActiveClass) {
WinActivate ahk_class %PrevActiveClass%
}
Why is the first snippet not working, and how to implement the same logic in one line of code?
CodePudding user response:
When you're using a ternary, you're in an expression.
Legacy commands can only be used when in a legacy statement, so more clearly said, only at the start of a new line (under normal circumstances).
What you're trying to do right now, is concatenate three variables together (and discard the result) named WinActivate
, ahk_class
and a dynamic variable named PrevActiveClass
.
how to implement the same logic in one line of code?
You can't.
The best you can do is two lines:
if (PrevActiveClass)
WinActivate, % "ahk_class " PrevActiveClass
(I switched to using an expression, would've worked in the legacy syntax as well, but ew, legacy syntax)
Technically you could, of course, do this:
WinActivate, % PrevActiveClass ? "ahk_class " PrevActiveClass : "NonExistentWindow"
But that would be a really stupid design.
Also, you could define your own function for activating a window and then call that in your expression:
PrevActiveClass ? WinActivateFunc("ahk_class " PrevActiveClass)
WinActivateFunc(WinTitle := "", WinText := "", ExcludeTitle := "", ExcludeText := "")
{
WinActivate, % WinTitle, % WinText, % ExcludeTitle, % ExcludeText
}
Some people actually do this so they can use legacy commands inline.
I know there are some libraries people have made as well which offer functions like that for every(?) legacy command.
You should be able to find one from probably the AHK forums.
Also, I guess AHKv2 deserves a mention here since there simply is no legacy syntax in v2. This problem is non-existent there.