Retrieving text using a control handle

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
Athol
Newbie
Posts: 2
Joined: Sat Dec 05, 2009 12:27 pm

Retrieving text using a control handle

Post by Athol » Sat Dec 05, 2009 12:40 pm

Hi

I am trying to retrieve text associated with a control using the handle of that control. In the code below, res1 returns some valid text. However, res2 returns the value of ##NOSUCHOBJECT## instead of the same text as returned by res1. Any clues as to what I am doing wrong?

Let>WIN_USEHANDLE=0
GetControlText>XYZ,TCheckBox,1,res1

Let>WIN_USEHANDLE=1
//TCheckBox instance 1 has the focus
GetFocusedObject>hwnd
GetControlText>hwnd,TCheckBox,1,res2

Regards
Athol

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Sat Dec 05, 2009 11:50 pm

I'm not aware of a Macro Scheduler function GetFocusedObject>. That would be the problem in the script you posted.

There might be a way to use LibFunc> and Windows API functions to get a dialog object handle but I'm not aware of an exact function to use.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Sun Dec 06, 2009 6:50 pm

GetFocusedObject is an undocumented function provided for a specific client, so I'm guessing Athol must be from that organisation.

GetControlText requires a window name and a class name of a child object within that window. The window name must be a parent window. What you're doing is passing in the actual object handle instead of its parent window handle. Hence the difference.

If you have the handle from GetFocusedObject and want to get that object's text use the API function GetWindowText via LibFunc to retrieve the text given the handle. E.g.

Code: Select all

GetFocusedObject>hwnd
LibFunc>user32,GetWindowTextA,gwaRes,hwnd,str,255
MidStr>gwaRes_2,1,gwaRes,theText
MessageModal>theText
That will return the caption text of the focused object, depending on the object in question and whether or not it published a caption property (if you get nothing back it is down to the control and not me).

Now that this has been outed, for everyone else's benefit, GetFocusedObject simply returns the handle of whatever object currently has the focus. I was planning to blog it at some point as a "bonus" function. It was being held over for official release in the next version. At present it is not included in the docs or the code builder, but if you type it into the editor you'll see it is recognised by the syntax highlighter and it also works in the current version! Enjoy.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Mon Dec 07, 2009 1:14 am

mtettmar wrote:GetFocusedObject is an undocumented function...
I was planning to blog it at some point as a "bonus" function. It was being held over for official release in the next version. At present it is not included in the docs or the code builder, but if you type it into the editor you'll see it is recognised by the syntax highlighter and it also works in the current version! Enjoy.
Looking forward to the blog post for examples of usage... and thanks Marcus for this early Christmas bonus. :D
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

Thien
Newbie
Posts: 1
Joined: Wed May 04, 2011 7:02 pm
Location: London

GetControlText mismatch

Post by Thien » Wed May 04, 2011 7:17 pm

Hello,

Can someone tell me if the function GetControlText works in virtual environment, such as remote desktops?

I am using the function to get the text within a ThunderRT6TextBox in a VB6-based form.

Code snippet:

Label>Check_Retry
WIN_USEHANDLE=1
GetControlText>HWND_Form,ThunderRT6TextBox,9,controlTxt

If>controlTxt=##NOSUCHOBJECT##,Check_Retry


The macro worked well before in live environment but seems to go into an eternal loop in the virtual environment during testing due to GetControlText always returning ##NOSUCHOBJECT## despite the class and instance number being correct.

Any suggestions appreciated. :?

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts