Waiting until area of window refreshes/updates?

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

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

Waiting until area of window refreshes/updates?

Post by RNIB » Wed Apr 22, 2009 12:03 pm

I'm trying to write a macro to automate the process of loading audio files into a program. The problem I'm having with this is in getting the macro to wait until the audio has actually loaded before proceeding with the next step.

The program in question draws a waveform display of the audio file once it's loaded in but the speed it takes to do this depends on the size of the audio file, how far in our out the waveform display is zoomed to and the speed of the computer.

I've tried just putting in long wait commands which kinda works but it's not very efficient or free from errors.

As when an audio file is loaded into this program the waveform display area refreshes to show the waveform, is there a way to get the macro to look at a specified area of the screen to wait for the refresh before it continues?

I've seen and tried things like WaitWindowChanged but this didn't work as only part of the window changes and it's purely graphical i.e. the title bar doesnt change.

gdyvig
Automation Wizard
Posts: 447
Joined: Fri Jun 27, 2008 7:57 pm
Location: Seattle, WA

Post by gdyvig » Wed Apr 22, 2009 12:30 pm

This is what you are looking for:
//Wait up to 60 seconds for loading done image
Let>WSI_TIMEOUT=60
WaitScreenImage>BitmapFile,color_tolerance

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

Post by RNIB » Wed Apr 22, 2009 1:47 pm

Thanks for that, much appreciated.

I'm having some problems with it and I don't really understand the description of this command in the help file which makes finding a solution somewhat tricky! I also don't know if I should be providing any values for the WaitScreenImage command.

Any chance you could explain in lamens terms what it's doing? :oops:

Sorry, I'm a newbie

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

Post by Marcus Tettmar » Wed Apr 22, 2009 2:42 pm

