jpuziano wanted to count how many times Notepad was opened and closed.
He found that the OnEvent triggers normally fire all the time a Window is open, but wanted a trigger that fired when the Window was actually opened and was actually closed. This triggered a number of discussions.
I made the suggestion to make a small script to update an INI file and have that macro triggered by the Macro Scheduler built in Trigger Events. That explanation by me was not clear, so I made the time to do it myself. Here are two scripts to do the work:
CountProgramOpenings.scp
Code: Select all
/*
Purpose:
To count the number of times a program is opened.
Triggered in Macro Scheduler Event: WindowOpen, enter name of window
Designed with
XP-PRO SP2
Macro Scheduler 11.1.05
Tested with "Calculator"
=============================================================
*/
// Open Program counter
IfFileExists>ProgramCounter.ini,UpdateINI
WriteLn>ProgramCounter.ini,vResult,[Counters]
EditIniFile>ProgramCounter.ini,Counters,Opened,0
EditIniFile>ProgramCounter.ini,Counters,Closed,0
// Get INI Opened/Closed/Active values
Label>UpdateINI
ReadIniFile>ProgramCounter.ini,Counters,Closed,vClosed
ReadIniFile>ProgramCounter.ini,Counters,Opened,vOpened
Let>vActive=%VOpened%-%vClosed%
// Update current counts
Let>vOpened=%VOpened%+1
Let>vActive=%vActive%+1
// Write new INI Opened value
EditIniFile>ProgramCounter.ini,Counters,Opened,%VOpened%
MessageModal>Active copies of Program: %vActive%
Code: Select all
/*
Purpose:
To count the number of times a program is closed.
Triggered in Macro Scheduler Event: WindowClose, enter name of window
Designed with
XP-PRO SP2
Macro Scheduler 11.1.05
Tested with "Calculator"
=============================================================
*/
// Closed Program counter
IfFileExists>ProgramCounter.ini,UpdateINI
WriteLn>ProgramCounter.ini,vResult,[Counters]
EditIniFile>ProgramCounter.ini,Counters,Opened,0
EditIniFile>ProgramCounter.ini,Counters,Closed,0
Label>UpdateINI
// Get INI Opened/Closed/Active values
ReadIniFile>ProgramCounter.ini,Counters,Closed,vClosed
ReadIniFile>ProgramCounter.ini,Counters,Opened,vOpened
Let>vActive=%VOpened%-%vClosed%
// Update current counts
Let>vClosed=%VClosed%+1
Let>vActive=%vActive%-1
// Write new INI Closed value
EditIniFile>ProgramCounter.ini,Counters,Closed,%VClosed%
MessageModal>Active copies of Program: %vActive%
Then, to my surprise, I found that they trigger just like the commands for OnEvent, they did not fire each time a new window opened up or closed. They keep running once they start and do not fire again.
I never thought to test for multiple windows in the past, and always thought that when the defined window opened, the macro script would trigger. This is an unpleasant surprise. I could accept how the script commands worked, but did not expect a similar behaviour with the scheduling tools.
I would like to see two enhancements:
1. Allow the scheduler tools to trigger the scripts every time the defined window is opened/closed, allowing multiple windows to fire each time.
2. Provide the script commands with event triggers for when the windows actually open/close, in addition to the existing ones that run while they are open/closed.