SetTitleMatchMode not working Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
mikeyww
Posts: 27166
Joined: 09 Sep 2014, 18:38

Re: SetTitleMatchMode not working

10 Jan 2021, 11:31

The script I provided should work. Feel free to ask questions.
reluct
Posts: 134
Joined: 28 Nov 2018, 05:07

Re: SetTitleMatchMode not working

10 Jan 2021, 11:54

Thanks, I think now is the time to practice.
User avatar
mikeyww
Posts: 27166
Joined: 09 Sep 2014, 18:38

Re: SetTitleMatchMode not working

10 Jan 2021, 12:25

Sure, I agree!

Regarding WinTitle: although you can use the window's title text alone, I often add the "ahk_exe..." part at the end, because it helps distinguish from other windows with the same title. I sometimes have that situation, with multiple browsers, editing windows with the same name, etc. You can omit it if you don't need it.
reluct
Posts: 134
Joined: 28 Nov 2018, 05:07

Re: SetTitleMatchMode not working

10 Jan 2021, 22:22

I don't know why SetTitleMatchMode, 2 is such a non-tolerant command. Why does it stop working. How does the function at the top of the script prevent it?

Code: Select all

; ====================== Functions =========================

ControlClick2(X, Y, WinTitle="", WinText="", ExcludeTitle="", ExcludeText="") 
{ 
  hwnd:=ControlFromPoint(X, Y, WinTitle, WinText, cX, cY 
                             , ExcludeTitle, ExcludeText) 
  PostMessage, 0x201, 0, cX&0xFFFF | cY<<16,, ahk_id %hwnd% ; WM_LBUTTONDOWN 
  PostMessage, 0x202, 0, cX&0xFFFF | cY<<16,, ahk_id %hwnd% ; WM_LBUTTONUP 
  PostMessage, 0x203, 0, cX&0xFFFF | cY<<16,, ahk_id %hwnd% ; WM_LBUTTONDBLCLCK 
  PostMessage, 0x202, 0, cX&0xFFFF | cY<<16,, ahk_id %hwnd% ; WM_LBUTTONUP 
} 

; Retrieves the control at the specified point. 
; X         [in]    X-coordinate relative to the top-left of the window. 
; Y         [in]    Y-coordinate relative to the top-left of the window. 
; WinTitle  [in]    Title of the window whose controls will be searched. 
; WinText   [in] 
; cX        [out]   X-coordinate relative to the top-left of the control. 
; cY        [out]   Y-coordinate relative to the top-left of the control. 
; ExcludeTitle [in] 
; ExcludeText  [in] 
; Return Value:     The hwnd of the control if found, otherwise the hwnd of the window. 
ControlFromPoint(X, Y, WinTitle="", WinText="", ByRef cX="", ByRef cY="", ExcludeTitle="", ExcludeText="") 
{ 
    static EnumChildFindPointProc=0 
    if !EnumChildFindPointProc 
        EnumChildFindPointProc := RegisterCallback("EnumChildFindPoint","Fast") 
    
    if !(target_window := WinExist(WinTitle, WinText, ExcludeTitle, ExcludeText)) 
        return false 
    
    VarSetCapacity(rect, 16) 
    DllCall("GetWindowRect","uint",target_window,"uint",&rect) 
    VarSetCapacity(pah, 36, 0) 
    NumPut(X + NumGet(rect,0,"int"), pah,0,"int") 
    NumPut(Y + NumGet(rect,4,"int"), pah,4,"int") 
    DllCall("EnumChildWindows","uint",target_window,"uint",EnumChildFindPointProc,"uint",&pah) 
    control_window := NumGet(pah,24) ? NumGet(pah,24) : target_window 
    DllCall("ScreenToClient","uint",control_window,"uint",&pah) 
    cX:=NumGet(pah,0,"int"), cY:=NumGet(pah,4,"int") 
    return control_window 
} 