WaitScreenImage takes a bitmap file as a parameter. That bitmap file you will have captured using the built in image capture tool (Editor - Tools/Image Capture Tool. Capture the part of the screen you want to wait for.

This explains it in more detail:
http://www.mjtnet.com/blog/2007/02/20/h ... cognition/

Another possibility is to use WaitScreenText. If there is some text that only appears when the window is ready/has appeared, then you could use WaitScreenText to wait until that text is on the screen. You then know the window is there and ready.
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 » Wed Apr 22, 2009 3:23 pm

Ahh right, I have a better understanding of it now.

Hmm, I've just noticed something else which might be a better 'trigger' but I'm not sure how to capture it.

The program I'm trying to automate basically presents a list on screen with the list giving the names of the audio files. Each of these files names will be unique.

You can navigate up and down the list by using Ctrl+Page Up and Ctrl+Page Down.

Each time you navigate to a different item in the list, it loads the waveform of that file into a window underneath the list. This is where the 'problem' is because it takes a different amount of time to load this waveform display. However ONLY once the waveform has loaded does the file name in the list get highlighted in yellow and this would therefore seem a more effective way of detecting when the waveform has fully loaded. See screen dump below.

Image

Being that the name of the file will be different each time and the position in the list and therefore on screen will be different each time is there any way of getting EasePublisher to wait for the list item to be highlighted before proceeding with the rest of the macro?

gdyvig
Automation Wizard
Posts: 447
Joined: Fri Jun 27, 2008 7:57 pm
Location: Seattle, WA

Is only one line in yellow?

Post by gdyvig » Wed Apr 22, 2009 3:53 pm

If only one line is expected to be yellow, make your needle file bitmap one of the following:
- Yellow portion of just the words "Insert audio"
- Yellow portion of just the extension ".wav"

For either of these NumFound should be 1.


One other suggestion.
You might also see if the WaitReady comand works.
It is supposed to wait for the window to finish painting, does not care what the painting looks like. I think it works by waiting for the window display to be stable for some lenght of time.

Gale

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

Post by RNIB » Thu Apr 23, 2009 3:19 pm

Many thanks for all your help. I've made quite a bit of progress but I'm not sure that I'm using the best/correct commands to do this as I got a lot of what I've done from watching this video http://www.mjtnet.com/demos/irdemo.html and I've never done anything with image recognition before. I also have a couple of problems with the macro as it stands.

This is what I have written so far:

Code: Select all

//Ask how many times to run
Input>NumTimes, Enter Numer of Runs:,10
Let>x=0
Repeat>x
//Switch to EasePublisher
SetFocus>EasePublisher
Wait>1
//Move to end of Waveform
Press Alt
Press End
Release Alt
//Get macro to wait until waveform fully loaded
GetScreenRes>sX,sY
ScreenCapture>0,0,sX,sY,C:\temp\pagenav.bmp
FindImagePos>C:\temp\insertaudio.bmp,C:\temp\pagenav.bmp,0,0,XArr,YArr,NumFound
If>imgs>0
//Open Add Page Dialogue
Press CTRL
Send>1
Release CTRL
Endif
//Enter automatic page number
Wait>1
Press Enter
Wait>1.5
SetFocus>EasePublisher
//Move to next file in list
Press CTRL
Press Page Down
Release CTRL
Until>x=NumTimes
Now the first thing to say is that I don't know if I needed to get the screen resolution it's just that the demo video used it and as different resolutions are used on a number of our PC's I figured it couldn't hurt.

The two problems I have at the moment are:

1. It 'misses' the first item in the list.
I think this is because there is always something selected and therefore highlighted in yellow in EasePublisher. Therefore when the macro first runs instead of adding the page number to that file it now jumps over the first file and continues with the other files in the list.

2. The Loop no longer works.
When I run the macro I get a dialogue asking me how many times I run the macro. It doesnt matter what I enter here, the macro will just continue forever.

Apart from that it does seem to work and is so much faster and reliable than it was before so again, thank you very much for all your help.

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 Apr 23, 2009 3:43 pm

Hi,

The reason your loop doesn't end is that you are not incrementing x. So x never reaches NumTimes. Add this at the end of the loop:

Let>x=x+1

To counter the first problem, what happens if you click away from the list of items? Does it lose focus? If so does the yellow line change colour? If so, then you could have your macro initially focus some other part of the window, so that there is no selected item. Either that or if this is just an issue that occurs on the first iteration you can handle it by making the macro act differently if x=0 (first iteration). I can't tell you what you'd do differently without seeing the process take place. But I'm sure there is something you can do. You either need to accommodate a different scenario on the 1st parse, or have the macro focus the window in such a way that there is no selected line when the loop starts.
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 » Tue May 05, 2009 1:47 pm

Okay I'm utterly confused now!

On earlier posts in this thread I said that once the audio had loaded a line would turn yellow. Unfortunately due to an upgrade in the software I'm automating this is no longer the case. However it now display the words "audio loading..." when it's loading the waveform display so I am now going to use this as the 'trigger'

This is what I currently have:

Code: Select all

Input>NumTimes, Enter Numer of Runs:,10
Let>x=0
Repeat>x
SetFocus>EasePublisher
Wait>1
//Navigate to end of waveform display
Press Alt
Press End
Release Alt
//Wait for audio to load by looking to see if audio loading text is visible or not
GetScreenRes>sX,sY
ScreenCapture>0,0,sX,sY,C:\temp\pagenav.bmp
FindImagePos>C:\temp\loadingaudio.bmp,C:\temp\pagenav.bmp,0,0,XArr,YArr,NumFound
//If audio loading text is not visible add a page tag
If>imgs>0
Press CTRL
Send>1
Release CTRL
Wait>1
Press Enter
Wait>1.5
SetFocus>EasePublisher
Endif
//Move to next item in list
Press CTRL
Press Page Down
Release CTRL
Let>x=x+1
Until>x=NumTimes
When I run this all that happens is that the macro jumps me to the end of the first waveform, then moves down to the next item in the list, jumps to the end of that and so on, i.e. at no point does it add the page tag BUT it does seem to wait for the audio loading text to dissapear before it moves on with the next stage. That said, if I change If>imgs>1 it makes no difference.

Basically what I'm trying to do is this, and the important thing to remember is that EasePublisher loads the waveform display at two different times. At Stage 1 when you first select the item in the list and then again at Stage 2 when you jump to the end of the waveform display for that list item:


1. I run the macro and it asks me how many times I want it to run

You always have to have something selected in EasePublisher, it's impossible to click away and have nothing selected. Therefore because the user will have selected the first item manually it will have loaded the waveform before the macro is run. Therefore there is no need to have the macro wait for the audio to load at Stage 1 on the first loop of the macro

2. It jumps to the end of the waveform in the currently selected item
3. It then needs to wait for the audio to load (stage 2)
4. Then it adds a page tag
5. Then it moves to the next item in the list
6. Then it waits for the audio to load (stage 1)
7. Then it goes back to the start

I've tried so many variations now that and being the newbie I am, I've got myself utterly confused and am not sure where it's going wrong or more importantly how to correct it!

gdyvig
Automation Wizard
Posts: 447
Joined: Fri Jun 27, 2008 7:57 pm
Location: Seattle, WA

Imgs vs NumFound

Post by gdyvig » Tue May 05, 2009 2:27 pm

Looks like a copy/paste error from your original script.

Your FindImagePos command stores the image count in the variable NumFound, then you check for the image count in the variable imgs.

If you like imgs better, change NumFound to imgs.


Gale

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

Re: Imgs vs NumFound

Post by RNIB » Tue May 05, 2009 3:59 pm

gdyvig wrote:Looks like a copy/paste error from your original script.

Your FindImagePos command stores the image count in the variable NumFound, then you check for the image count in the variable imgs.

If you like imgs better, change NumFound to imgs.


Gale
Ahh right yes. I've ammended it slightly now to:

Code: Select all

//Ask how many times to run
Input>NumTimes, How Many Pages Do You Want To Add:,10
Let>x=0
Repeat>x
//Switch to EasePublisher
SetFocus>EasePublisher
Wait>1
//Move to end of Waveform
Press Alt
Press End
Release Alt
//Get macro to wait until waveform fully loaded
GetScreenRes>sX,sY
ScreenCapture>0,0,sX,sY,C:\temp\pagenav.bmp
FindImagePos>C:\temp\loadingaudio.bmp,C:\temp\pagenav.bmp,0,0,XArr,YArr,imgs
If>imgs>0
//Open Add Page Dialogue
Press CTRL
Send>1
Release CTRL
Endif
//Enter automatic page number
Wait>1
Press Enter
Wait>1.5
SetFocus>EasePublisher
//Move to next file in list
Press CTRL
Press Page Down
Release CTRL
//Get macro to wait until waveform fully loaded
GetScreenRes>sX,sY
ScreenCapture>0,0,sX,sY,C:\temp\pagenav.bmp
FindImagePos>C:\temp\loadingaudio.bmp,C:\temp\pagenav.bmp,0,0,XArr,YArr,imgs
If>imgs>0
Let>x=x+1
Endif
Until>x=NumTimes
Trouble is, it's still not working but I think I now understand why.

Code: Select all

//Get macro to wait until waveform fully loaded
GetScreenRes>sX,sY
ScreenCapture>0,0,sX,sY,C:\temp\pagenav.bmp
FindImagePos>C:\temp\loadingaudio.bmp,C:\temp\pagenav.bmp,0,0,XArr,YArr,imgs
If>imgs>0
If I've understood this correctly then I'm asking the macro to look for the content of the loadingaudio.bmp within the pagenav.bmp file and if it sees it to then do something.

Actually what I need it to do is to look for the content of the loadingaudio.bmp within the pagenav.bmp and when it DOESN'T see it, to then do something.

Again if I've understood this correctly the line If>imgs>0 translates into English as being If the count from the variable imgs is greater than 0 and so if I changed that line to If>imgs<1 (if the count from the variable imgs is less than 1) than that should work shouldn't it? Trouble is that it doesnt so either I've not written this correctly or something else is wrong :oops:

gdyvig
Automation Wizard
Posts: 447
Joined: Fri Jun 27, 2008 7:57 pm
Location: Seattle, WA

Use a Repeat Loop

Post by gdyvig » Tue May 05, 2009 4:24 pm

Usually you don't need to do the ScreenCapture. Just specify SCREEN.

You want to wait for an image to disappear. There is no Wait command to do that. After you have found the image, add a loop to wait for it to disappear:

Code: Select all

Let>maxwait=10
Let>counter=0
Repeat>imgs
 Let>counter=counter+1
 if>counter=maxwait
   exit>
 endif
 wait>1
 FindImagePos>SCREEN,C:\temp\pagenav.bmp,0,0,XArr,YArr,imgs
Until>imgs=0

The above code is untested.
Also, if imgs<0 your FindImagePos command contains a syntax error.

Hope this helps,

Gale

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

Post by RNIB » Wed May 06, 2009 11:01 am

Gale, thank you so much!!! I really appreciate all of your help as I was/am really out of my depth with this as I've pretty much only ever recorded Key Strokes with Macro Scheduler before.

I've made a couple of ammendments to your suggested fix and so the whole thing looks like this now:
//Ask how many times to run
Input>NumTimes, How Many Pages Do You Want To Add:,10
Let>x=0
Repeat>x
//Switch to EasePublisher
SetFocus>EasePublisher
Wait>1
//Move to end of Waveform
Press Alt
Press End
Release Alt
//Get macro to wait until waveform fully loaded
GetScreenRes>sX,sY
FindImagePos>C:\temp\loadingaudio.bmp,SCREEN,0,0,XArr,YArr,imgs
Let>maxwait=10
Let>counter=0
Repeat>imgs
Let>counter=counter+1
if>counter=maxwait
exit>
endif
wait>1
FindImagePos>C:\temp\loadingaudio.bmp,SCREEN,0,0,XArr,YArr,imgs
Until>imgs=0
//Open Add Page Dialogue
Press CTRL
Send>1
Release CTRL
Endif
//Enter automatic page number
Wait>1
Press Enter
Wait>1.5
SetFocus>EasePublisher
//Move to next file in list
Press CTRL
Press Page Down
Release CTRL
//Get macro to wait until waveform fully loaded
GetScreenRes>sX,sY
FindImagePos>C:\temp\loadingaudio.bmp,SCREEN,0,0,XArr,YArr,imgs
Let>maxwait=10
Let>counter=0
Repeat>imgs
Let>counter=counter+1
if>counter=maxwait
exit>
endif
wait>1
FindImagePos>C:\temp\loadingaudio.bmp,SCREEN,0,0,XArr,YArr,imgs
Until>imgs=0
Let>x=x+1
Until>x=NumTimes
I've been testing this and so far it seems to work perfectly but before I role this out to everyone to use, does it look correct to you?

Again, thank you so much for your help!!

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

Post by RNIB » Wed May 06, 2009 1:47 pm

On doing some extensive tests I've found that it can 'fall over' i.e. insert several page tags under one item in the list instead of just one under each item in the list but that this happens at random.

I suspect that this may be down to the problems we are experiencing with our server/network where it can run slow causing files to take longer to load.

I've increased the length of all the Wait commands from 1 to 2 seconds which seems to have fixed things....so far. Is there a better way though i.e. to make sure that the macro doesnt do anything until the audio has loaded regardless of how long that might be. Basically I would like the macro to run as fast as the host computer & network/server will allow without having to slow things down too much.

The macro now looks like this:

Code: Select all

//Ask how many times to run
Input>NumTimes, How Many Pages Do You Want To Add:,10
Let>x=0
Repeat>x
//Switch to EasePublisher
SetFocus>EasePublisher
Wait>2
//Move to end of Waveform
Press Alt
Press End
Release Alt
//Get macro to wait until waveform fully loaded
GetScreenRes>sX,sY
FindImagePos>C:\temp\loadingaudio.bmp,SCREEN,0,0,XArr,YArr,imgs
Let>maxwait=10
Let>counter=0
Repeat>imgs
Let>counter=counter+1
if>counter=maxwait
exit>
endif
wait>2
FindImagePos>C:\temp\loadingaudio.bmp,SCREEN,0,0,XArr,YArr,imgs
Until>imgs=0
//Open Add Page Dialogue
Press CTRL
Send>1
Release CTRL
Endif
//Enter automatic page number
Wait>2
Press Enter
GetScreenRes>sX,sY
FindImagePos>C:\temp\loadingaudio.bmp,SCREEN,0,0,XArr,YArr,imgs
Let>maxwait=10
Let>counter=0
Repeat>imgs
Let>counter=counter+1
if>counter=maxwait
exit>
endif
wait>2
FindImagePos>C:\temp\loadingaudio.bmp,SCREEN,0,0,XArr,YArr,imgs
Until>imgs=0
SetFocus>EasePublisher
//Move to next file in list
Press CTRL
Press Page Down
Release CTRL
//Get macro to wait until waveform fully loaded
GetScreenRes>sX,sY
FindImagePos>C:\temp\loadingaudio.bmp,SCREEN,0,0,XArr,YArr,imgs
Let>maxwait=10
Let>counter=0
Repeat>imgs
Let>counter=counter+1
if>counter=maxwait
exit>
endif
wait>2
FindImagePos>C:\temp\loadingaudio.bmp,SCREEN,0,0,XArr,YArr,imgs
Until>imgs=0
Let>x=x+1
Until>x=NumTimes
I've also tried increasing the maxwait to 60 but I'm still finding that it gets things wrong quiet frequently now.

gdyvig
Automation Wizard
Posts: 447
Joined: Fri Jun 27, 2008 7:57 pm
Location: Seattle, WA

Use WaitWindowOpen before SetFocus

Post by gdyvig » Wed May 06, 2009 2:43 pm

Hi,

Glad it is working for you now!

Here is a suggestion to make it run faster when your servers are up to it:

In several places you have a hard Wait followed by a SetFocus.
Replace the Wait> with WaitWindowOpen.

In fact you should always precede a SetFocus with WaitWindowOpen.

There are a couple other places the script where it looks like the window title might change. Add a WaitWindowOpen and a SetFocus there tool.

At the beginning of our script add:
Let>WW_TIMEOUT=30

Now all of your WaitWindowOpens will automaticially adust to server responsiveness in a range between 0 and 30 seconds. Setting this value high will not make the script run slower if the servers are running fast.

You may want to add some error handling. What if the window never opens in the time you deem reasonable.

Add this after each WaitWindowOpen:

Code: Select all

if>WW_RESULT=FALSE
  //Decide how you want to report errors here.
  exit>
endif
On the parts where you are waiting for screen images to disappear, the script will be a little more responsive if you poll more frequently. Make thiese changes:
//Let>maxwait=10
Let>maxwait=100
...
...
//Let>wait=1
Let>wait=0.1

This means the script will still take 10 seconds to wait for an image to disappear plus a fraction of a second to do the FindImagePos commands. Increase maxwait as needed.


On script organization you could replace repetive sequences of code with SRT's, but that is not essential.


Let us know if these optimizations work.


Gale

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