MONITOR NEW WINDOWS
Author SGP
Message No.

http://groups.yahoo.com/group/power-pro/message/9981

File Name Window_ActOn.powerpro
Requirements regex.dll syntax requires PPpro version 3.7 or above, map.dll, file.dll
Description: Monitor windows and run powerpro commands
Notes:

INTRODUCTION:
This script was started as an alternative to the Monitor or Open special lists. My recommendation is to learn to use those lists, as they are very powerful. In rare cases, you may find that this script is more useful or more flexible.

I enjoyed this script especially as a learning tool.

Prior to running the script, you need to specify which window captions it should monitor and which PP commands it should execute when the window captions are "found". Note that the meaning of "found" can be specified as a parameter to this script.

Window captions and PP commands are specified in an external file (multiple files are supported, see below). For instance, specify
*notepad*        %windir%\media\ding.wav
to play a ding when notepad is "found".

There are three ways to use this script:
(1) Run it from the command line or from another script, it will perform its actions once and then exit.
(2) Assign it to the Open special list, it will monitor all new "found" windows.
(3) Assign it to the Monitor special list, it will periodically monitor all "found" windows.

It gives you more flexibility than Open or Monitor, but it's less performant (less snappy) and, admittedly, not all combinations of "found", captions and commands make practical sense. But you should experiment and see what's useful for you. And don't forget that you can use the Open or Monitor lists directly. See some examples following the USAGE section.

USAGE:
powerpro .Window_ActOn( [ <window predicate> ] )
where <window predicate> is an optional STRING and can be one of
   [not] visiblewindow
   [not] activewindow
   [not] anywindow
   [not] win.maxxed
   [not] win.minned
   [not] win.topmost
   [not] win.trayminned
   [not] win.resizable
   [not] win.maxable
   [not] win.minable
   [not] win.toolwindow
when <window predicate> is omitted, "activewindow" is used "not" can be used to invert the predicate value. See powerpro.hlp and plugins/win.txt for definitions of each <window predicate>.  See also the CONFIGURATION section below.

CONFIGURATION FILE:
The config filename must be the same as the script filename with .powerpro/.txt replaced by .cfg.  Both files must be in the same folder. The config file MUST be tab-delimited with NO empty lines. example
        <caption1><tab><actions1>
        <caption2><tab><actions2>
        ...
If you need to use different configuration files just rename this file and its configuration file accordingly. Multiple copies of this script can be used simultaneously, provided that each copy has a unique filename and hence configuration file.

EXAMPLES:
(A) To associate commands to new windows:
create a new command list named
[OnNewWindows]
Name field: *
Command field: .Window_ActOn("visiblewindow")
Start pproconf, go to Command lists > Setup > Special Lists enter "OnNewWindows" in the Open field

In the configuration file Window_ActOn.cfg enter
    *notepad*        %windir%\media\ding.wav

Explanation: Each time a new window is created, OnNewWindows("visiblewindow") is executed, which in turn looks for visible windows whose captions match the captions specified in the .cfg file, *notepad* in this case. For all matches found, their associated set of PP commands are executed, ding.wav in this case. Thus, every time any new window is created, if notepad is still visible the same match will be found, and a ding will play. Admittedly, this example may be of limited value to you. Just experiment.

(B) To associate commands to new windows:
In the above example, change
    .Window_ActOn("visiblewindow")
to
    .Window_ActOn("activewindow")
in the Command field

Explanation: ding.wav will play only when notepad is the active window AND a new window is created. In practical terms, this is typically what happens when notepad is started.

(C) To do something to open windows:
In the above example, change
    .Window_ActOn("activewindow")
to
    .Window_ActOn("anywindow")
in the Command field

and change
    *notepad*        %windir%\media\ding.wav
to
    *notepad*        win close! *notepad*

Then run this script from the command line. It will close all notepad windows, if any.

LIMITATIONS:
PowerPro 3.8 and below can't detect new console windows, hence this script isn't called when a new command processor (cmd.exe, etc.) window opens

local wp = ifelse(arg(1), arg(1), "activewindow")
local c a s
static m = "gm"++(scriptname) ;;global map name

; Window_ActOn.cfg lists the actions to start for each new window
; Actions are stored in map gmWindow_ActOn.
; New entries added to Window_ActOn.cfg won't be recognized until
; the map is destroyed, which forces its reinitialization.
; To refresh the map run the following line:
;   .Window_ActOn@Reset from the command line
; The next time this script is called, the list will be refreshed automatically.
; In all subsequent calls, the list that is in memory will be used to
; improve execution speed.
; Do not add an action to reset the map in the config file, as it will
; most-likely generate a map error.

;debug &(scriptname) started

if (not map.exists (eval(m)) )do
    ;debug &(scriptname): initializing &(m)
    local fp = file.open(scriptfolder++"\"++scriptname++".cfg", "r")
    if(0<fp)do
        do(m++"=map.create(60)","","")
        for (;not file.eof(fp);)
            s = file.ReadString(fp)
            regex.match (s, "([^\t]+)\t+(.*)", "\1", "c")
            regex.match (s, "([^\t]+)\t+(.*)", "\2", "a")
            ;debug &(scriptname): actions for &(c) : &(a)
            if(c)
                map.set(eval(m),c,a)
        endfor
        file.close(fp)
    endif
endif

map.restart(eval(m))
for(1)
    c=map.getnext(eval(m))
    if(map.eof(eval(m)))
        break
    ;win.debug("testing",eval(m),"for", c)
    if( eval(wp++"('""++c++"'")") )
        do(map.get(eval(m),c), "", "")
endfor

quit

; run this to force rereading the configuration file
@Reset
if( map.exists(eval(m)) )
    do(m++"=map.destroy("++m++")","","")

quit