Placing focus back on Cantabile with AutoIt

Trying to get some coding written by GPT - and not doing as well as hoped.
This script opens Sumatra PDF reader and will refocus on it whenever the script is called, say via a binding. Some programs refuse to refocus when their paths are substituted - Cantabile being one such program. The script will do a one time launch but will not regain focus if the script is run again. I’m mystified as to why this fails. The Cherry GX-80 also fails to refocus.
Any ideas?
Cheers!
AutoIt script:

; Check if Sumatra is running
If Not WinExists(β€œ[CLASS:SumatraMainWindow]”) Then
; Launch Sumatra if it’s not already open
Run(β€œC:\Program Files\SumatraPDF\SumatraPDF.exe”)
WinWait(β€œ[CLASS:SumatraMainWindow]”)
EndIf

A Cantabile script using additional methods to force focus as recommended by GPT - only provides a one time launch.

; Check if Cantabile is running
$hWnd = WinGetHandle(β€œ[CLASS:CantabileMainWindow]”, β€œ[EXE:Cantabile.exe]”)

If $hWnd = 0 Then
; Launch Cantabile if it’s not already open
Run(β€œC:\Program Files\Topten Software\Cantabile 4.0\Cantabile.exe”)
WinWait(β€œ[CLASS:CantabileMainWindow]”)
Else
; Activate Cantabile window if it’s already open
WinActivate($hWnd)
WinWaitActive($hWnd)
EndIf

; Set focus on Cantabile main window control
ControlFocus($hWnd, β€œβ€, β€œ[CLASS:CantabileMainWindow]”)

; Wait for the control to have focus
ControlFocus($hWnd, β€œβ€, β€œ[CLASS:CantabileMainWindow]”)

Why don’t you just use the WIndow Title? I am using Cantabile extensively with Autohotkey and that works with no glitches

3 Likes

Success!
Thank you for the tip!

1 Like