Run Program correlated to WaitWindowOpen

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
chihuahualand
Junior Coder
Posts: 35
Joined: Thu Jan 12, 2006 9:20 pm
Contact:

Run Program correlated to WaitWindowOpen

Post by chihuahualand » Wed Jul 15, 2009 3:58 am

Is there a way to correlate Run Program to WaitWindowOpen when you don't know what the caption will be?

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

Post by JRL » Wed Jul 15, 2009 3:02 pm

I hardly ever say no. But logic would dictate that since a program can spawn a window with any title. The operating system can't possibly detect the title of a window until that window has opened. If the window is already open then WaitWindowOpen> will always evaluate to "true".

I could see a potential for using GetWindowList> and GetWindowProcess> to tie an unknown window name to the spawning process.

Something like this: This does not check for the process already being open.

Code: Select all

Let>ProcessName=NotePad.exe
Let>msg_width=500

Run>%ProcessName%
Let>rp_wait=1
Let>rp_windowmode=0
Run>cmd /c tasklist.exe /FI "imagename eq %ProcessName%">%Temp_Dir%processlist.txt
ReadFile>%Temp_Dir%processlist.txt,ProcessList


GetWindowList>WinList
Separate>WinList,%CRLF%,Window
Let>kk=0
Repeat>kk
  Add>kk,1
  Let>value=Window_%kk%
  GetWindowProcess>value,ProcID,ProcName
  If>%ProcID%<100,Skip
  Separate>ProcessList,ProcID,var
  If>var_count>1
    MDL>Process ID = %ProcID%%crlf%Process Name = %ProcName%%crlf%Window Name = %value%
    Exit>0
  EndIf
  Label>Skip
Until>kk,Window_Count

MDL>Process not found
Last edited by JRL on Wed Jul 15, 2009 3:41 pm, edited 1 time in total.

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

Try WaitWindowChanged and GetActiveWindow

Post by gdyvig » Wed Jul 15, 2009 3:05 pm

Hi chihuahualand,

You could use WaitWindowChanged to tell you when a new window opened.

Then use GetActiveWindow to get the title or handle of the window.


Gale

chihuahualand
Junior Coder
Posts: 35
Joined: Thu Jan 12, 2006 9:20 pm
Contact:

Post by chihuahualand » Thu Jul 16, 2009 12:00 am

I was thinking that this might be complicated and reasonably un-bulletproof or perhaps just more cumbersome than practical.

thank you for your insight and direction.

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

Post by JRL » Thu Jul 16, 2009 4:35 am

I was thinking that this might be complicated and reasonably un-bulletproof or perhaps just more cumbersome than practical.
I don't think that's the case. Gale's solution is robust and very easy to implement.

chihuahualand
Junior Coder
Posts: 35
Joined: Thu Jan 12, 2006 9:20 pm
Contact:

Post by chihuahualand » Thu Jul 16, 2009 1:30 pm

JRL;
I agree, Gale's solution is robust and very easy to implement.
no slight was intended...

but unless I'm mistaken, the window can't be guaranteed to be that of the Run Program... It's an assumption, albeit a good one, it's still an assumption and on a slow or heavily loaded machine the issue can become exacerbated.

and this not a complaint just whining. possibly simply thinking out loud.

This script is for KB updates to MS Office XP. There's about 30 of these silly things. While similar, there are slight variations... and I'm wishing for something that is totally FU-proof. I have a workaround with creating a list of programs and the respective captions. It's just clunky.

The original post was to glean innovative and clever ideas from innovative and clever folks. Yours was one, the kind of thing I was looking for. My reply was uncalled for... It was exasperation after a long day. I apologize.

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

Absolutely foolproof?

Post by gdyvig » Thu Jul 16, 2009 3:13 pm

I don't think it is possible to be absolutely certain the next window to display was started by your application without knowing something about what you expect. You could do additional checking of the window that displayed to determine if it is yours.

There are some posts on an IsProcessRunning vbscript function that may help. Normally the window will open with a known owner property. There could be some exceptions. The difficult part will be distinquishing various error windows your run command triggered with different owners from totally unrelated and unsolicated windows that may display at any time.

Gale
Last edited by gdyvig on Fri Jul 17, 2009 3:39 am, edited 1 time in total.

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Thu Jul 16, 2009 5:19 pm

You might want to look at some of the tools in SysInternals. It may be possible to create a log of window openings and parse out the info that you need, or use it to crate your own log of what is open/closed.

Home page is now: http://technet.microsoft.com/en-us/sysi ... fault.aspx since it was acquired by Microsoft.
Forum page is here: http://forum.sysinternals.com/ Someone there can probably give you the correct command line(s) to include your Macro Scheduler script.

I cannot suggest a specific tool right now but Process Monitor and PS Tools are probably the most likely candidates. The whole Suite is worth having in your toolbox (FREE).
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

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

