WinActivate - sometimes works! Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

WinActivate - sometimes works!

Post by Albireo » 08 Dec 2022, 18:42

Hi!
I have problems activating a window.
Sometimes it works and sometimes not.
By activate I mean that the window opens and appears at the front of the screen.
An excerpt from the program .:
examples of values .:
  • aWin.artID = Header ahk_exe Pvxwin32.exe ahk_class #109
  • aWin.ID = ahk_exe Pvxwin32.exe ahk_class #109
  • aWin.hwnd = 0xe1522
  • runTimeLeft = 0 to 3

Code: Select all

...
	aWin.hwnd := WinExist(aWin.artID)
	if aWin.hwnd
	{	WinActivate % aWin.ID
		WinWaitActive % aWin.ID,, % runTimeLeft
		if ErrorLevel
...
(Maybe WinWaitActive is not needed - but could it create a problem?)
Don't think it's a problem to identify the program with aWin.hwnd

My wish is to first check if the exe program is started (or not) - It seems to work fine
If the EXE program is run, it must be opened in a predetermined size and placed in the desired location.
The problem is that the EXE program is not always placed at the front of the screen.
Experience that it works worse if the exe program is minimized before running this script.

Where am I doing wrong?

User avatar
mikeyww
Posts: 26604
Joined: 09 Sep 2014, 18:38

Re: WinActivate - sometimes works!

Post by mikeyww » 08 Dec 2022, 18:51

Instead of checking for one window and then potentially activating a different window, activate the same window that you already identified. This is simple because if you eliminate the WinActivate parameter, the last found window will be used.

When you activate a window, you do not typically need a WinWait.

Code: Select all

If WinExist(aWin.artID) {
 WinActivate
 MsgBox, 64, Done, Done!
}

Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: WinActivate - sometimes works!

Post by Albireo » 13 Dec 2022, 09:51

mikeyww wrote:
08 Dec 2022, 18:51
...Instead of checking for one window and then potentially activating a different window, activate the same window that you already identified. This is simple because if you eliminate the WinActivate parameter, the last found window will be used...
Excuse me (but I don't see the difference)
But back to the problem.

When testing this program .:

Code: Select all

aWin := {}
aWin.head := "Produkter"
aWin.ID := "ahk_exe Pvxwin32.exe ahk_class #109"
aWin.artID := aWin.head " " aWin.ID
aWin.hwnd := WinExist(aWin.artID)

If WinExist(aWin.artID)
{    WinActivate
    MsgBox, 64, Done, Done!
}
I have the same problem - the desired window does not activate - always.
If the window is minimized, it works less often or not at all.

User avatar
mikeyww
Posts: 26604
Joined: 09 Sep 2014, 18:38

Re: WinActivate - sometimes works!

Post by mikeyww » 13 Dec 2022, 10:08

You can try this script, see if it works, and then change the values. After you change it, see what hWnd is being reported. If it is non-null, see if the window activates. If it does not, you can try WinActivateForce or UI Access. If the reported hWnd is null, then you have the wrong WinTitle.

Code: Select all

aWin := {}
aWin.head  := "Untitled"
aWin.ID    := "ahk_exe notepad.exe"
aWin.artID := aWin.head " " aWin.ID
If hWnd := WinExist(aWin.artID) {
 MsgBox, 64, Success, Window was found!`n`n%hWnd%
 WinActivate
} Else MsgBox, 48, Failure, Window was not found!
If you have more than one matching window, you might get unexpected results.

Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: WinActivate - sometimes works!

Post by Albireo » 13 Dec 2022, 14:04

Thank you!
With Notepad it always works - no problem!

Maybe it was an UI Access - problem?
The AHK-program must always be run as an Administrator

Code: Select all

SetBatchLines -1
#SingleInstance Force
#NoEnv

ChkAdmin()

; #WinActivateForce
aWin := {}
; aWin.head  := "Namnlös - Anteckningar"
; aWin.ID    := "ahk_exe notepad.exe"
aWin.head  := "Fackhandelsvaror"
aWin.ID    := "ahk_exe Pvxwin32.exe ahk_Class #109"

aWin.artID := aWin.head " " aWin.ID
If hWnd := WinExist(aWin.artID) {
 MsgBox, 64, Success, Window was found!`n`n%hWnd%
 WinActivate % aWin.artID
 If ErrorLevel
	MsgBox, 64, % ErrorLevel
} Else MsgBox, 48, Failure, Window was not found!


ChkAdmin()
{	; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=40970&p=186920&hilit=ChkAdmin#p186920
	full_command_line := DllCall("GetCommandLine", "str")
	if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)"))
	{	try
		{	if A_IsCompiled
				Run *RunAs "%A_ScriptFullPath%" /restart
			else
				Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
		}
		ExitApp
	}
}

User avatar
mikeyww
Posts: 26604
Joined: 09 Sep 2014, 18:38

Re: WinActivate - sometimes works!  Topic is solved

Post by mikeyww » 13 Dec 2022, 14:08

If the window is elevated, then it is possible.

Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: WinActivate - sometimes works!

Post by Albireo » 13 Dec 2022, 14:10

mikeyww wrote:
13 Dec 2022, 14:08
If the window is elevated, then it is possible.
Explain "is elevated"

User avatar
mikeyww
Posts: 26604
Joined: 09 Sep 2014, 18:38

Re: WinActivate - sometimes works!

Post by mikeyww » 13 Dec 2022, 14:20

I'm not an expert in it, but executing some Windows commands and manipulating certain windows may require access rights that differ from the user's current ones, needing UI Access or admin rights. I believe that this is true, for example, for Windows windows such as Task Scheduler and Task Manager, and to manage Windows services through the command line. It could be true for any number of specific windows. Furthermore, when a process with admin rights runs another process, that spawned process runs with admin rights by default. This is part of why running your own script with admin rights is often not a good idea unless there is a specific reason to do so. In addition, I believe that the visibility of network drives is specific to the level of rights.

Post Reply

Return to “Ask for Help (v1)”