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

后端 未结 3 1387
一整个雨季
一整个雨季 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:59

    Here is a powershell example to activate winmerge and send some keys.

    EDIT: Reduced copy pasta with some .NET variables. $SK = Sendkeys $AA = AppActivate $LRA = Reflect .NET.

    $startapp = "C:\DevTools\WinMerge\WinMergeU.exe"
    ii $startapp
    $SK = "[System.Windows.Forms.SendKeys]::SendWait"
    $AA = "[Microsoft.VisualBasic.Interaction]::AppActivate"
    $LRA = "[void][System.Reflection.Assembly]::LoadWithPartialName"
    Start-Sleep 1
    $LRA+'("Microsoft.VisualBasic")'
    $AA+'("WinMerge")'
    Start-Sleep -m 100
    $LRA+'("System.Windows.Forms")'
    Start-Sleep -m 100
    $SK+'("%F")'
    $SK+'("o")'
    $SK+'("{ENTER}")'
    $SK+'("%T")'
    $SK+'("r")'
    Start-Sleep 1
    $AA+'("Save As")'
    Start-Sleep 1
    $SK+'("Diff.txt")'
    $SK+'("{ENTER}")'
    

    To encapsulate this in a command window, save it to a file PowerShellScript.ps1: Note, changed the command syntax a bit, should work if you use the & {.\dot\source\path}

    start /b /wait powershell.exe  -nologo -WindowStyle Hidden -sta -Command "& {.\PowerShellScript.ps1}"
    

提交回复
热议问题