Detect if data is in a field

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
RNIB
Macro Veteran
Posts: 198
Joined: Thu Jan 10, 2008 10:25 am
Location: London, UK

Detect if data is in a field

Post by RNIB » Thu Jan 06, 2011 11:10 am

I'm trying to automate the process of copying certain data from certain fields in a database and pasting it into another program and this will mean merging the data of two fields for some records. The database in question records information about books and there are two fields for the title of the book.

Field 1 will be empty if the book doesnt begin with the words A, An or The
Field 2 contains the rest of the title of the book

So for "A Tale of Two Cities" Field 1 will contain "A" and Field 2 contains "Tale of Two Cities" whilst for "Watership Down" Field one will be empty and Field 2 will contain "Watership Down".

Due to not having rights to the SQL database itself the only way I can copy data from the database is to navigate to each field and Ctrl+C. However the program I'm then pasting this data into requires it all to be pasted into the same field and this presents the problem.

For "A Tale of Two Cities" this will require me to copy Field 1, paste it into the field in the new program, Add a space, go back to Field 2 and copy it's contents before going back to the new program, and pasting in the rest of the title after the space that was added.

For "Watership Down" though Field 1 will be empty and so nothing to copy but if the macro still tries to copy it then it will paste into the new program whatever was originally in the paste buffer. Also there doesn't need to be a space inserted before the contents of field 2 are entered.

So, how can I get MacroScheduler to detect if there is any data present in Field 1 and if there is to go to a subroutine to copy the required data inserting the space but if Field 1 is blank to go to another subroutine to jump to field 2 and simply copy the content?

RNIB
Macro Veteran
Posts: 198
Joined: Thu Jan 10, 2008 10:25 am
Location: London, UK

Post by RNIB » Thu Jan 06, 2011 12:42 pm

I've been trying to get it working using image recognition but I'm clearly doing something wrong as it's not working properly.

I've taken a needle image of the empty title field and am trying to get the macro to find it on screen. This is the code I'm using at present:
//navigates to dc:title page this is the program I'm pasting into
Press Down
Wait>1
Press Enter
WaitWindowOpen>Edit meta "dc:title"
Press Shift
Press End
Release Shift
Wait>1
Press Del
SetFocus>Recording Management System - [View/Amend Title] - This is the program I'm trying to copy from
WaitWindowOpen>Recording Management System - [View/Amend Title]
GetScreenRes>sX,sY
FindImagePos>C:\Documents and Settings\ojohnson\Desktop\title.bmp,SCREEN,0,0,XArr,,NoThe
If>NoThe=0
//This copies the content of field 1 if it isn't empty
Press Ctrl
Send>c
Release Ctrl
SetFocus>Edit meta "dc:title"
WaitWindowOpen>Edit meta "dc:title"
Press Ctrl
Send>v
Release Ctrl
Wait>1
Send>
Wait>1
SetFocus>Recording Management System - [View/Amend Title]
WaitWindowOpen>Recording Management System - [View/Amend Title]
//Navigates to Field 2
Press Tab
Wait>1
Press Shift
Press End
Wait>1
Press Ctrl
Send>c
Release Ctrl
SetFocus>Edit meta "dc:title"
WaitWindowOpen>Edit meta "dc:title"
Press Ctrl
Send>v
Release Ctrl
Wait>1

ELSE
//This navigates to Field 2 if Field 1 is empty
Press Tab
Wait>1
Press Shift
Press End
Wait>1
Press Ctrl
Send>c
Release Ctrl
SetFocus>Edit meta "dc:title"
WaitWindowOpen>Edit meta "dc:title"
Press Ctrl
Send>v
Release Ctrl
Wait>1

ENDIF
//This closes the dc:title field window
Press Enter
If the book is "A Tale of Two Cities" then this all works (except for some reason it doesnt close the dc:title field window and the macro hangs at this point. However if the book is "Watership Down" then it still pasts in whatever was in the paste buffer before jumping to field 2 and pasting that in. I think therefore that it isn't ever finding the needle image but I don't understand why?

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

Post by Marcus Tettmar » Thu Jan 06, 2011 1:17 pm

You seem to have the Y array result variable missing from your FindImagePos line.

That needs fixing though it shouldn't stop it finding the image in itself

Was the needle captured with the built-in capture tool? If not, capture it again with Macro Scheduler's own capture tool.

Are you calling FindImagePos too early? I note you call it immediately after the window has opened. Is the field even on the screen yet? Don't forget Macro Scheduler often runs way faster than human eye can see. So between your WaitWindowOpen and your FindImagePos the object you're looking for may not exist yet - may not be rendered fully yet.

Have you read this:
http://www.mjtnet.com/blog/2009/02/13/i ... -mistakes/
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

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

RNIB
Macro Veteran
Posts: 198
Joined: Thu Jan 10, 2008 10:25 am
Location: London, UK

Post by RNIB » Thu Jan 06, 2011 3:16 pm

mtettmar wrote:You seem to have the Y array result variable missing from your FindImagePos line.

That needs fixing though it shouldn't stop it finding the image in itself

Was the needle captured with the built-in capture tool? If not, capture it again with Macro Scheduler's own capture tool.

Are you calling FindImagePos too early? I note you call it immediately after the window has opened. Is the field even on the screen yet? Don't forget Macro Scheduler often runs way faster than human eye can see. So between your WaitWindowOpen and your FindImagePos the object you're looking for may not exist yet - may not be rendered fully yet.

Have you read this:
http://www.mjtnet.com/blog/2009/02/13/i ... -mistakes/
Aha! Yes you were right, it was running faster than the screen was being drawn and so it missed the bitmap. I've put in a WaitWindowOpen and a further Wait command and now it is working correctly in part. It now does correctly copy and paste the required data from the two fields and merges them into the single field regardless of what the book is called but for some reason it then hangs when I ask it to simply close the window it pasted into which is simply done by pressing Enter. I'll have another look at the code as I must be doing something wrong.

As for the Y array, I have to hold my hand up and say that I don't understand this bit. I've only ever done one bit of image recognition before and that was with a lot of help from this forum. As I'm only needing to know if the needle image is found on screen or not, do I actually need to use the x & y arrays seeing as the co-ordinates aren't of any 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 » Thu Jan 06, 2011 3:28 pm

No, if you only care whether or not a match was found you don't need to USE the x and y arrays. BUT, not specifying a variable for the Y array could cause confusion later as it will end up creating a variable with an empty name (because you've specified an empty string). Just put YArr in there and forget about it.

But if you want to understand what XArr and YArr do let me try and explain. If not, stop reading.

In most cases we only care about ONE match. But it's entirely possible that FindImagePos could find MORE THAN ONE match. And this is in fact sometimes desirable - you might want to use it to find all occurrences of something. So how do you return the position(s) of more than one thing? We can't just use an ordinary variable.

So we use arrays. An array is just a list. If we therefore specify XArr and YArr as our array variables to hold the X and Y positions of each match we would get each X,Y position in:

XArr_0,YArr_0
XArr_1,YArr_1
...
XArr_n,YArr_n

Note that the array index is zero based - it starts at position 0.

If you only ever expect to find one match then NumFound would be 1 and the matching position would be in coords XArr_0,YArr_0

If there were two matches, NumFound would be 2 and the positions of each would be in XArr_0,YArr_0 ; XArr_1,YArr_1

Hope that makes sense.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

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

RNIB
Macro Veteran
Posts: 198
Joined: Thu Jan 10, 2008 10:25 am
Location: London, UK

Post by RNIB » Thu Jan 06, 2011 3:38 pm

Ahh Cheers Marcus, that makes sense and I can see how that would be useful and it's given me some ideas for another macro.

I've changed the code to this now:
//opens meta data window
SetFocus>EasePublisher
WaitWindowOpen>EasePublisher
Press CTRL
Send>m
Release CTRL
Wait>1

