Detecting active file with WinActive/WinExist

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Dummie1138
Posts: 17
Joined: 20 Jan 2023, 04:24

Detecting active file with WinActive/WinExist

16 Feb 2023, 04:00

Hi. I have the following code.

Code: Select all

arduinoLink := "C:\Users\User\Documents\Arduino\456_Arduino_String\456_Arduino_String.ino"
;Arduino
#HotIf WinActive("ahk_exe arduinoLink")   ;Close file if already active.
!+a:: WinClose

#HotIf WinExist("ahk_exe arduinoLink")    ;Make file active.
!+a:: WinActivate

#HotIf
!+a:: run arduinoLink     ;Activate file.
I am having trouble with

Code: Select all

#HotIf WinActive("ahk_exe arduinoLink")
and

Code: Select all

#HotIf WinExist("ahk_exe arduinoLink")
. Namely that they do not seem to detect when my file at the location arduinoLink is active. I am not sure how to proceed; the documentation has stated that prefacing the location with "ahk_exe" is needed, which I have already done so.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Detecting active file with WinActive/WinExist

16 Feb 2023, 04:42

ahk_exe expects an exe name(which usually end with ".exe", look in Task Manager > Details tab). what is the exe name of the program that Running C:\Users\User\Documents\Arduino\456_Arduino_String\456_Arduino_String.ino launches? (then use that name to query against)
CptRootBeard
Posts: 26
Joined: 16 Nov 2020, 14:47
Contact:

Re: Detecting active file with WinActive/WinExist

17 Feb 2023, 12:21

If your .ino file is source code (which I think it is, but it's been a minute since I've worked with Arduino), and you are trying to open/close it in the Arduino IDE, you will need to change your approach.
The .ino file will not own the window - as swag has said, the executable running the file will. A text file (which is what the source code is) is not 'active' in the same way as a running program.

Are you trying to make a keyboard shortcut to activate the editor tab that contains your code? If so, you might be able to just use

Code: Select all

Run(arduinoLink)
again. In many editors (including Notepad++, which is the most comparable thing I have on hand) trying to open a file that is already open will bring its editor tab to the front.

If you want to force the editor to close the file, you could use the same Run() command again to force the editor tab into focus, then use Send() to send the keyboard shortcut required to close the file.
That shortcut varies between editors, but in Notepad++ it is Ctrl+W.

Forgive me if I'm off base, but it seems this may be what you are trying to accomplish.
Dummie1138
Posts: 17
Joined: 20 Jan 2023, 04:24

Re: Detecting active file with WinActive/WinExist

20 Feb 2023, 04:28

@swagfag

The following is my new code based on your suggestions.

Code: Select all

arduinoLink := "C:\Users\User\Documents\Arduino\456_Arduino_String\456_Arduino_String.ino"
arduinoLocation := "C:\Program Files (x86)\Arduino\arduino.exe"
;Arduino
#HotIf WinActive("ahk_exe arduinoLocation arduinoLink")
!+a:: WinClose

#HotIf WinExist("ahk_exe arduinoLocation arduinoLink")
!+a:: WinActivate

#HotIf
!+a:: run arduinoLink
Sadly the issues I had persist; my window does not close when the executable at arduinoLocation running the file at arduinoLink is active.
Dummie1138
Posts: 17
Joined: 20 Jan 2023, 04:24

Re: Detecting active file with WinActive/WinExist

20 Feb 2023, 04:34

CptRootBeard wrote: The .ino file will not own the window - as swag has said, the executable running the file will. A text file (which is what the source code is) is not 'active' in the same way as a running program.
I have also tried this based on your suggestions, with the HotIf only containing the text executable location.

Code: Select all

arduinoLink := "C:\Users\User\Documents\Arduino\456_Arduino_String\456_Arduino_String.ino"
arduinoLocation := "C:\Program Files (x86)\Arduino\arduino.exe"
;Arduino
#HotIf WinActive("ahk_exe C:\Program Files (x86)\Arduino\arduino.exe")
!+a::{
MsgBox "In WA."
WinMinimize
}

#HotIf WinExist("ahk_exe arduinoLocation")
!+a::{
 MsgBox "In WE."
 WinActivate
 }
 
 #HotIf
!+a:: run arduinoLink
 
Sadly, the code never entered WinExist and WinActive even when C:\Program Files (x86)\Arduino\arduino.exe was active or existed.

What I'm trying to achieve is opening and closing the file, but also activating the file with a hotkey, but thank you very much for bringing the shortcut Ctrl+W to my attention, it's been very helpful.
hd0202
Posts: 183
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: Detecting active file with WinActive/WinExist

20 Feb 2023, 05:12

I am also new to V2 and think, that your variables 'arduinoLink' and 'arduinoLocation' must not be between the quotation marks

Hubert
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Detecting active file with WinActive/WinExist

20 Feb 2023, 05:45

i dont think i suggested doing any of that(ie using the full path as an exe name). reread what i actually wrote

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: No registered users and 19 guests