You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AutoHotkey脚本跨PC控件类名不一致,正则适配失败求助

问题:AutoHotkey控件类名跨PC适配失败(正则表达式无效)

我编写了一款AutoHotkey脚本,用于通过控件类名定位目标输入框并填充CSV中的数据。该脚本在本地PC运行正常,但在不同PC上发现控件类名存在差异,部分为r8、r7或r6。尝试使用正则表达式进行适配,但疑似语法存在问题,无法实现预期效果,求解决方案。

原脚本代码

; 载入CSV数据到内存
material_map := {}
FileRead, csvdata, mapping.csv
Loop, parse, csvdata, `n, `r
{
    if (A_Index = 1) ; 跳过标题行
        continue
    fields := StrSplit(A_LoopField, ",")
    if (fields.Length() >= 4) ; 只处理四个字段
    {
        no := Trim(fields[1])
        material_map[no] := material_map[no] ? material_map[no] . "|" . fields[2] . "," . fields[3] . "," . fields[4] : fields[2] . "," . fields[3] . "," . fields[4]
    }
}


^q::
    SetTimer, on_top, 50  ; Set a timer to apply always on top
    InputBox, userInput, 输入料号, 请手动输入料号或复制料号至此, , 220, 120  ; Launch inputbox
    SetTimer, on_top, Off  ; Stop on_top timer when done
    if (ErrorLevel)  ; Check if the user canceled the input
        return
    userInput := Trim(userInput)

    if !material_map.HasKey(userInput)
    {
        MsgBox, 未找到料号:%userInput%,请确认输入是否正确。
        return
    }

    options := StrSplit(material_map[userInput], "|")
    if (options.Length() = 1)
    {
        selected := options[1]
    }
    else
    {
        ; 创建选择框供用户选择
        choice := ""
        Loop, % options.Length()
        {
            choice .= A_Index . ": " . options[A_Index] . "`n"
        }
        choice .= "请选择一个选项:"
        InputBox, userChoice, 选择品名, %choice%, , 300, 200
        if (ErrorLevel || userChoice < 1 || userChoice > options.Length())
            return
        selected := options[userChoice]
    }

    selectedFields := StrSplit(selected, ",")

    MsgBox, % "系统即将自动填写以下数据:`n`n"
        . "输入的料号:" userInput "`n"
        . "品名:" selectedFields[1] "`n"
        . "原材料编号:" selectedFields[2] "`n"
        . "原材料UL认证档案号:" selectedFields[3]

    ;WinActivate, ahk_class WindowsForms10.Window.8.app.0.13965fa_r8_ad1
    
    WinActivate, TCL瑞智二维码打印软件 

    sleep, 200

    ; 填写料号
    controlfocus, WindowsForms10.EDIT.app.0.13965fa_r8_ad110, A
    controlsettext, WindowsForms10.EDIT.app.0.13965fa_r8_ad110, % userInput, A
    sleep, 200

    ; 填写品名
    controlfocus, WindowsForms10.EDIT.app.0.13965fa_r8_ad116, A
    controlsettext, WindowsForms10.EDIT.app.0.13965fa_r8_ad116, % selectedFields[1], A
    sleep, 200

    ; 填写原材料编号
    controlfocus, WindowsForms10.EDIT.app.0.13965fa_r8_ad111, A
    controlsettext, WindowsForms10.EDIT.app.0.13965fa_r8_ad111, % selectedFields[2], A
    sleep, 200

    ; 填写原材料ul认证档案号
    controlfocus, WindowsForms10.EDIT.app.0.13965fa_r8_ad12, A
    controlsettext, WindowsForms10.EDIT.app.0.13965fa_r8_ad12, % selectedFields[3], A
    sleep, 200

on_top:  ; 
    ib_ahk := "ahk_class #32770"  ; The class for the inputbox
    if WinExist(ib_ahk)  ; When it exists
        WinSet, AlwaysOnTop, On, ahk_class #32770  ; Apply always on top attribute
return

尝试的正则适配代码(无效)

SetTitleMatchMode, RegEx
    sleep, 200

    controlfocus, WindowsForms10\.EDIT\.app\.0\.13965fa_r\d_ad110, A
    controlsettext, WindowsForms10\.EDIT\.app\.0\.13965fa_r\d_ad110, % userInput, A
    sleep, 200

    controlfocus, WindowsForms10\.EDIT\.app\.0\.13965fa_r\d_ad116, A
    controlsettext, WindowsForms10\.EDIT\.app\.0\.13965fa_r\d_ad116, % selectedFields[1], A
    sleep, 200

    controlfocus, WindowsForms10\.EDIT\.app\.0\.13965fa_r\d_ad111, A
    controlsettext, WindowsForms10\.EDIT\.app\.0\.13965fa_r\d_ad111, % selectedFields[2], A
    sleep, 200

    controlfocus, WindowsForms10\.EDIT\.app\.0\.13965fa_r\d_ad12, A
    controlsettext, WindowsForms10\.EDIT\.app\.0\.13965fa_r\d_ad12, % selectedFields[3], A
    sleep, 200

解决方案

问题原因

你只设置了SetTitleMatchMode, RegEx,这个命令仅对窗口标题的正则匹配生效,而控件类名的匹配默认是精确匹配,所以你的正则表达式不会被识别。

修正步骤

  1. 启用控件正则匹配模式:在使用控件正则匹配前,添加SetControlMatchMode, RegEx,这个设置是全局的,如果后续需要精确匹配控件,记得用SetControlMatchMode, 1改回默认模式。
  2. 优化正则表达式:
    • 转义所有.(正则中.代表任意字符,需要用\.表示实际的点)
    • 用r\d+代替r\d,可以匹配r后面1位或多位数字(覆盖更多可能的版本号)
    • 可选:添加^和$锚定,确保完全匹配控件类名,避免误匹配相似控件

修正后的代码片段

WinActivate, TCL瑞智二维码打印软件 
sleep, 200

; 启用控件正则匹配模式
SetControlMatchMode, RegEx

; 填写料号
controlfocus, ^WindowsForms10\.EDIT\.app\.0\.13965fa_r\d+_ad110$, A
controlsettext, ^WindowsForms10\.EDIT\.app\.0\.13965fa_r\d+_ad110$, % userInput, A
sleep, 200

; 填写品名
controlfocus, ^WindowsForms10\.EDIT\.app\.0\.13965fa_r\d+_ad116$, A
controlsettext, ^WindowsForms10\.EDIT\.app\.0\.13965fa_r\d+_ad116$, % selectedFields[1], A
sleep, 200

; 填写原材料编号
controlfocus, ^WindowsForms10\.EDIT\.app\.0\.13965fa_r\d+_ad111$, A
controlsettext, ^WindowsForms10\.EDIT\.app\.0\.13965fa_r\d+_ad111$, % selectedFields[2], A
sleep, 200

; 填写原材料ul认证档案号
controlfocus, ^WindowsForms10\.EDIT\.app\.0\.13965fa_r\d+_ad12$, A
controlsettext, ^WindowsForms10\.EDIT\.app\.0\.13965fa_r\d+_ad12$, % selectedFields[3], A
sleep, 200

; 可选:如果后续有精确匹配控件的需求,改回默认模式
; SetControlMatchMode, 1

内容的提问来源于stack exchange,提问作者夏浅沫

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 18:57:03