Jump to content
Sign in to follow this  
WideBoyDixon

Detect new applications

Recommended Posts

WideBoyDixon

I've seen this requested a few times now so I thought I'd have a stab at it. This script attempts to monitor changes in the visible windows so it will detect when applications are opened and closed. It's a bit clunky (especially using two loops to construct the difference) but I thought I'd throw it out there as an example script.

#include <Array.au3>
#include <Misc.au3>

TrayTip("Windows Changes", "Monitoring windows ...", 1, 1)
Global $aVW1 = _GetVisibleWindows(), $aVW2, $i, $j, $sDiff, $bFound
While 1
    $aVW2 = _GetVisibleWindows()
    $sDiff = ""
    For $i = 1 To $aVW1[0][0]
        $bFound = False
        For $j = 1 To $aVW2[0][0]
            If $aVW1[$i][1] = $aVW2[$j][1] Then
                $bFound = True
                ExitLoop
            EndIf
        Next
        If Not $bFound Then $sDiff &= _IIf($sDiff = "", "", @CRLF) & "Closed : " & $aVW1[$i][0]
    Next
    For $j = 1 To $aVW2[0][0]
        $bFound = False
        For $i = 1 To $aVW1[0][0]
            If $aVW1[$i][1] = $aVW2[$j][1] Then
                $bFound = True
                ExitLoop
            EndIf
        Next
        If Not $bFound Then $sDiff &= _IIf($sDiff = "", "", @CRLF) & "Opened : " & $aVW2[$j][0]
    Next
    If $sDiff <> "" Then TrayTip("Window Changes", $sDiff, 1, 1)
    $aVW1 = $aVW2
    Sleep(1000)
Wend

Func _GetVisibleWindows()
    Local $aRet = WinList(), $i = 1
    While $i <= $aRet[0][0]
        If (BitAND(WinGetState($aRet[$i][1]), 2) = 2) And ($aRet[$i][0] <> "") Then
            $i += 1
        Else
            _ArrayDelete($aRet, $i)
            $aRet[0][0] -= 1
        EndIf
    Wend
    Return $aRet
EndFunc

If anyone has any improvements to make or suggestions then they're welcome! Especially the ugly method of calculating the changes ...

WBD

Edited by WideBoyDixon

Share this post


Link to post
Share on other sites
WideBoyDixon

I realised that new Windows might not be as important as new processes. Here's another shot this time using the ProcessList()

#include <Array.au3>
#include <Misc.au3>

TrayTip("Process Changes", "Monitoring processes ...", 1, 1)
Global $aPL1 = ProcessList(), $aPL2, $i, $j, $sDiff, $bFound
While 1
    $aPL2 = ProcessList()
    $sDiff = ""
    For $i = 1 To $aPL1[0][0]
        $bFound = False
        For $j = 1 To $aPL2[0][0]
            If $aPL1[$i][1] = $aPL2[$j][1] Then
                $bFound = True
                ExitLoop
            EndIf
        Next
        If Not $bFound Then $sDiff &= _IIf($sDiff = "", "", @CRLF) & "Ended : " & $aPL1[$i][0]
    Next
    For $j = 1 To $aPL2[0][0]
        $bFound = False
        For $i = 1 To $aPL1[0][0]
            If $aPL1[$i][1] = $aPL2[$j][1] Then
                $bFound = True
                ExitLoop
            EndIf
        Next
        If Not $bFound Then $sDiff &= _IIf($sDiff = "", "", @CRLF) & "Started : " & $aPL2[$j][0]
    Next
    If $sDiff <> "" Then TrayTip("Process Changes", $sDiff, 1, 1)
    $aPL1 = $aPL2
    Sleep(1000)
Wend

WBD

Share this post


Link to post
Share on other sites
Ascend4nt

Here's a variation that allows you to skip traversing the array each time until an actual change happens. It does a simple check to see if a.) the # of processes has changed, or if not, then b.) the last Process ID # in the list doesn't match the previous list's last-Process ID #. I've found this to be a reliable method to detect new processes for a process information project I've created for myself.

This is all due to the nice way ProcessList() collects processes in the order of their creation, and the extremely unlikely condition that if as many processes close that open, the same Process ID# will be applied to the last (or really, most any of the new processes created in the time between checks). You could however also add a comparison for the last-process name too, if you'd like to be absolutely sure.

I also added a simple HotKey escape:

Global $aPrevProcessList,$iPrevLastPID,$iPrevProcessCount
Global $bHotKeyPressed=False

Func _EscPressed()
    $bHotKeyPressed=True
EndFunc

HotKeySet("{Esc}", "_EscPressed")

$aPrevProcessList=ProcessList()
$iPrevProcessCount=$aPrevProcessList[0][0]
$iPrevLastPID=$aPrevProcessList[$iPrevProcessCount][1]

Local $aProcessList
Do
    $aProcessList=ProcessList()
    If $iPrevProcessCount<>$aProcessList[0][0] Or $iPrevLastPID<>$aProcessList[$aProcessList[0][0]][1] Then
        ConsoleWrite("Changes in Process List found"&@CRLF)
        ; Do the individual comparisons\updates here
        
        ; And then set 'prev' information to current
        $iPrevProcessCount=$aProcessList[0][0]
        $iPrevLastPID=$aProcessList[$iPrevProcessCount][1]
        $aPrevProcessList=$aProcessList
    EndIf
    Sleep(250)
Until $bHotKeyPressed

*edit: got rid of smiley that popped up in the wrong place

Edited by ascendant

Share this post


Link to post
Share on other sites
CyberMax

I realised that new Windows might not be as important as new processes. Here's another shot this time using the ProcessList()

#include <Array.au3>
#include <Misc.au3>

TrayTip("Process Changes", "Monitoring processes ...", 1, 1)
Global $aPL1 = ProcessList(), $aPL2, $i, $j, $sDiff, $bFound
While 1
    $aPL2 = ProcessList()
    $sDiff = ""
    For $i = 1 To $aPL1[0][0]
        $bFound = False
        For $j = 1 To $aPL2[0][0]
            If $aPL1[$i][1] = $aPL2[$j][1] Then
                $bFound = True
                ExitLoop
            EndIf
        Next
        If Not $bFound Then $sDiff &= _IIf($sDiff = "", "", @CRLF) & "Ended : " & $aPL1[$i][0]
    Next
    For $j = 1 To $aPL2[0][0]
        $bFound = False
        For $i = 1 To $aPL1[0][0]
            If $aPL1[$i][1] = $aPL2[$j][1] Then
                $bFound = True
                ExitLoop
            EndIf
        Next
        If Not $bFound Then $sDiff &= _IIf($sDiff = "", "", @CRLF) & "Started : " & $aPL2[$j][0]
    Next
    If $sDiff <> "" Then TrayTip("Process Changes", $sDiff, 1, 1)
    $aPL1 = $aPL2
    Sleep(1000)
Wend
WBD

 

 

This code sometimes compiled without error, sometimes compiled with error

in  "If Not $bFound Then $sDiff &= _IIf($sDiff = "", "", @CRLF) & "Ended : " & $aPL1[$i][0]"

with error : _IIf(): undefined function.

Any ideas why? I didnt change the code but scite sometimes gives me this error above, other times, it compiled without error.

I even use 3.3.10.2 and 3.3.11.4, using latest scite and I even include the updated beta files, but still giving me this problem.

Eg. I compiled 10 times, 8 times have this error without even changing anything in the code.

   

Edited by CyberMax

Share this post


Link to post
Share on other sites
jchd

Look at the changelogs: _Iif is obsolete. Use the new ternary operator.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.