Skip to content

Instantly share code, notes, and snippets.

@ambyte
Created March 18, 2015 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ambyte/f2e77c771b6b816bdea0 to your computer and use it in GitHub Desktop.
Save ambyte/f2e77c771b6b816bdea0 to your computer and use it in GitHub Desktop.
Get selected text with UI Automation
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