Passing GUI text to PowerShell
I'm curious if anyone has devised a clever way to pass text from the Windows GUI to a script. I have an application which does not have a commandline interface, which I need to pass it's text output to a PowerShell Script. My initial thought was to capture the screen to an image file and then process it with an OCR tool. However, I've not found an OCR commandline tool which will adequately recognize the text. I know this question is a bit "out of the box", but if anyone has any suggestions or might be able to point me in the direction of someone who has accomplished this, that'd be great! Perhaps there is some way to accomplish through .NET?
August 25th, 2015 10:26am

https://wasp.codeplex.com/

With WASP you should be able to do something like

- get the window position

- position the cursor, click or right click, or send key inputs

- copy the text to the cpliboard

- process it in your script


Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 10:49am

Use the clipboard.  Ctl-C then paste it in PowerShell.

Not really a scripting issue. More an issue of how to use basic Windows commands.

August 25th, 2015 10:50am

Thanks for the suggestion. I have never come across these cmdlets but they look as though I may be able to use them as some point. I was initially thinking using SendKeys, but I need to clarify. This text is not selectable. This was why I had gone down the OCR route.
Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 11:22am

@jrv, sorry, let me clarify. This text is not selectable. If it was just an issue of knowing how to use basic Windows commands, I wouldn't be writing an advanced PowerShell script! hahah :)
  • Edited by Aldohr 15 hours 50 minutes ago
August 25th, 2015 11:23am

Thanks for the suggestion. I have never come across these cmdlets but they look as though I may be able to use them as some point. I was initially thinking using SendKeys, but I need to clarify. This text is not selectable. This was why I had gone down the OCR route.

As a note - SendKeys won't help you here. Avoid SendKeys at all costs, as it is completely and utterly unreliable.

Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 11:23am

If you cannot copy the text to the clipboard then you are of of luck.
August 25th, 2015 11:24am

I've generally found I'm only out of luck if I'm out of creativity.
Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 11:26am

@jrv, sorry, let me clarify. This text is not selectable. If it was just an issue of knowing how to use basic Windows commands, I wouldn't be writing an advanced PowerShell script! hahah :)

Since you know Windows then you also know that what you are trying to do, as you are describing it, cannot be done.  You cannot readily copy a windows and decode it as this would be a great way to steal information.  Windows uses may technical methods to prevent what you are trying to do.  If the windows contents can be copied to the clipboard then you can, sometimes, copy  it.
August 25th, 2015 11:27am

I've generally found I'm only out of luck if I'm out of creativity.

A windows is a graphic.  It can be grabbed with a screen grabber or by using print screen. OCR of screen text seldom works as TrueType fonts on the screen are not easily convertible.

Your issue is not a scripting issue but one of how to find and use a utility that can grab a graphic.

Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 11:30am

jrv, Of course. But as this is a problem that perhaps others have faced during scripting, I posed the question thinking there may be some functionality within PowerShell I am overlooking or that someone else may have had some experience with this. Thank you for your input. I understand that you have not found a way to do this, however, I will continue working through the problem. Perhaps there are others reading who are willing to think less categorically.
August 25th, 2015 11:51am

Good luck.  I have  worked with the Windows APIs for almost 30 years. There is no mechanism in Windows or in PowerShell to do this. You might be able to get somewhere with the debug API but that would require a fundamental knowledge of Windows technologies and the Win32API as well as the debug subsystem.

Your issue cannot be solved with PowerShell.

It is like the speed of light.  I know physics well enough to guarantee you that you cannot run faster than the speed of light.  You can be as creative as you want but I can still guarantee you that you will never find a way to run faster than the speed of light.

Start simple. As if there is any way to convert screen graphics with any OCR software.  Find that answer and maybe you will get closer.

Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 12:23pm

Previously I was capturing the screen to an image and then trying to run OCR on the image. However, I found a tool that will do this all in one command and is giving me very accurate results:

http://capture2text.sourceforge.net/

I was able to combine this tool with WASP (https://wasp.codeplex.com/) (Thanks psott for the great suggestion!)

$WindowTitle = "MyWindow" #Title of window to capture

#First, we need to figure out where the window is located
$Window = Select-Window -Title "*$WindowTitle*"
$WindowPosition = Get-WindowPosition -Window $Window.Handle

#We better make sure the window is on top
Set-WindowActive -Window $Window.Handle

#Start the OCR tool with commandline parameters
$Exe = Get-Item .\Capture2Text\Capture2Text.exe
$Command = '"' + $Exe.FullName + '" ' + $WindowPosition.X + ' ' + $WindowPosition.Y + ' ' + $WindowPosition.Right + ' ' + $WindowPosition.Bottom + ' ' + 'OCR.txt'
$Shell = New-Object -ComObject "WScript.Shell"
$Shell.Run($Command,"1","true")

#Let's see what we got
Get-Content .\Capture2Text\OCR.txt


August 25th, 2015 1:33pm

Still not a powershell issue it is a tool issue.

Glad you were able too find something that works.  You would have saved time if you had just search for the tool without any reference to PowerShell.  PowerShell just doesn't have those capabilities and there are really no direct PowerShell methods for manipulating graphics in another process.

Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 1:50pm

Yep, I'm still not sure why you're hung up on defining the "issue" But I understand at the end of the day. "You are right."

The question was "has [anyone] devised a clever way to pass text from the Windows GUI to a script." The point was to get GUI text into powershell, which is what I've done. Seeing how you don't know my search history, I'd invite you to not make assumptions. Cheers.

August 25th, 2015 2:12pm

@jrv, sorry, let me clarify. This text is not selectable. If it was just an issue of knowing how to use basic Windows commands, I wouldn't be writing an advanced PowerShell script! hahah :)
  • Edited by Aldohr Tuesday, August 25, 2015 3:20 PM
Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 3:20pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics