用Groovy或bat脚本以编程方式将击键发送到窗口程序 [英] Programmatically send key strokes to a window program in Groovy or bat script

查看:210
本文介绍了用Groovy或bat脚本以编程方式将击键发送到窗口程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景故事:我需要以编程方式找到两个文件之间的差异.我想使用WinMerge生成一个报告(工具->生成报告),我可以对其进行分析以获取两个文件之间的差异.我需要使用Groovy脚本或bat脚本来完成此操作.

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 get the differences between two files. I need this done using either a Groovy script or a bat script.

我希望WinMerge提供命令行选项来生成报告,然后我可以使用命令选项,没有这样的运气.

I was hoping that WinMerge would offer command line options to generate the report and then I could just use a Groovy Process object to execute WinMergeU.exe with the arguments. No such luck according to the command options I've found for WinMerge.

接下来,我希望能够启动WinMerge并发送击键以逐步浏览菜单以生成报告(Alt + T,R,Diff.html,[Enter]).我没有找到通过Groovy Process做到这一点的方法,也没有找到在bat脚本中做到这一点的方法.我正在寻找类似于VB中的WshShell.Sendkeys的东西.这是野鹅追逐吗?

Next, I was hoping to be able to start WinMerge and send keystrokes to step through the menus to generate the report(Alt+T, R, Diff.html, [Enter]). I don't see a way to do that from a Groovy Process and I haven't found a way to do this in a bat script. I'm looking for something similar to WshShell.Sendkeys in VB. Is this a wild-goose chase?

在bat文件中使用PowerShell更新/答案: Knuckle-Dragger关于在bat文件中使用PowerShell脚本的评论引起了我的兴趣.

UPDATE/Answer with PowerShell in a bat file: I was intrigued by Knuckle-Dragger's comment about using a PowerShell script in a bat file.

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

要将其封装在命令窗口中,请将其保存到文件 PowerShellScript.ps1 :

To encapsulate this in a command window, save it to a file PowerShellScript.ps1:

start /b /wait powershell.exe -nologo -WindowStyle Hidden -sta *PowerShellScript.ps1*

推荐答案

下面是一个强大的示例,用于激活winmerge并发送一些密钥.

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

带有某些.NET变量的简化版面食. $ SK = Sendkeys $ AA = AppActivate $ LRA =反映.NET.

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}")'

要将其封装在命令窗口中,请将其保存到文件 PowerShellScript.ps1 :注意,稍微更改了命令语法,如果使用& {.\ dot \ source \ path}

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}"

这篇关于用Groovy或bat脚本以编程方式将击键发送到窗口程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