//navigates to dc:title page
Press Down
Wait>1
Press Enter
WaitWindowOpen>Edit meta "dc:title"
Press Shift
Press End
Release Shift
Wait>1
Press Del
SetFocus>Recording Management System - [View/Amend Title]
WaitWindowOpen>Recording Management System - [View/Amend Title]
Wait>2
FindImagePos>C:\Documents and Settings\ojohnson\Desktop\title.bmp,SCREEN,0,0,XArr,YArr,NoThe
If>NoThe=0
Press Ctrl
Send>c
Release Ctrl
SetFocus>Edit meta "dc:title"
WaitWindowOpen>Edit meta "dc:title"
Press Ctrl
Send>v
Release Ctrl
Wait>1
Send>
Wait>1
SetFocus>Recording Management System - [View/Amend Title]
WaitWindowOpen>Recording Management System - [View/Amend Title]
Press Tab
Wait>1
Press Shift
Press End
Wait>1
Press Ctrl
Send>c
Release Ctrl
SetFocus>Edit meta "dc:title"
WaitWindowOpen>Edit meta "dc:title"
Press Ctrl
Send>v
Release Ctrl
Wait>1



Else
Press Tab
Wait>1
Press Shift
Press End
Wait>1
Press Ctrl
Send>c
Release Ctrl
SetFocus>Edit meta "dc:title"
WaitWindowOpen>Edit meta "dc:title"
Press Ctrl
Send>v
Release Ctrl
Wait>1

Endif
This all works correctly. However if I run this macro then the correct data is pasted into a the relevant field via an open window. When the macro ends I can then simply press the Enter key and that will close the window because the OK button is currently highlighted. What's confusing the hell out of me though is that if I add after the Endif, Press Enter, it doesnt work and I hear the Windows Default Beep sound :?: :?: Essentially the macro then just won't continue with the rest and I have to stop the macro from running.

I can't see anything wrong, any ideas?

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

Post by Marcus Tettmar » Thu Jan 06, 2011 3:42 pm

That sounds like the enter is landing somewhere other than you expect it to. In other words the wrong thing is focused.

Of course I can only guess as I can't debug it here.

But usually a beep occurs when you try to interact with a form which has a modal dialog in front of it for example. Or you try to interact with something that is disabled. I suspect you don't have the right thing focused and that enter is not going where you want it to.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

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

adroege
Automation Wizard
Posts: 438
Joined: Tue Dec 07, 2004 7:39 pm

Post by adroege » Thu Jan 06, 2011 10:24 pm

I think the problem is much too simple to use
image recognition. I copy values all the time from
database grids on screen and make decisions based
upon if a value copied using Ctrl-C is empty or not.

Code: Select all

  //Put a known value in the clipboard first
  PutClipBoard>%SPACE%
  Press Ctrl
  Send>c
  Release Ctrl
  Wait>0.5
  GetClipBoard>V_COUNTRY
  
  
  If>V_COUNTRY=%SPACE%
     //Nothing was copied from Ctrl-C
     Trim>V_COUNTRY,V_COUNTRY
  Else
     //Do something else
     Trim>V_COUNTRY,V_COUNTRY
  Endif

RNIB
Macro Veteran
Posts: 198
Joined: Thu Jan 10, 2008 10:25 am
Location: London, UK

Post by RNIB » Tue Jan 11, 2011 10:58 am

adroege wrote:I think the problem is much too simple to use
image recognition. I copy values all the time from
database grids on screen and make decisions based
upon if a value copied using Ctrl-C is empty or not.

Code: Select all

  //Put a known value in the clipboard first
  PutClipBoard>%SPACE%
  Press Ctrl
  Send>c
  Release Ctrl
  Wait>0.5
  GetClipBoard>V_COUNTRY
  
  
  If>V_COUNTRY=%SPACE%
     //Nothing was copied from Ctrl-C
     Trim>V_COUNTRY,V_COUNTRY
  Else
     //Do something else
     Trim>V_COUNTRY,V_COUNTRY
  Endif
Sorry I've been away for a few days. That's a much better and more reliable way of doing this. Thank you very much, it works like a charm

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