4

我想制作一个 AutoHotkey 脚本来更改 PuTTY SSH 客户端中的字体。(我更喜欢小字体以获得高信息密度,但是当我向同事展示某些东西时,他们需要能够清楚地看到它。)我已经做到了这一点:

#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

当从 PuTTY 终端窗口运行时,通过“发送 a”的所有内容都按预期导航 PuTTY 菜单,将我带到外观子类别。此时我想点击“更改...”按钮来设置字体。我不希望发送一堆选项卡或指定屏幕坐标来选择按钮;因为这看起来很笨拙,并且可能会随着未来的更新而中断。不过,我无法让 ControlClick 工作。经过几个小时的研究,我上面使用的这条线是我最好的猜测,我不明白为什么它什么也没做。

这是我将鼠标悬停在按钮上时的 Window Spy 输出:

>>>>>>>>>>( 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 )<<<<

谢谢你的帮助。

4

1 回答 1

3

我需要做两件事才能让它发挥作用。

首先,在第一个 ControlClick 参数中包含单词“ClassNN”是错误的,尽管我发现有几个例子使用了它。参数可以是按钮的文本 (Change...)、文本的开头 (Change) 或其 ClassNN (Button8),但不能是“ClassNN Button8”。之后的一切都是不必要的,并且使用默认值可以正常工作。我目前只是使用“ControlClick,Change...”作为整行,尽管明确指定 WinTitle 可能更明智(“PuTTY Reconfiguration”或“ahk_class PuTTYConfigBox”都有效)。

其次,正如 MCL 指出的那样,在 ControlClick 命令之前,我需要“WinWait,PuTTY Reconfiguration”。我不完全清楚为什么,但它有效。

这是我的最终工作代码,F9 切换到 ProggyCleanTT 12 点,F10 切换到 Lucida Console 20 点:

#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
}

如果您在按下热键和进一步输入之间不等待片刻,它会做一些奇怪的事情,它可能会有更强大的导航,但它确实有效。

于 2013-09-17T22:54:07.967 回答