Created
March 18, 2015 10:08
-
-
Save ambyte/f2e77c771b6b816bdea0 to your computer and use it in GitHub Desktop.
Get selected text with UI Automation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static string GetText() | |
{ | |
try | |
{ | |
var element = AutomationElement.FocusedElement; | |
if (element != null) | |
{ | |
object pattern; | |
if (element.TryGetCurrentPattern(TextPattern.Pattern, out pattern)) | |
{ | |
var tp = (TextPattern)pattern; | |
var sb = new StringBuilder(); | |
foreach (var r in tp.GetSelection()) | |
{ | |
sb.AppendLine(r.GetText(-1)); | |
} | |
return sb.ToString(); | |
} | |
} | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e); | |
} | |
return ""; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment