"WinActive" issue and "Run"

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Vas
Posts: 4
Joined: 06 May 2017, 00:57
Contact:

"WinActive" issue and "Run"

06 May 2017, 05:07

Hi, I'm quite new in autohotkey and i need an assistance with two issues.
First one is that my WinActive command seem to be not regonized. I'm using it as condition in if function. Regardles of the state of the WinActive value it awayis send me the same result.
The second issue is that on my win 10 machine the function Run cannot be used without a file path.

Here's my simple code, i reduce it to the most simpler level for easy debuging.

Code: Select all

{
IfWinExist, ahk_exe winword.exe
	{
	if WinActive "winword.exe"
		{
		WinMinimize
		}
	else
		{
		WinActivate ahk_exe winword.exe
		}
	}
else
	{
	Run, "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Word 2016.lnk"
	}
Return
}
My desire is to have a program that i can minimze if it's active, and activate if minimized. The command work one by one. "WinMinimize" and "WinActivate" are wokring perfectly. The problem seem to be in the "if WinActivate". I've made the example with winword.exe, but in general i'm using variables. The results are the same, it aways run the operator for which the window is active. Can someone point me to why could that be?

In regards to my seond issue normally i can run a program just by calling its .exe, however on my win10 machine some programs do not recognize those and require me to use file path to be runned.
Here's a list of the offenders:
Win7/Win10
W/W - explorer.exe
W/W - dopus.exe
W/W - chrome.exe
W/W - firefox.exe
W/D - winword.exe
W/D - excel.exe
W/D - spotify.exe

W - working, D - Doesn't work.

Does anyone know a reason why that could be?
User avatar
Soft
Posts: 174
Joined: 07 Jan 2015, 13:18
Location: Seoul
Contact:

Re: "WinActive" issue and "Run"

06 May 2017, 06:38

if WinActive "winword.exe"
WinAcitvate ~ is not a command nor function, should be changed
try this one

Code: Select all

If WinExist("ahk_exe winword.exe")
{
	If WinActive("ahk_exe winword.exe")
		WinMinimize
	Else
		WinActivate, ahk_exe winword.exe
}
Else
	Run, C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Word 2016.lnk
AutoHotkey & AutoHotkey_H v1.1.22.07
Vas
Posts: 4
Joined: 06 May 2017, 00:57
Contact:

Re: "WinActive" issue and "Run"

06 May 2017, 07:31

Thak you that actually helped me.
I've tried that variant but it seem i'm missing some basic education in regards to AHK.
If i keep If WinActive("ahk_exe winword.exe") it works perfectly, but if i add one "space" before the brakets, the code stop working propertly.

Thank you again for the answer adn if you point me to where can i lear the slight syntax quirks, that'll be great.

Also the problem for Run still stand.
Vas
Posts: 4
Joined: 06 May 2017, 00:57
Contact:

Re: "WinActive" issue and "Run"

06 May 2017, 07:43

To furder point my confusion, here's another code i've done:

Code: Select all

F_Switch(Target,TClass,TGroup,TPath = 0)
{
IfWinExist, ahk_exe %Target% ;Checking state of existence
	{
	GroupAdd, %TGroup%, %TClass% ;Definition of the group (grouping all instance of this class) (Not the perfect spot as make fo unnecessary reapeats of the command with every cycle, however the only easy option to keep the group up to date with the introduction of new instances)
	if WinActive ("%TClass%") ;Status check for active window if belong to the same instance of the "Target"
		{
		GroupActivate, %TGroup%, r ;If the condition is met cycle between targets belonging to the group
		}
	else
		WinActivate %TClass% ;you have to use WinActivatebottom if you didn't create a window group.
	}
else
	{
	if TPath = 0 
		Run, %Target%
	else
		Run, %TPath% ;Command for running target if conditions are satisfied
	}
Return
In this function that i've made i have the same operator if WinActive ("%TClass%") here i have a "space" and the switch works, if i remove it the code stops working. In the other snipit that you can see above seem to have the oposite effect. Haveing "space" breaks the code and removing it make the code work.
Can someone explane to me that.
User avatar
Soft
Posts: 174
Joined: 07 Jan 2015, 13:18
Location: Seoul
Contact:

Re: "WinActive" issue and "Run"

06 May 2017, 08:45

Vas wrote: ... seem i'm missing some basic education in regards to AHK.
If i keep If WinActive("ahk_exe winword.exe") it works perfectly, but if i add one "space" before the brakets, the code stop working propertly.
... adn if you point me to where can i lear the slight syntax quirks, that'll be great.
When using command(MsgBox, WinActivate ...), default type of input is mostly traditional (not expression)
but when it comes to function, type is expression
Which means...

Code: Select all

; variable name 'Variable' is storing number 100
; = variable name 'Variable' is referencing number 100
Variable := 100

; it's not 100, but string(text) "Variable"
; Because 'MsgBox' is a command, hence only takes argument as a traditinal type
Msgbox, Variable

; So, in order to pass a variable properly, we need change the current type(= traditional) to expression
; To force expression, enclose variable with percent signs, or like % variable
; then it forces AutoHotkey to interpret a reference(100) of a variable, rather than the text of it("Variable")
MsgBox, % Variable

; However, this is 100, why ?
; Because MsgBoxFunction is a function, meaning it takes arguments(= parameters) as a expression type
; so the reference of variable name 'Variable' is passed (100)
MsgBoxFunction(Variable)

; In expression type, to express a string("Variable") rather than a reference
; enclose a string with double quotes, (e.g "Variable")
MsgBoxFunction("Variable")

MsgBoxFunction(Text)
{
	MsgBox, % Text
}

Now, let's see your code again

Code: Select all

IfWinExist, ahk_exe winword.exe
It's okay, IfWinExist is a command and 'ahk_exe winword.exe' is a string text, not a reference of something

Code: Select all

if WinActive "winword.exe"
Syntax is wrong. WinActive is an native function of ahk, and it's correct syntax is WinActive("WinTitle")
so it should be WinActive("ahk_exe winword.exe")
or if you assign a variable that refers to "ahk_exe winword.exe", code should be like

Code: Select all

var := "ahk_exe winword.exe"
WinActive(var)

Code: Select all

WinActivate ahk_exe winword.exe
okay, WinActivate is a command

Code: Select all

Run, "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Word 2016.lnk"
Not okay, Run is a command, hence ...

Code: Select all

Run, C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Word 2016.lnk

or

file := "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Word 2016.lnk"
Run, % file
and finally
If i keep If WinActive("ahk_exe winword.exe") it works perfectly, but if i add one "space" before the brakets, the code stop working propertly.
Because "ahk_exe winword.exe" (without a space) and "ahk_exe winword.exe " (with a space) is a different string
WinActive("ahk_exe winword.exe") checks for Window title name ahk_exe winword.exe
while WinActive("ahk_exe winword.exe ") checks ahk_exe winword.exe (with a space after)
AutoHotkey & AutoHotkey_H v1.1.22.07
Vas
Posts: 4
Joined: 06 May 2017, 00:57
Contact:

Re: "WinActive" issue and "Run"

06 May 2017, 13:21

Thank you i understood almost everything, but the space position that i was refernig to waslike this.
WinActive("ahk_exe winword.exe")
WinActive ("ahk_exe winword.exe")

Thank you for the good explenation.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, daiweisc, GEOVAN and 191 guests