GetText Method

Applies to TestComplete 15.44, last modified on November 10, 2022
This method is provided by the legacy OCR plugin. In version 12.60, the plugin was replaced with the new Optical Character Recognition plugin powered by Google Cloud Vision API. To learn more, see Optical Character Recognition.
The legacy OCR plugin was removed from TestComplete in version 12.60. If you need to use objects, methods, and properties provided by the legacy plugin with this version of TestComplete, please contact our Customer Care team. The legacy OCR plugin was restored in TestComplete version 14.0. To use the objects, methods, and properties with this or later TestComplete version, you need to install and enable the plugin manually.

Description

The OCRObject.GetText method lets you recognize all the text that is available within the visible area of an onscreen object in order to obtain text.

Declaration

OCRObjectObj.GetText(Options)

OCRObjectObj An expression, variable or parameter that specifies a reference to an OCRObject object
Options [in]    Optional    An OCROptions object    
Result String

Applies To

The method is applied to the following object:

Parameters

The method has the following parameter:

Options

Specifies the OCROptions object that includes settings to be used during character recognition.

Result Value

A string that represents the recognized text.

Remarks

A number of factors affect the recognition accuracy, read the Using Optical Character Recognition - Tips topic to learn how to improve it.

The following example recognizes the text written in Windows Notepad and posts the obtained text to the test log.

JavaScript, JScript

function Main()
{
  var p, w, w1, OCRObj, OCROptions, Font, Rect, s;
  p = Sys.Process("notepad");
  w = p.Window("Notepad", "*").Window("Edit");
  Rect = w.Picture(0, 0, 200, 25);
  Log.Picture(Rect, "Region with text to be recognized.");
  OCRObj = OCR.CreateObject(Rect);
  OCROptions = OCRObj.CreateOptions();
  p.Window("Notepad", "*").MainMenu.Click("Format|Font...");
  w = p.Window("#32770", "Font");
  Font = OCROptions.Fonts.Add();
  Font.Name = w.Window("ComboBox", "", 1).wText;
  Font.Sizes.Add(aqConvert.VarToInt(w.Window("ComboBox", "", 3).wText));
  w.Window("ComboBox", "", 1).ClickItem("Arial");
  w.Window("ComboBox", "", 2).ClickItem("Regular");
  w.Window("Button", "OK").ClickButton();
  aqUtils.Delay(1000);
  s = OCRObj.GetText(OCROptions);
  Log.Message(s, "", pmNormal, null, Rect);
}

Python

def Main():
  p = Sys.Process("notepad")
  w = p.Window("Notepad", "*").Window("Edit")
  Rect = w.Picture(0, 0, 200, 25)
  Log.Picture(Rect, "Region with text to be recognized.")
  OCRObj = OCR.CreateObject(Rect)
  OCROptions = OCRObj.CreateOptions()
  p.Window("Notepad", "*").MainMenu.Click("Format|Font...")
  w = p.Window("#32770", "Font")
  Font = OCROptions.Fonts.Add()
  Font.Name = w.Window("ComboBox", "", 1).wText
  Font.Sizes.Add(aqConvert.VarToInt(w.Window("ComboBox", "", 3).wText))
  w.Window("ComboBox", "", 1).ClickItem("Arial")
  w.Window("ComboBox", "", 2).ClickItem("Regular")
  w.Window("Button", "OK").ClickButton()
  aqUtils.Delay(1000)
  s = OCRObj.GetText(OCROptions)
  Log.Message(s, "", pmNormal, None, Rect)

VBScript

Sub Main
  Set p = Sys.Process("notepad")
  Set w = p.Window("Notepad", "*").Window("Edit")
  Set Rect = w.Picture(0, 0, 200, 25)
  Call Log.Picture(Rect, "Region with text to be recognized.")
  
  Set OCRObj = OCR.CreateObject(Rect)
  Set OCROptions = OCRObj.CreateOptions
  Call p.Window("Notepad", "*").MainMenu.Click("Format|Font...")
  Set w = p.Window("#32770", "Font")
  Set Font = OCROptions.Fonts.Add
  Font.Name = w.Window("ComboBox", "", 1).wText
  Call Font.Sizes.Add(aqConvert.VarToInt(w.Window("ComboBox", "", 3).wText))
  Call w.Window("ComboBox", "", 1).ClickItem("Arial")
  Call w.Window("ComboBox", "", 2).ClickItem("Regular")
  Call w.Window("Button", "OK").ClickButton
  aqUtils.Delay(1000)
  s = OCRObj.GetText(OCROptions)
  Call Log.Message(s, "", pmNormal, Null, Rect)
End Sub

DelphiScript

procedure Main;
var p, w, w1, OCRObj, OCROptions, Font, Rect, s : OleVariant;
begin
  p := Sys.Process('notepad');
  w := p.Window('Notepad', '*').Window('Edit');
  Rect := w.Picture(0, 0, 200, 25);
  Log.Picture(Rect, 'Region with text to be recognized.');
  OCRObj := OCR.CreateObject(Rect);
  OCROptions := OCRObj.CreateOptions;
  p.Window('Notepad', '*').MainMenu.Click('Format|Font...');
  w := p.Window('#32770', 'Font');
  Font := OCROptions.Fonts.Add;
  Font.Name := w.Window('ComboBox', '', 1).wText;
  Font.Sizes.Add(aqConvert.VarToInt(w.Window('ComboBox', '', 3).wText));
  w.Window('ComboBox', '', 1).ClickItem('Arial');
  w.Window('ComboBox', '', 2).ClickItem('Regular');
  w.Window('Button', 'OK').ClickButton;
  aqUtils.Delay(1000);
  s := OCRObj.GetText(OCROptions);
  Log.Message(s, '', pmNormal, nil, Rect);
end;

C++Script, C#Script

function Main()
{
  var p, w, w1, OCRObj, OCROptions, Font, Rect, s;
  p = Sys["Process"]("notepad");
  w = p["Window"]("Notepad", "*")["Window"]("Edit");
  Rect = w["Picture"](0, 0, 200, 25);
  Log["Picture"](Rect, "Region with text to be recognized.");
  OCRObj = OCR["CreateObject"](Rect);
  OCROptions = OCRObj["CreateOptions"]();
  p["Window"]("Notepad", "*")["MainMenu"]["Click"]("Format|Font...");
  w = p["Window"]("#32770", "Font");
  Font = OCROptions["Fonts"]["Add"]();
  Font["Name"] = w["Window"]("ComboBox", "", 1)["wText"];
  Font["Sizes"]["Add"](aqConvert["VarToInt"](w["Window"]("ComboBox", "", 3)["wText"]));
  w["Window"]("ComboBox", "", 1)["ClickItem"]("Arial");
  w["Window"]("ComboBox", "", 2)["ClickItem"]("Regular");
  w["Window"]("Button", "OK")["ClickButton"]();
  aqUtils["Delay"](1000);
  s = OCRObj["GetText"](OCROptions);
  Log["Message"](s, "", pmNormal, null, Rect);
}

See Also

OCROptions Object
OCRObject.FindRectByText
Using Optical Character Recognition - Tips

Highlight search results