Using AutoHotkey to click a button within a window
I needed to do two things to get this to work.
First, including the word "ClassNN" in the first ControlClick parameter was wrong, despite several examples I found that used that. The parameter can be the button's text (Change...), the beginning of the text (Change), or its ClassNN (Button8), but not "ClassNN Button8". Everything after that is unnecessary, and works fine with the default values. I'm currently just using "ControlClick, Change..." as the entire line, although it might be wiser to explicitly specify the WinTitle as well (either "PuTTY Reconfiguration" or "ahk_class PuTTYConfigBox" works).
Second, as MCL pointed out, I needed "WinWait, PuTTY Reconfiguration" before the ControlClick command. I'm not entirely clear why, but it works.
Here's my final, working code, with F9 switching to ProggyCleanTT 12 point, and F10 switching to Lucida Console 20 point:
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
#SingleInstance force ; Lets the RunMe plugin for Notepad++ reload the script with Shift-F5.
; This will only work for PuTTY sessions in which, under Window/Behavior, you have checked
; "System menu appears on ALT-Space". Don't forget to save the change.
#IfWinActive ahk_class PuTTY
F9::ChangePuttyFont("ProggyCleanTT", 12)
F10::ChangePuttyFont("Lucida Console", 20)
#IfWinActive
ChangePuttyFont(font, size)
{
Send !{Space} ; open the system menu
Send g ; open Change Settings
Send !g ; select the Category menu
Send Window ; select the Window category
Send {Right} ; expand the category
Send Appearance ; select the Appearance subcategory
WinWait, PuTTY Reconfiguration ; This is necessary for some reason
ControlClick, Change... ; click the "Change..." button (under Font Settings)
Send %font% ; select font
Send !s ; select size field
Send %size% ; select size
Send {Enter} ; close font window
SEND !a ; close settings window
return
}
It does odd things if you don't wait a moment between pressing the hotkey and further input, and it could probably have more robust navigation, but it works.
Robert
Updated on June 26, 2022Comments
-
Robert almost 2 years
I want to make an AutoHotkey script to change the font in the PuTTY SSH client. (I prefer a small font for high information density, but when I'm showing something to a coworker, they need to be able to see it clearly.) I've gotten this far:
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases. SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory. SendMode Input ; Recommended for new scripts due to its superior speed and reliability. #SingleInstance force ; Lets the RunMe plugin for Notepad++ reload the script with Shift-F5. #IfWinActive ahk_class PuTTY ; If PuTTY is active ^+1:: ; and Ctrl-Shift-1 is pressed { Send !{Space} ; Alt-Space to open the system menu Send g ; open Change Settings Send !g ; select the Category menu Send w ; select the Window category Send {Right} ; expand the category Send a ; select the Appearance subcategory ControlClick, ClassNN Button8, ahk_class PuTTYConfigBox, , Left, 1 } #IfWinActive
When run from a PuTTY terminal window, everything up through "Send a" navigates the PuTTY menus as expected, bringing me to the Appearance subcategory. At this point I want to click the "Change..." button to set the font. I'd prefer not to send a bunch of tabs or specify a screen coordinate to select the button; as that seems kludgey and likely to break with future updates. I can't get ControlClick to work, though. The line I used above is my best guess after a couple hours of research, and I can't see why it does nothing.
Here's the Window Spy output when I'm hovering over the button:
>>>>>>>>>>( Window Title & Class )<<<<<<<<<<< PuTTY Reconfiguration ahk_class PuTTYConfigBox >>>>>>>>>>>>( Mouse Position )<<<<<<<<<<<<< On Screen: 1051, 207 (less often used) In Active Window: 432, 202 >>>>>>>>>( Now Under Mouse Cursor )<<<<<<<< ClassNN: Button8 Text: Change... Color: 0xF0F0F0 (Blue=F0 Green=F0 Red=F0) >>>>>>>>>>( Active Window Position )<<<<<<<<<< left: 619 top: 5 width: 456 height: 438 >>>>>>>>>>>( Status Bar Text )<<<<<<<<<< >>>>>>>>>>>( Visible Window Text )<<<<<<<<<<< &Apply &Cancel Cate&gory: Cursor appearance: B&lock &Underline &Vertical line Cursor &blinks Adjust the use of the cursor Fo&nt used in the terminal window Font: Lucida Console, 24-point Change... Allow selection of variable-pitch fonts Font &quality: Antialiased Non-Antialiased ClearType Default Font settings Hide mouse &pointer when typing in window Adjust the use of the mouse pointer Gap b&etween text and window edge: &Sunken-edge border (slightly thicker) Adjust the window border >>>>>>>>>>>( Hidden Window Text )<<<<<<<<<<< >>>>( TitleMatchMode=slow Visible Text )<<<< 1 >>>>( TitleMatchMode=slow Hidden Text )<<<<
Thanks for your help.