; Ported from AutoHotkey::script2.cpp::EnumChildFindPoint() 
EnumChildFindPoint(aWnd, lParam) 
{ 
    if !DllCall("IsWindowVisible","uint",aWnd) 
        return true 
    VarSetCapacity(rect, 16) 
    if !DllCall("GetWindowRect","uint",aWnd,"uint",&rect) 
        return true 
    pt_x:=NumGet(lParam+0,0,"int"), pt_y:=NumGet(lParam+0,4,"int") 
    rect_left:=NumGet(rect,0,"int"), rect_right:=NumGet(rect,8,"int") 
    rect_top:=NumGet(rect,4,"int"), rect_bottom:=NumGet(rect,12,"int") 
    if (pt_x >= rect_left && pt_x <= rect_right && pt_y >= rect_top && pt_y <= rect_bottom) 
    { 
        center_x := rect_left + (rect_right - rect_left) / 2 
        center_y := rect_top + (rect_bottom - rect_top) / 2 
        distance := Sqrt((pt_x-center_x)**2 + (pt_y-center_y)**2) 
        update_it := !NumGet(lParam+24) 
        if (!update_it) 
        { 
            rect_found_left:=NumGet(lParam+8,0,"int"), rect_found_right:=NumGet(lParam+8,8,"int") 
            rect_found_top:=NumGet(lParam+8,4,"int"), rect_found_bottom:=NumGet(lParam+8,12,"int") 
            if (rect_left >= rect_found_left && rect_right <= rect_found_right 
                && rect_top >= rect_found_top && rect_bottom <= rect_found_bottom) 
                update_it := true 
            else if (distance < NumGet(lParam+28,0,"double") 
                && (rect_found_left < rect_left || rect_found_right > rect_right 
                 || rect_found_top < rect_top || rect_found_bottom > rect_bottom)) 
                 update_it := true 
        } 
        if (update_it) 
        { 
            NumPut(aWnd, lParam+24) 
            DllCall("RtlMoveMemory","uint",lParam+8,"uint",&rect,"uint",16) 
            NumPut(distance, lParam+28, 0, "double") 
        } 
    } 
    return true 
}

; Обводка текства в Otter and Квизлет и поиск

sc52:: ; Numpad0=NumpadIns
Text:="|<>*170$71.00Dzzzz1zzzw00Dzzzy7zzzvz0Tzzzzzzzzrz0Tzzzzzzzzjz0y3y3UQ007Tz0w7w70s00Szy1sDsC1k01xzy3kTkQ3U03vzw7UzUs7zkDrzsD1z1kDzUzjzkS3y3UTy1zM7Uw7w70zs7ys61sDsC1zUTxs43kTkQ3z1zvs0DUzUs7w3zrs0T0y1kDkDzbs1y1w3UT0zz007w00D0y00C007w00S1s00Q007w01w3U00s087w07s6001o1s7y0zkA003jzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"

 if (ok:=FindText(152-150000, 148-150000, 152+150000, 148+150000, 0, 0, Text))
{
  Sleep 250
	Send {LCtrl Down}{LShift Down}{LAlt Down}{LWin Down}{scB}{LCtrl Up}{LShift Up}{LAlt Up}{LWin Up}
	sleep 1500
	Send {LCtrl Down}{LShift Down}{LAlt Down}{LWin Down}{scB}{LCtrl Up}{LShift Up}{LAlt Up}{LWin Up}
	Sleep 100
    Send {LCtrl Down}{F11}{LCtrl Up} ; ctrl+F11
    Send {LCtrl Down}{sc2F}{LCtrl Up}
    Send {Enter}
}
If A_Cursor=Unknown ; ;The hand-shaped cursors (pointing and grabbing) are classified as Unknown
{
	Click, down
	Send ^{Space} ; Play-Pause Otter
	Sleep 2000
	Click, up
	Send {LCtrl Down}{sc2E}{LCtrl Up}
    sleep 250
    Send {LCtrl Down}{F11}{LCtrl Up} ; ctrl+F11 показать окно Голдендикт
    Send {LCtrl Down}{sc2F}{LCtrl Up}
    Send {Enter}
}
Else If A_Cursor=IBeam ; курсор как при впечатать текст
{
	Send {Backspace}
}
Return

