autohotkey partial window title match (without ahk class) - autohotkey

I would like to add partial window title matching to #IfWinActive.
For example to match any notepad window, regardless wether the title is "Untitled - Notepad" or "MyFile - Notepad"
This should be very simple. My attemtps:
SetTitleMatchMode, Regex
SetTitleMatchMode, Slow
#IfWinActive *.Notepad
+n::b
#IfWinActive
second attempt matchmode 2 should match anywhere
SetTitleMatchMode, 2
#IfWinActive Notepad
+n::b
#IfWinActive
A test with using the full title Untitled - Notepad confirms that the code +n::b does substute Shift-n for Shift-b
NOTE: ahk_class is not an option as the class is different on every machine for the target programme TeXnicCenter

As Robert Ilbrink said, my second example actually works, and the problem was that I did not place SetTitleMatchMode at the very top of my script.
The default example script for ^!n was in effect and interfered. Not exactly sure why it does that, but placing SetTitleMatchMode at the top solved my problem.

Related

WinActivate does not work as expected. Re-activating focus to the starting window

I am having some serious struggles fully grasping the control on activating windows and forcing their focus and foremost position.
In order to debug a larger script I made a separate script to test the use of WinActivate and again I am observing frustrating behaviour as it either all together ignores the title I have defined or is failing in some other way. In the smaller test script I am simply requesting that the window in which the hotkey was triggered be set as active after another action, specifically an input box
Below is the simple code for testing:
F10::
SetTitleMatchMode, 1
DetectHiddenWindows, Off
WinGetTitle, startTitle, A
msgbox % "Start Title = <" . startTitle . ">"
;WinActivate, startTitle
inputbox, mode, Test box, Testing,,260,160
sleep 500
WinActivate, startTitle
Return
This code does not properly activate the starting window. For example I execute the hotkey in an empty notepad window and upon submitting blank into the input box the focus becomes notepad++ on my second monitor. The second time I press the hotkey from within notepad (or another application) notepad does not lose focus. In a third execution I begin from notepad again and after the input box appears I switch the focus to another window. I again submit blank to the inputbox but that new window remains the focus and notepad is not activated or brought to the foremost position.
Can someone please explain to me what is going on with WinActivate?
I was having similar frustration with unexpected results making a windows script host file and I think I must be missing some fundamental detail in windows.
You are trying to activate a window that start with the literal text "startTitle".
You forgot(?) to either enter expression syntax with % or use the legacy way of referring to a variable %startTitle% (please don't use legacy).
Extra stuff:
You shouldn't specify SetTitleMatchMode and DetectHiddenWindows inside your hotkey statement. There is no need (unless there actually is) to set those every time you hit the hotkey. Just specify them at the top of your script once.
Both of them are useless for you though, below I'll show why. Also DetectHiddenWindows is already off by default.
WinGetTitle is not good to use for this. What you actually want to do is get the hwnd of the window you wish by using e.g. WinExist().
And then refer to the window by its hwnd. Much better than working with window titles, and impossible to match the wrong window as well. To refer to a window by its hwnd, you specify ahk_id followed by the hwnd on a WinTitle parameter.
And lastly, the concatenation operator . is redundant. Of course you may prefer to use it, but in case you didn't know, it can just be left out.
Here's your revised code:
F10::
_HWND := WinExist("A")
MsgBox, % "Start hwnd = <" _HWND ">"
InputBox, mode, Test box, Testing,,260,160
Sleep, 500
WinActivate, % "ahk_id " _HWND
Return

Ableton Issue while trying to control click

Even though I have read the docs and watched tutorials I am a complete beginner so I apologize If its a silly question. I have tried for quite a while to do this with no results.
I want to have NUMPAD0 key to press the global recording button in Ableton Live but I do not want to do it through mouse coordinates (1. Because the windows might be minimized when I need to record and 2. Because I want to implement this in the future in order to be able to press the recording button also on individual tracks as I create them, which might range from 1 to 80 new tracks for example)
The thing is that the Windows class dynamically changes and the Control ID is not shown in winspy.
This is what I have been doing:
GroupAdd, Live, ahk_class
Afx:0000000140000000:0:0000000000000000:0000000000900015:00000000007A04C3
GroupAdd, Live, ahk_class
Afx:0000000140000000:0:0000000000000000:0000000000900015:0000000000200651
GroupAdd, Live, ahk_class
Afx:0000000140000000:0:0000000000000000:0000000000900015:0000000000290651
GroupAdd, Live, ahk_class
Afx:0000000140000000:0:0000000000000000:0000000000900015:00000000001E05F1
curMode := 1
return
Numpad0::
SetControlDelay -1
controlclick, ahk_group Live0,,,,,NA
return
So, my issues are these: 1. In the GroupAdd command I have put the window classes that I have seen yet I believe there might be more classes, that appear everytime I open a new Ableton window. 2. In the Control Click command, I havent put the control ID because I couldnt find it.
Any advice on how to finish this code or change it to a different it in order to press that Record button in Ableton, without using coordinates?
EDIT:
This is the code Im trying now, following suggestions but I havent got any results yet:
SetTitleMatchMode, Regex
SetTitleMatchMode, Slow
#IfWinActive, .*als
Numpad0::F9
#IfWinActive
return
And this is the 2 version of that, which is not working either
SetTitleMatchMode, Regex
SetTitleMatchMode, Slow
#IfWinActive, Afx:0000000140000000
Numpad0::F9
#IfWinActive
return
While I've never used GroupAdd, if there's a finite number of Class permutations, it seems to me you could RegExMatch against those and then form your group inside that. The GroupAdd spec seems to make all parameters optional beyond the GroupName, so perhaps you could skip the Class declaration after confirming you've got the right window.
Example:
WinGetTitle, Title, A
WinGetClass, Class, A
if (RegExMatch(Title, "Ableton Live.*") AND (RegExMatch(Class, "(AbletonClass1|AbletonClass2|AbletonClass3)")))
{
; Do stuff
}

What is ahk_class? How can I use it for window matching?

The AutoHotkey Beginner Tutorial includes a sample script to demonstrate window specific hotkeys and hotstrings.
#IfWinActive ahk_class Notepad
::msg::You typed msg in Notepad
#IfWinActive
#IfWinActive untitled - Paint
::msg::You typed msg in MSPaint!
#IfWinActive
The second example is fairly self-explanatory: Check for a window named "untitled - Paint". It's the first example's use of ahk_class that has me confused.
I haven't been able to find any explanation of the variable in the AHK documentation. According to an AHK forum post, ahk_class is the name of a window as given by Windows Spy, the post didn't go into any more detail.
Would there be any difference between using ahk_class Notepad and Untitled - Notepad? Would the second example work if replaced with #IfWinActive ahk_class Paint?
What is ahk_class and how can I use it for window matching?
From https://autohotkey.com/docs/misc/WinTitle.htm#ahk_class
A window class is a set of attributes that the system uses as a template to create a window. In other words, the class name of the window identifies what type of window it is.
In other words you can use it to identify windows of the same type, if you open notepad the title will be Untitled - Notepad if you save it to temp.txt the title will become temp - Notepad. ahk_class on the other hand will always remain Notepad.
The second example will work if you replace it with #IfWinActive ahk_class MSPaintApp because that's the class of mspaint.
Usually you find ahk_class using Window Spy and then use it in your scripts. If you don't have Window Spy you can use the following hotkey:
#1::WinGetClass, Clipboard, A ; Will copy the ahk_class of the Active Window to clipboard
After you found it you can use it in any place you can use window title for example instead of writing WinActivate, Untitled - Notepad you can write WinActivate, ahk_class Notepad.
Check this article. Ahk_class is the class that is given to you when you use the WindowSpy tool. This tool should be in the same folder as your AutoHotkey executable.

Having two Windows excluded in a IfWinNotActive in AutoHotKey

I am mapping Alt+F4 to ESC so that I can close windows just by pressing escape. However there are two windows that I need to actually use ESC in. So, when either of these two windows are active I want to be able to press ESC without closing the window. What is the easiest way to accomplish this? I have my script working when I just am excluding one active window but I need to work when either of the two windows are active.
Here is my attempted code:
GroupAdd, ESC, Untitled - Notepad
GroupAdd, ESC,
#IfWinNotActive, ahk_group, ESC
Escape::!F4
Return
This is the code that works properly with just one window:
;#IfWinNotActive, Untitled - Notepad
;Escape::!F4
;Return
UPDATE: Should this work?
SetTitleMatchMode, 2
SetTitleMatchMode, 2
#IfWinNotActive, Untitled - Notepad
#IfWinNotActive, Document 1 - Microsoft Word
Escape::!F4
Return
You have an extra comma in your #IfWinNotActive line after ahk_group
Try the following:
GroupAdd, ESC, Untitled - Notepad
; Add more windows to the group if necessary
#IfWinNotActive, ahk_group ESC
Escape::!F4
Return
Try using SetTitleMatchMode.
http://www.autohotkey.com/docs/commands/SetTitleMatchMode.htm
2: A window's title can contain WinTitle anywhere inside it to be a
match.
Then you can do this (it is case-sensitive by default):
settitlematchmode, 2
#IfWinNotActive, Untitled -
Try this:
First, you can't remap the same key twice in the same script.
This command affects the behavior of all windowing commands, e.g.
IfWinExist and WinActivate.
Second, you stack lines like this:
#IfWinNotActive, Untitled - Notepad
#IfWinNotActive, Document 1 - Microsoft Word
you are saying, if win1 is not active, THEN if win2 is not active.
Try this, instead:
settitlematchmode, 2
app1 := winexist("other app")
app2 := winexist("Untitled - Notepad")
if(app1 || app2) ;the || means OR. you can use && for AND.
Escape::!F4 ;you can only map a particular key one time per script
or this, which is more along the lines of your approach:
settitlematchmode, 2
GroupAdd, ESC, Untitled - Notepad
GroupAdd, ESC, My other window
IfWinNotActive, ahk_group ESC
Escape::!F4
Return
This worked for me:
1: write this at the top of your script (before the first hotkey):
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetTitleMatchMode, Regex
2: Then you can use the not operator !:
if !(WinActive("ahk_class Notepad") or WinActive("ahk_class Calculator")) {
...
}
Credit

script opens document but not with hotkey

I'm creating a script to open and run with a program that has no help files but comes with a huge PDF manual. There is no way to open it from the program, so I have set up a command to open it. The command opens the PDF when run on its own, but when I assign a hotkey, it does not work. What very basic information am I missing?
thanks,
Ellen
SetTitleMatchMode, 2
runwait C:\Program Files\FontLab\TypeTool3\TTool3.exe, , max
IfWinExist TypeTool 3
Return
ExitApp
Return
#ifWinActive, TypeTool 3
$wheeldown::wheelup
$wheelup::wheeldown
F1::
Run, C:\Documents and Settings\Ellen\My Documents\TypeTool3WinMan.pdf
Return
#ifWinActive
Do you have a Return before your #IfWinActive statement? If not then during startup the script will run every line until it hits the first Return, which seems to be all the way to the end for you. So also place a return after your Run, C:..... command
And oh.. Are you sure about that weird long ahk_class? It could be correct but it looks strange and if this is just a littlebit off, your hotkey would never work, so try it first with the #IfWinActive line commented out.
SetTitleMatchMode, 2
;All the stuff you want to run at startup....
Return
#ifWinActive, (part of) the window name here e.g. Excel
$wheeldown::wheelup
$wheelup::wheeldown
F1::
Run, C:\Documents and Settings\Ellen\My Documents\TypeTool3WinMan.pdf
Return
#ifWinActive