Getting text content from window

This topic contains 9 replies, has 5 voices, and was last updated by  Hil 2 weeks ago.

  • Author
    Posts
  • #80407

    Hil
    Participant

    Hi,
    I have an application which creates a window with text in it.
    How can I extract the TEXT from this window.
    The name of the window is called "Message Center".

    I am able to get a handle to the window with:
    Get-Process | Where-Object {$_.MainWindowTitle -like "*Message*"}

    Thanks
    Hil

  • #80419

    postanote
    Participant

    But if you are creating the Window, you already know what the text is.

    Why go and try to extract something you manually put there?

    Or does your app put text there based on other constructs in your app?

    Either way, if you app fires up a process window, you can get all titles of running apps this way.

    Get-Process | Where {$_.mainWindowTItle} | Format-Table MainWindowtitle -AutoSize

  • #80420

    Olaf Soyk
    Participant

    There is no build in way for Powershell to get the text of any other Windows application window if this Windows application does not provide an API for this.

  • #80606

    Hil
    Participant

    postanote, Thanks for your comments.
    I am not looking for the title of the window. I already know that.
    I am looking for the TEXT in the window. With your command
    Get-Process | Where {$_.MainWindowTitle -like "*Message*"} | Format-Table MainWindowtitle -AutoSize
    ....here is what i get

    "Message Center Main@thinkorswim [build 1908.52]"
    This is only the title of the window. I need to extract the text from inside the window

    Is there a way I get powershell to do something similar to https://stackoverflow.com/questions/375117/screen-scraping-a-windows-application-in-c-sharp ... maybe WM_GETTEXT

    • #80933

      postanote
      Participant

      Ahhh.. mea culpa, I misread you use case.

      Along with the UIAutomation pointed to earlier by Rick, there is a tool on CodePlex you can use that might save you this work.
      'wasp.codeplex.com'
      It is open source, so you can see how things are done and tweak to your needs.
      It was last published in 2009, so a bit old, but still works.

      Though I have not tried it for this specific thing, you could look at its...
      GetWindowText Method string GetWindowText()

      Without digging at the source right now, I would assume in the DLL they are using this implementation in some creative way:

      'msdn.microsoft.com/en-us/library/windows/desktop/ms633520(v=vs.85).aspx'

      Copies the text of the specified window's title bar (if it has one) into a buffer. If the specified window is a control, the text of the control is copied. However, GetWindowText cannot retrieve the text of a control in another application.

      Which gets you back to your idea of using — WM_GETTEXT

      Remarks

      If the target window is owned by the current process, GetWindowText causes a WM_GETTEXT message to be sent to the specified window or control. If the target window is owned by another process and has a caption, GetWindowText retrieves the window caption text. If the window does not have a caption, the return value is a null string. This behavior is by design. It allows applications to call GetWindowText without becoming unresponsive if the process that owns the target window is not responding. However, if the target window is not responding and it belongs to the calling application, GetWindowText will cause the calling application to become unresponsive.

      To retrieve the text of a control in another process, send a WM_GETTEXT message directly instead of calling GetWindowText.

    • #81244

      Hil
      Participant

      Hi Postanote, Thanks for the details.
      I did try wasp but ran into problems installing it. Via powershell it says I am not running as administrator, when I actually am.. It shows in the PS window title.

      I tried importing the module , but that failed too. Is the current version of wasp working for you?
      or maybe I am doing something wrong.

      Thanks
      Hil

  • #80699

    Rick
    Participant

    You can use UIAutomation for this. I am far from an expert or even that knowledgeable on the subject but I am interested in figuring it out. I found FlaUI https://github.com/Roemer/FlaUI which allows you to use UIAutomation through powershell. I was able to come up with the following code to pull the text from a notepad window. I used FlaUIInspect to find the controlid. I wonder if you could do something similar with your app. If you figure it out please share your code so we can all benefit.

    Add-Type -Path 'E:\Development\MyGitHub\FlaUI\src\FlaUI.UIA3\bin\Debug\FlaUI.Core.dll'
    Add-Type -Path 'E:\Development\MyGitHub\FlaUI\src\FlaUI.UIA3\bin\Debug\FlaUI.UIA3.dll'
    $app = [FlaUI.Core.Application]::AttachOrLaunch('notepad')
    $uia = New-Object FlaUI.UIA3.UIA3Automation
    $mw = $app.GetMainWindow($uia)
    $Document = $mw.FindFirstChild($uia.ConditionFactory.ByControlType([FlaUI.Core.Definitions.ControlType]::Document))
    Write-Output $mw.Title
    Write-output $Document.Patterns.Value.Pattern.Value.Value
    
  • #80927

    Hil
    Participant

    Thanks Rick. That is really great. I went though UIAutomation and did find a world of things I never knew.
    I am still going though it and no luck on my current project, but its good to know that UIAutomationSpy.exe can do the heavy lifting for you of writing the complex part of the script.
    Another cool tool is Inspect (https://msdn.microsoft.com/en-us/library/windows/desktop/dd318521(v=vs.85).aspx)

  • #81053

    Roman
    Participant

    Hi
    I am the author of FlaUI. As already mentioned, FlaUI can be used with Powershell (down to 2.0 because FlaUI is multi-targetted also to .net 3.5). It abstracts UI Automation to a usable level with providing everything UIA provides.
    Feel free to open issues in FlaUI or post code samples there so I could add them to the wiki for others.
    Regards
    Roman

    • #81236

      Hil
      Participant

      Hi Roman, Thanks for your message.
      When I run the UIAutomationSpy.exe , It is not able to get a handle to the text.

      Digressing a bit, I have seen when components of a webpage want to be hidden from being accessed directly, they place it into a frame (iframe). The way around uncovering the components of the webpage would be getting the iFrame link and then navigating to the iFrame link

      Similarly, i am thinking the components of this "Message Center" window are shielded inside some kind of frame as you will see in the screenshots. Could that be the reason why UIAutomationSpy is not able to see inside the window. Any way around it.. or accessing or calling the frame directly so that we can get a handle to the text?

      Code section:

      Script section:

      Thanks
      Hil

You must be logged in to reply to this topic.