Post by JRL » Thu Jul 16, 2009 5:25 pm

I think, using process ID, that its possible to be certain that a currently active window was or was not spawned by a specified process. I rewrote what I posted above to make it faster and easier to use. Place the WFUW subroutine in your script then call it with two parameters, the name of the process whose windows you want to detect and a timeout value. If the timeout value is "0' it will wait forever or until a matching window becomes the active window.

As posted, the routine will wait for a notepad window to become the currently active window. When a notepad window becomes the current active window a message box will pop up.

If you start this script, then open notepad the message box will pop up right way.

If you first open notepad, put the notepad screen in the background, then start the script. The message box will not pop up until the notepad window becomes the current active window.

If multiples of notepad are open, only the last window opened will trigger the subroutine.

Code: Select all

Let>msg_width=500

//WFUW Usage:
//GoSub>WFUW,Process Name (as Shown in Task Manager),Wait Timeout
//The descriptive name for this sub is WaitForUnknownWindowFromSpecifiedProcess

GoSub>WFUW,notepad.exe,0

If>%WFUW_Result%=WaitForUnknownWindowFromSpecifiedProcess Timed Out
  MDL>**TIMED OUT**%CRLF%%WFUW_Result%%CRLF%Process ID = %pid%%crlf%Window ProcID = %ProcID%%CRLF%Current Active Window = %WinTitle%%crlf%Total time = %TotalTime%
Else
  MDL>**FOUND**%CRLF%%WFUW_Result%%CRLF%Process ID = %pid%%crlf%Window ProcID = %ProcID%%CRLF%Current Active Window = %WinTitle%%crlf%Total time = %TotalTime%
EndIf

SRT>WFUW
  //Error checking - make sure there are parameters
  Assigned>WFUW_var_1,WFUW_answer
  If>WFUW_answer=FALSE,NoParameters
  If>%WFUW_var_1%=,NoParameters
  Assigned>WFUW_var_2,WFUW_answer
  If>WFUW_answer=TRUE
    //If timeout is set to zero or nothing then set timeout to large number (basically no timeout)
    If>{(%WFUW_var_2%=0)or(%WFUW_var_2%="")}
      Let>WFUW_var_2=99999999999999999999999999999999999999
    EndIf
  Else
    Let>WFUW_var_2=99999999999999999999999999999999999999
  EndIf

    //Marcus provided VBScript to get Process ID http://www.mjtnet.com/usergroup/viewtopic.php?t=5476
    VBSTART
      Function GetPID(pgm)
        set wmi = getobject("winmgmts:")
        sQuery = "select * from win32_process " & "where name='" & pgm & "'"
        set processes = wmi.execquery(sQuery)
        for each process in processes
          getPID = process.ProcessID
        next
      End Function
    VBEND

    //Start timeout timer
    VBEval>timer,StartTime

  //Working Loop Starts here//
  Label>WFUW_Start
  //Aquire the current window title
  GetActiveWindow>WinTitle,WinX,WinY
  //Get Process ID of named process
  VBEval>GetPID("%WFUW_var_1%"),pid
  //Get Process ID from current active window
  GetWindowProcess>WinTitle,ProcID,ProcName
    //Check for timeout
    VBEval>timer-%StartTime%,TotalTime
    If>%TotalTime%>%WFUW_var_2%
      Let>WFUW_Result=WaitForUnknownWindowFromSpecifiedProcess Timed Out
      Goto>WFUW_End
    EndIf
  //Compare Process ID of specified process and ID of current active window
  If>%pid%=%ProcID%
    Let>WFUW_Result=WaitForUnknownWindowFromSpecifiedProcess found
    Goto>WFUW_End
  EndIf
  Wait>0.01
  Goto>WFUW_Start
  //Working Loop Ends here//

  Label>WFUW_End
  Let>WFUW_var_1=
  Let>WFUW_var_2=
  Label>NoParameters
END>WFUW

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

GetWindowProcess

Post by gdyvig » Fri Jul 17, 2009 4:08 am

I found another simpler command:

GetWindowProcess>window_title,process_id,process_name

You can compare process_name with the executable filename - they should match.


Gale
-----------------------------------------------------------------------
Oops, as JRL noted in the next post, he is already using this command. I was focusing on the vbscript portion and did not notice.
Last edited by gdyvig on Fri Jul 17, 2009 5:13 am, edited 1 time in total.

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

Post by JRL » Fri Jul 17, 2009 5:06 am

GetWindowProcess>window_title,process_id,process_name
That's half of what makes the above posted subroutine work. See line 52. The problem with just matching the window name with the process name occurs when you have the same process running more than once. You really need to tie the window title to the Process ID to know for sure whether a given process spawned a given window.

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