我正在使用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:myfilepathhdr_2016-04-22T080033_00_rgb
^Hyphen
转换为
C:myfilepathhdr_2016_04-22T080033_00_rgb
^Underscore
由于文件名中同时存在连字符和下划线,因此很难执行程序性更正(例如,用连字符替换所有下划线(。
如何纠正或防止此类错误?
这既是我对GUI自动化的第一次尝试,也是我对SO的第一个问题,我为我缺乏经验、措辞不当或偏离StackOverflow约定而道歉。
只需使用ControlSetText
而不是ControlSend
,因为它将一次设置完整的Text,并且不允许其他按键(如Shift(干扰Send函数触发的许多虚拟按键。
如果连字符有问题,需要更换,可以这样做:
#include <File.au3>
; your path
$sPath = 'C:myfilepath'
; 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