AutoIt ControlSend()函数偶尔用下划线替换连字符

时间:2016-08-05 19:02:32

标签: automation autoit

我使用AutoIt脚本自动与GUI交互,部分过程涉及使用ControlSend()函数将文件路径放入组合框。大多数情况下,该过程正常,但偶尔(约1/50调用函数?)文件路径中的单个连字符将替换为下划线。该脚本将在无人监督的情况下运行以进行批量数据处理,并且此类错误通常会导致强制关注弹出窗口发出尖叫声"无法找到该文件!"并停止进一步处理。

不幸的是,由于组合框的字符限制,我无法通过一次调用提供所有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的第一个问题,我为我缺乏经验,措辞不当或偏离StackOverflow惯例而道歉。

2 个答案:

答案 0 :(得分:1)

如果连字符是问题而你需要更换它,你可以这样做:

#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, '\1_\3')
        FileMove($sPath & '\' & $aFiles[$i], $sPath  & '\' & $sTmpName)
    EndIf
Next

答案 1 :(得分:1)

只需使用ControlSetText代替ControlSend,因为它会立即设置完整的文字,并且不允许其他按键(如Shift)干扰发送的许多虚拟按键功能火灾。