/*
Numpad4::
SetTitleMatchMode, RegEx
Text:="|<>*170$71.00Dzzzz1zzzw00Dzzzy7zzzvz0Tzzzzzzzzrz0Tzzzzzzzzjz0y3y3UQ007Tz0w7w70s00Szy1sDsC1k01xzy3kTkQ3U03vzw7UzUs7zkDrzsD1z1kDzUzjzkS3y3UTy1zM7Uw7w70zs7ys61sDsC1zUTxs43kTkQ3z1zvs0DUzUs7w3zrs0T0y1kDkDzbs1y1w3UT0zz007w00D0y00C007w00S1s00Q007w01w3U00s087w07s6001o1s7y0zkA003jzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"

 if (ok:=FindText(152-150000, 148-150000, 152+150000, 148+150000, 0, 0, Text))
{
    Sleep 250
	Send {LCtrl Down}{LShift Down}{LAlt Down}{LWin Down}{scB}{LCtrl Up}{LShift Up}{LAlt Up}{LWin Up}
	sleep 2000
	Send {LCtrl Down}{LShift Down}{LAlt Down}{LWin Down}{scB}{LCtrl Up}{LShift Up}{LAlt Up}{LWin Up}
	Sleep 100
    Send {F23}
}
else If A_Cursor=Unknown ; ;The hand-shaped cursors (pointing and grabbing) are classified as Unknown
{
	Send, {sc1D Down}
	Click
	Send, {sc1D Up}
	Sleep, 1000
	MouseGetPos, xpos, ypos
	MouseClick, left, 1691, 120, , 0 ; [WhichButton , X, Y, ClickCount, Speed, D|U, R]
	MouseMove, xpos, ypos, 0
}
Else If A_Cursor=IBeam ; курсор как при впечатать текст
{
	Send, {sc1D Down}
	Click, 2
	Send {sc1D Up}
	Sleep, 1000
	MouseGetPos, xpos, ypos
	MouseClick, left, 1691, 120, , 0 ; [WhichButton , X, Y, ClickCount, Speed, D|U, R]
	MouseMove, xpos, ypos, 0
}
Return
*/

SetTitleMatchMode, 2

; Numpad4 Скрипт GD открыть слово в новой вкладке

#IfWinActive ahk_exe GoldenDict.exe
Numpad4::  
If A_Cursor=Unknown ; ;The hand-shaped cursors (pointing and grabbing) are classified as Unknown
{
	Send, {sc1D Down}
	Click
	Send, {sc1D Up}
	Sleep, 1000
	MouseGetPos, xpos, ypos
	MouseClick, left, 1691, 120, , 0 ; [WhichButton , X, Y, ClickCount, Speed, D|U, R]
	MouseMove, xpos, ypos, 0
}
Else If A_Cursor=IBeam ; курсор как при впечатать текст
{
	Send, {sc1D Down}
	Click, 2
	Send {sc1D Up}
	Sleep, 1000
	MouseGetPos, xpos, ypos
	MouseClick, left, 1691, 120, , 0 ; [WhichButton , X, Y, ClickCount, Speed, D|U, R]
	MouseMove, xpos, ypos, 0
}
#If
Return

#IfWinActive Quizlet ahk_exe chrome.exe
Numpad4::
Sleep 250
Send {LCtrl Down}{LShift Down}{LAlt Down}{LWin Down}{scB}{LCtrl Up}{LShift Up}{LAlt Up}{LWin Up}
sleep 2000
Send {LCtrl Down}{LShift Down}{LAlt Down}{LWin Down}{scB}{LCtrl Up}{LShift Up}{LAlt Up}{LWin Up}
Sleep 100
Winhide ahk_exe chrome.exe
WinActivate, Engvidshortcut ahk_exe anki.exe
Send {Escape}
Sleep 500
Send, {scA}{sc1D Down}{sc2F}{sc1D Up}{sc1C} ;  9 - LCtrl вниз - v - LCtrl вверх - enter
#If
return
gregster
Posts: 9080
Joined: 30 Sep 2013, 06:48

Re: SetTitleMatchMode not working

10 Jan 2021, 22:26

Not the functions make it unreachable in your code above, but the hotkey subroutine for the sc52 key - and its closing return at line 115, of course.
But that has nothing to do specifically with SetTitleMatchMode - this would be true for any kind of code at that same place. It's way past the auto-execute section.
User avatar
mikeyww
Posts: 27166
Joined: 09 Sep 2014, 18:38

Re: SetTitleMatchMode not working

10 Jan 2021, 22:28

