Invoking a subroutine when new window is opened

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
jiby
Junior Coder
Posts: 30
Joined: Fri Feb 26, 2010 12:26 pm

Invoking a subroutine when new window is opened

Post by jiby » Wed Dec 08, 2010 7:32 pm

hi,

On XP, I am trying to invoke a procedure whenever a new notepad is opened.

My procedures are as:
---------------------
OnEvent>WINDOW_OPEN,Untitled*,WF_TYPE,DoThis
SRT>DoThis
Ask>yss,res
END>DoThis

Wait>10
---------------------

After running the script, if I open the notepad, I could see no prompt window popping up. Please help me in resolving this.

Thanks in advance.
jiby

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

Post by Marcus Tettmar » Wed Dec 08, 2010 8:07 pm

WF_TYPE should be a numeric value being one of the WF_TYPE values (see the SetFocus help topic for a list of options).

Try this:

Code: Select all

OnEvent>WINDOW_OPEN,Untitled*,1,DoThis

Label>idle_loop
  Wait>0.02
Goto>idle_loop

SRT>DoThis
  Ask>yss,res
END>DoThis
Note that once you open Notepad DoThis will keep firing because you have no provision in here to flag that we've already triggered. Note also the idle_loop. This is to simulate the script doing something otherwise it's just going to end. And waiting 10 seconds won't help as that 10 second wait will hold things up anyway. Hopefully the above will get you going where you want to go.
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
JRL
Automation Wizard
Posts: 3518
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Dec 08, 2010 8:10 pm

I had just typed this up when Marcus posted. It has the flag he mentioned and also another Onevent to close the script when Notepad is closed.

Code: Select all

Let>WF_TYPE=1
OnEvent>WINDOW_OPEN,Untitled*,WF_TYPE,DoThis
Let>DoThisFlag=0

SRT>DoThis
  If>DoThisFlag=0
    Ask>yss,res
    OnEvent>WINDOW_NOTOPEN,Untitled*,WF_TYPE,Quit
    Let>DoThisFlag=1
  EndIf
END>DoThis

SRT>Quit
  Exit>0
END>Quit

Label>Loop
  Wait>0.01
Goto>Loop

jiby
Junior Coder
Posts: 30
Joined: Fri Feb 26, 2010 12:26 pm

Post by jiby » Thu Dec 09, 2010 6:11 pm

Thanks a lot for the information.

Is it possible to trigger "OnEvent" only when any NEW Window is opened. I tried,

OnEvent>WINDOW_OPEN,*,1,DoThis

When 4 windows are already opened before running this script, it is going to infinite loop. I was getting continuous "Confirm" popup window.

--yuv.

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

Post by jpuziano » Thu Dec 09, 2010 7:07 pm

jiby wrote:Is it possible to trigger "OnEvent" only when any NEW Window is opened. I tried,

OnEvent>WINDOW_OPEN,*,1,DoThis

When 4 windows are already opened before running this script, it is going to infinite loop. I was getting continuous "Confirm" popup window.
Yes, I'd expect that. Look at the definition for OnEvent>WINDOW_OPEN in the Help File:
Help File wrote:WINDOW_OPEN
Window_Title
WF_TYPE
Triggers when specified window is open.
Notice it does not say it triggers "when the specified window opens" or "when the specified window first opens"... just... when it is open.

Well, once it opens, it may be open for a long time... that OnEvent is going to keep firing over and over again and will continually keep firing until that window finally is closed.

Sidenote: Personally I think of and treat OnEvent>WINDOW_OPEN more like... "OnState">WINDOW_OPEN... because it keeps firing while the state of that window is "Open". This threw me at first because when I first saw the new OnEvent commands... I expected it would fire only once... when the window actually opened. Nope, that's not the way it works and once I knew that, things made sense again. I just mention this in case you were thinking the same way.

There is no OnEvent trigger you can use to tell you that a new specific window has just been opened though I think that would be a fine idea for a new EventType... maybe call it "WINDOW_JUST_OPENED" to remove any ambiguity? Anyway, we do have OnEvent>WINDOW_NEWACTIVE which "Triggers when the foreground window changes". If the new windows you are trying to trigger on are not visible windows, you're out of luck... but if they are visible, then when they open, it will cause the "active foreground window" to change and this event will get triggered. At that point, if you want to determine if you indeed have a new window open now, you'd have to:

get a list of all currently open windows by:
- window title (no so good, how do you handle multiple windows with the same title)
- window handle (better, each is unique)
- process id (also unique)

For more on this, check out the following post in which I wrote something to track the opening and closing of multiple Notepad windows... perhaps you can use a similar approach.

http://www.mjtnet.com/forum/viewtopic.php?t=5350

Take care
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 - :-)

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