Programmatically send key strokes to a window program in Groovy or bat script

后端 未结 3 1385
一整个雨季
一整个雨季 2021-01-07 14:17

Backstory: I need to programmatically find the differences between two files. I want to use WinMerge to generate a report (Tools -> Generate Report) that I can parse to ge

3条回答
  •  借酒劲吻你
    2021-01-07 14:46

    This is something that I threw together without testing, and I don't have WinMerge so testing isn't really an option.

    RunMe.bat

    start "" "C:\Path\WinMerge.exe" "file1" "file2"
    GenerateDiffFile.vbs
    

    GenerateDiffFile.vbs

    Set s = CreateObject("WScript.Shell")
    
    wscript.sleep(1000) ' Sleep for 1 second to allow time for WinMerge to finish loading and merge the files
    
    s.SendKeys("%tr")
    wscript.sleep(250) ' Give time for dialog box to appear
    s.SendKeys("Diff.html{Enter}")
    

    This is completely untested, but I think it is very close to what you need... You can see that the batch-file runs WinMerge, passing the two files to merge on the command line. Then it launches the VBS script, which pauses long enough to allow the launched WinMerge to be able to accept keyboard input. I used this page from Microsoft to know what keys to send.

    I've been sending keys to programs for years. It works very reliably, as long as the program you want to send keys to keeps focus.

提交回复
热议问题