【问题标题】:AutoIt ControlSend() function occasionally replaces hyphens with underscoresAutoIt ControlSend() 函数偶尔会用下划线替换连字符
【发布时间】:2016-08-05 19:02:32
【问题描述】:

我正在使用 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 约定有偏差,我深表歉意。

【问题讨论】:

  • 脚本运行时是否按了 shift 键?您可以使用BlockInput(...),但要小心防止这种情况发生。您是否与ConsoleWrite(...) 核对过所需的输出数据是否与输入数据相同?您可能会摆弄 SendKeyDelay 选项以使其处理得更快,但这种奇怪行为的原因并不明显......或者您可以使用剪贴板和Send("^v") 来一次插入完整的文件路径。或者只使用ControlSetText 而不是ControlSend
  • @Samoth 太棒了!使用 ControlSetText 似乎可以解决问题。虽然我仍然对问题发生的原因感到好奇,但这可以完成工作。我愿意接受此作为答案,如果您将其移至答案类别,我会这样做。

标签: automation autoit


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    #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
    

    【讨论】:

    • 感谢您的意见!我实际上不知道正则表达式分组(反向引用?)和替换模式的某些部分。看起来正则表达式方法可以很好地替换/标准化输入,但问题似乎与正确输出信息有关。
    猜你喜欢
    • 2018-05-20
    • 2015-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-18
    相关资源
    最近更新 更多