GetText Method

Applies to TestComplete 12.60, last modified on September 17, 2018
The Optical Character Recognition feature provided by the OCR plugin is deprecated. It was removed from TestComplete in version 12.60 and is no longer available for later versions.
Instead, TestComplete version 12.60 introduces the new Optical Character Recognition engine featuring Google Cloud Vision API. To learn more, see Optical Character Recognition.
If you need to use the legacy feature, please contact our Customer Care team.

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