I agree with @gregster. We are going around in circles now. Review posts on page 1, which include an instruction to move the Set command inside the hotkey routine (if it's not in the auto-execute section), as well as a reference to the documentation for auto-execute.
reluct
Posts: 134
Joined: 28 Nov 2018, 05:07

Re: SetTitleMatchMode not working

10 Jan 2021, 23:39

I absolutely don't understand the problem with the hotkey subroutine for the sc52 key - and its closing return at line 115
gregster
Posts: 9080
Joined: 30 Sep 2013, 06:48

Re: SetTitleMatchMode not working

11 Jan 2021, 06:49

The auto-execcute section of a script automatically ends at the start of first hotkey subroutine (so its final return doesn't even matter in this case) - or the first hotstring, or an executed return:
https://www.autohotkey.com/docs/Scripts.htm#auto wrote:After the script has been loaded, it begins executing at the top line, continuing until a Return, Exit, hotkey/hotstring label, or the physical end of the script is encountered (whichever comes first). This top portion of the script is referred to as the auto-execute section.
(function definitions, on the other hand, don't stop code execution)


But your SetTitleMatchmode, 2 line comes after your first hotkey routine (and it's not a directive that would get executed at script start) and is effectively in a no-man's-land which can never be reached by the flow of code execution - it is neither part of the auo-execute section, nor of a function, nor of a labeled subroutine.
reluct
Posts: 134
Joined: 28 Nov 2018, 05:07

Re: SetTitleMatchMode not working

11 Jan 2021, 07:26

But I don't need

Code: Select all

SetTitleMatchmode, 2
to run the first hotkey routine (sc52 ::; Numpad0 = NumpadIns) and this block works fine. So I added it after.

This part works well too.

Code: Select all

SetTitleMatchMode, 2
#IfWinActive ahk_exe GoldenDict.exe
Numpad4::  
If A_Cursor=Unknown ; ;The hand-shaped cursors (pointing and grabbing) are classified as Unknown
{
	Send, {sc1D Down}
	Click
	Send, {sc1D Up}
	Sleep, 1000
	MouseGetPos, xpos, ypos
	MouseClick, left, 1691, 120, , 0 ; [WhichButton , X, Y, ClickCount, Speed, D|U, R]
	MouseMove, xpos, ypos, 0
}
Else If A_Cursor=IBeam ; курсор как при впечатать текст
{
	Send, {sc1D Down}
	Click, 2
	Send {sc1D Up}
	Sleep, 1000
	MouseGetPos, xpos, ypos
	MouseClick, left, 1691, 120, , 0 ; [WhichButton , X, Y, ClickCount, Speed, D|U, R]
	MouseMove, xpos, ypos, 0
}
#If
Return
The problem with the last block.

Code: Select all

#IfWinActive Quizlet ahk_exe chrome.exe
Numpad4::
Sleep 250
Send {LCtrl Down}{LShift Down}{LAlt Down}{LWin Down}{scB}{LCtrl Up}{LShift Up}{LAlt Up}{LWin Up}
sleep 2000
Send {LCtrl Down}{LShift Down}{LAlt Down}{LWin Down}{scB}{LCtrl Up}{LShift Up}{LAlt Up}{LWin Up}
Sleep 100
Winhide ahk_exe chrome.exe
WinActivate, Engvidshortcut ahk_exe anki.exe
Send {Escape}
Sleep 500
Send, {scA}{sc1D Down}{sc2F}{sc1D Up}{sc1C} ;  9 - LCtrl вниз - v - LCtrl вверх - enter
#If
return
gregster
Posts: 9080
Joined: 30 Sep 2013, 06:48

Re: SetTitleMatchMode not working

11 Jan 2021, 07:39

But you don't have anything that gets affected by it in the sc52 hotkey routine, so just put it into the auto-execute section.
As long as it doesn't get executed, it won't have any effect.
reluct
Posts: 134
Joined: 28 Nov 2018, 05:07

Re: SetTitleMatchMode not working

11 Jan 2021, 07:56

But for me the question remains how does sc52 affect #IfWinActive Quizlet ahk_exe chrome.exe
Numpad4::
if it stops executing after Return?
gregster
Posts: 9080
Joined: 30 Sep 2013, 06:48

Re: SetTitleMatchMode not working

11 Jan 2021, 08:04

In your code above, the hotkey routine only affects (prevents) the execution of the SetTitleMatchMode 2 line.

If the condition in your #If...-directive in your code above (#IfWinActive Quizlet ahk_exe chrome.exe) heavily relies on mode 2 for title matching, it wouldn't work, because the default for title matching is mode 1 - and that default still remains in effect in your code above. That's the reason.

If you need mode 2, you need to activate it. At the moment, you don't activate it.
reluct
Posts: 134
Joined: 28 Nov 2018, 05:07

Re: SetTitleMatchMode not working

11 Jan 2021, 08:19

Ok then how does sc52 prevents the work of the SetTitleMatchMode 2 if it stops working after the Return?
User avatar
mikeyww
Posts: 27166
Joined: 09 Sep 2014, 18:38

Re: SetTitleMatchMode not working

11 Jan 2021, 08:22

Isolated code (not in a labeled routine or function that is called) between hotkey routines does not run, but it will run at the top of the script before you have defined any hotkeys or hotstrings.

I suggest reading about auto-exec as noted.
Last edited by mikeyww on 11 Jan 2021, 08:23, edited 1 time in total.
gregster
Posts: 9080
Joined: 30 Sep 2013, 06:48

Re: SetTitleMatchMode not working

11 Jan 2021, 08:23

Code: Select all

msgbox This is the auto-execute section. It gets executed at script start.

; the auto-execute section ends automatically here at the first hotkey label - here exists an implicit 'return'.
sc52::
; some code 
return	; even if you press the sc52 key, code execution goes no further.

msgbox This part of the code is unreachable, it never gets executed.
SetTitleMatchMode, 2 	; this is still unreachable code - the title match mode does NOT get changed
reluct
Posts: 134
Joined: 28 Nov 2018, 05:07

Re: SetTitleMatchMode not working

11 Jan 2021, 10:02

Ok finally I got it. Thank you.
reluct
Posts: 134
Joined: 28 Nov 2018, 05:07

Re: SetTitleMatchMode not working

11 Jan 2021, 23:22

Winhide ahk_exe chrome.exe closes the window instead of being minimized.

Code: Select all

#IfWinActive Quizlet ahk_exe chrome.exe
Numpad4::
Sleep 250
Send {LCtrl Down}{LShift Down}{LAlt Down}{LWin Down}{scB}{LCtrl Up}{LShift Up}{LAlt Up}{LWin Up}
sleep 2000
Send {LCtrl Down}{LShift Down}{LAlt Down}{LWin Down}{scB}{LCtrl Up}{LShift Up}{LAlt Up}{LWin Up}
Sleep 100
Winhide ahk_exe chrome.exe
WinActivate, Engvidshortcut ahk_exe anki.exe
Send {Escape}
Sleep 500
Send, {scA}{sc1D Down}{sc2F}{sc1D Up}{sc1C} ;  9 - LCtrl вниз - v - LCtrl вверх - enter
#If
return
gregster
Posts: 9080
Joined: 30 Sep 2013, 06:48

Re: SetTitleMatchMode not working

11 Jan 2021, 23:53

WinHide does not close a window, but it also does not minimize - it hides a window (makes it kind of invisible 👻).

WinMinimize minimizes a window (if the program plays along with it - see also the remarks).
reluct
Posts: 134
Joined: 28 Nov 2018, 05:07

Re: SetTitleMatchMode not working

12 Jan 2021, 00:49

However in my case Winhide ahk_exe chrome.exe closes the window.
When i use Winhide, Mozilla Firefox it works well. Although my browser is not Mozilla Firefox.
gregster
Posts: 9080
Joined: 30 Sep 2013, 06:48

Re: SetTitleMatchMode not working

12 Jan 2021, 01:02

Are you sure, it's closed - or just hidden? Did you try WinShow to unhide it?
I guess, its process should still show up in the taskmanager, even when hidden.

What works well? You minimize Chrome by using WinHide, Mozilla Firefox ? :) Or it minimizes Firefox? This all sounds odd.
Anyway, you should use WinMimimize to minimize (or the alternative in the remarks).

Edit: Just tested. Here, everything works as expected. WinHide hides, WinShow unhides, WinMinimize minimizes - this works both for Firefox and Chrome - with their respective wintitle criteria (tested with only one matching window instance, since these commands - by default- only work on the topmost matching window).

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: CoffeeChaton, Draken, peter_ahk and 113 guests