AutoHotkey

AutoHotkey

Window 平台里最经典和常用的自动化工具。使用脚本语言,具备图灵完备性,但其语法相当难用。

使用 ";" 作为行注释符号。

基本脚本语法

Keys

  • ^ : Ctrl
  • + : Shift
  • # : Win
  • ! : Alt

Variables

; 使用 %XX% 引用其它变量内容
; 默认系统所有环境变量均可用
; A_ 开头的为 AutoHotKey 提供的内置变量
dira=%A_programfiles%\IrfanView
dirb=%USERPROFILE%\data

常用自动化操作

点击 window 里控件

#WinActivateForce

!F11::  ; alt + F11
while true {
    Sleep, 1000 ; miliseconds
    WinActivate, ahk_class WindowClassName ; activate window

    Sleep, 1000
    ControlClick, X100 Y250, ahk_class WindowClassName ; click
    Sleep, 300000
}
  • WinActivate
  • ControlClick
    • ControlClick , Control-or-Pos, WinTitle, WinText, WhichButton, ClickCount, Options, ExcludeTitle, ExcludeText
    • Control-or-Pos: 坐标默认是相对于 window 左上角。
    • WinTitle。建议用 ahk 自带的 Window Spy 工具查看。

运行程序

Run

Run, Target , WorkingDir, Options, OutputVarPID
RunWait, Target , WorkingDir, Options, OutputVarPID
  • Options: 指定 Hide 在后台运行(适合 CLI 程序)。

ahk in rdp

测试发现,WinActivate 和 ControlClick 等很多命令对 rdp 会话局限性很大,如果 rdp 断开(disconnected) 甚至只要 rdp 窗口最小化,那么 WinActivate / ControlClick / Send 等都无法触发(rdp 窗口不在前台显示时可以,只要没有最小化)。这个是由于 Windows 自身的限制,非交互式的桌面会话(non-interactive,rdp disconnected 或最小化 rdp 窗口均属于这种)对 UI automation 非常不友好

尝试的 workaround 是:

  • rdp in vnc. 弄一台 Linux 服务器跑 rdp client 连接目标 Windows。 用 VNC 远程连接这台 Linux。测试可。VNC 窗口关闭时也可以触发 rdp。
  • rdp in rdp. 弄一台 Windows 服务器 rdp 连接目标 windows (这个内层的 rdp 窗口不要最小化). 外层的 rdp 可以最小化,不影响目标 windows 上的 ahk。但外层 rdp disconnected 后内层 rdp 里的 ahk 仍然无法运行。
    • 傻逼 rdp 就是坑货。

常用脚本

GeneralControl.ahk

; ctrl + [ to Esc
^[::Send {Esc}

VolumeControl.ahk

; ctrl + alt + [: mute
^![::Send {Volume_Mute}

; alt + [: volume down
![::Send {Volume_Down 1}

; alt + ]: volume up
!]::Send {Volume_Up 1}

按 "Alt + [" 和 "Alt + ]" 分别降低 / 增加音量。

MediaControl.ahk

; ctrl + shift + P: Play / Pause
^+P::Send {Media_Play_Pause}

; ctrl + shift + J: Next track
^+J::Send {Media_Next}

; ctrl + shift + K: Previous track
^+K::Send {Media_Prev}

TakeScreenshot.ahk

#persistent

; take screenshot and save to png file
; key: PrintScreen to capture full screen; Alt + PrintScreen or Alt + F12 to capture current window
; Require IrfanView (x64) https://www.irfanview.com/

screenshot1=%A_programfiles%\IrfanView\i_view64.exe ;- program
screenshots=D:\files\Drive\data\Screenshots ;- save folder

;-- button printscreen
;-- fullscreen
*~$printscreen::
SetTitleMatchMode, 2
ifexist,%screenshot1%
    {
   runwait, "%screenshot1%" "/capture=0 /convert=%screenshots%\screenshot_%A_now%.png"
   ;ifexist, %screenshots%
   ;  run,%screenshots%
   return
    }
return

~$!printscreen::
!F12::
SetTitleMatchMode, 2
ifexist,%screenshot1%
    {
    runwait, "%screenshot1%" "/capture=1 /convert=%screenshots%\screenshot_%A_now%.png"
    ;ifexist, %screenshots%
    ;  run,%screenshots%
    return
    }
return
;--------------

按 PrintScreen / Alt + PrintScreen 或 F12 / Alt + F12 截图并自动保存到某个文件夹。

需要安装 IrfanView。

IrfanView 默认设置下截图时包含鼠标光标。去除方式:运行一次 IrfanView 程序,点击 Options - Capture/Screenshot, 取消 Options 区域的 "include mouse cursor" 选项,点击 Start,然后再退出。

VolumeControlAllDevices.ahk

需要安装 NirCmd 位于 PATH。同时调整系统里所有播放设备的音量。而键盘的媒体按键只会调整默认播放设备的音量。

; need nircmd

; ctrl + alt + [: mute
^![::
Run nircmd.exe mutesysvolume 2 speakers 0, Hide
Run nircmd.exe mutesysvolume 2 speakers 1, Hide
Run nircmd.exe mutesysvolume 2 speakers 2, Hide
; Run nircmd.exe mutesysvolume 2 speakers 3, Hide
return

; alt + [: volume down
![::
Run nircmd.exe changesysvolume -2000 speakers 0, Hide
Run nircmd.exe changesysvolume -2000 speakers 1, Hide
Run nircmd.exe changesysvolume -2000 speakers 2, Hide
; Run nircmd.exe changesysvolume -2000 speakers 3, Hide
return

; alt + ]: volume up
!]::
Run nircmd.exe changesysvolume +2000 speakers 0, Hide
Run nircmd.exe changesysvolume +2000 speakers 1, Hide
Run nircmd.exe changesysvolume +2000 speakers 2, Hide
; Run nircmd.exe changesysvolume +2000 speakers 3, Hide
return

Last update: 2022-08-18 01:46:12 UTC