AutoIt ControlSend() 函数有时会将连字符替换为下划线

AutoIt ControlSend() function occasionally replaces hyphens with underscores

我正在使用 AutoIt 脚本自动与 GUI 进行交互,部分过程涉及使用 ControlSend() 函数将文件路径放入组合框中。大多数时候,该过程正常工作,但偶尔(~ 1/50 调用函数?)文件路径中的单个连字符被下划线替换。该脚本将 运行 不受监督地进行批量数据处理,此类错误通常会导致强制聚焦弹出窗口尖叫 "The file could not be found!" 并停止进一步处理。

不幸的是,由于组合框的字符限制,我无法通过一次调用提供所有 16 个参数,我不得不使用以下 for 循环单独加载每个图像:

;Iterate through each command line argument (file path)
For $i = 1 To $CmdLine[0]
    ;click the "Disk" Button to load an image from disk
    ControlClick("Assemble HDR Image", "", "[CLASS:Button; TEXT:Disk; Instance:1]")
    ;Give the dialogue time to open before entering text
    Sleep(1000)
    ;Send a single file path to the combo box
    ControlSend("Open", "" , "Edit1", $CmdLine[$i])
    ;"Press Enter" to load the image
    Send("{ENTER}")
Next

在错误运行中,文件路径

C:\my\file\path\hdr_2016-04-22T080033_00_rgb
                        ^Hyphen

转换为

C:\my\file\path\hdr_2016_04-22T080033_00_rgb
                        ^Underscore

由于文件名中同时存在连字符和下划线,因此难以执行编程更正(例如,将所有下划线替换为连字符)。

如何纠正或防止此类错误?


这是我第一次尝试 GUI 自动化,也是我第一个关于 SO 的问题,对于我缺乏经验、措辞不佳或偏离 Whosebug 惯例,我深表歉意。

如果连字符有问题而您需要替换它,您可以这样做:

#include <File.au3>

; your path
$sPath = 'C:\my\file\path'

; get all files from this path
$aFiles = _FileListToArray($sPath, '*', 1)

; if all your files looks like that (with or without hyphen), you can work with "StringRegExpReplace"
; 'hdr_2016-04-22T080033_00_rgb'

$sPattern = '(\D+\d{4})(.)(.+)'
; it means:
; 1st group: (\D+\d{4})
;    \D+    one or more non-digit, i.e. "hdr_"
;    \d{4}  digit 4-times, i.e. "2016"
; 2nd group: (.)
;    .      any character, hyphen, underscore or other, only one character, i.e. "~"
; 3rd group: (.+)
;    .      any character, one or more times, i.e. "22T080033_00_rgb"

; now you change the filename for all cases, where this pattern matches
Local $sTmpName
For $i = 1 To $aFiles[0]
    ; check for pattern match
    If StringRegExp($aFiles[$i]) Then
        ; replace the 2nd group with underscore
        $sTmpName = StringRegExpReplace($aFiles[$i], $sPattern, '_')
        FileMove($sPath & '\' & $aFiles[$i], $sPath  & '\' & $sTmpName)
    EndIf
Next

只需使用 ControlSetText 而不是 ControlSend,因为它会立即设置完整的文本,并且不允许其他击键(如 Shift)干扰发送-函数触发。