WideBoyDixon Posted April 2, 2009 Share Posted April 2, 2009 (edited) 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. expandcollapse popup#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 April 2, 2009 by WideBoyDixon [center]Wide by name, Wide by nature and Wide by girth[u]Scripts[/u]{Hot Folders} {Screen Calipers} {Screen Crosshairs} {Cross-Process Subclassing} {GDI+ Clock} {ASCII Art Signatures}{Another GDI+ Clock} {Desktop Goldfish} {Game of Life} {3D Pie Chart} {Stock Tracker}[u]UDFs[/u]{_FileReplaceText} {_ArrayCompare} {_ToBase}~ My Scripts On Google Code ~[/center] Link to comment Share on other sites More sharing options...
WideBoyDixon Posted April 2, 2009 Author Share Posted April 2, 2009 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 [center]Wide by name, Wide by nature and Wide by girth[u]Scripts[/u]{Hot Folders} {Screen Calipers} {Screen Crosshairs} {Cross-Process Subclassing} {GDI+ Clock} {ASCII Art Signatures}{Another GDI+ Clock} {Desktop Goldfish} {Game of Life} {3D Pie Chart} {Stock Tracker}[u]UDFs[/u]{_FileReplaceText} {_ArrayCompare} {_ToBase}~ My Scripts On Google Code ~[/center] Link to comment Share on other sites More sharing options...
Ascend4nt Posted April 2, 2009 Share Posted April 2, 2009 (edited) 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 April 2, 2009 by ascendant My contributions: Performance Counters in Windows - Measure CPU, Disk, Network etc Performance | Network Interface Info, Statistics, and Traffic | CPU Multi-Processor Usage w/o Performance Counters | Disk and Device Read/Write Statistics | Atom Table Functions | Process, Thread, & DLL Functions UDFs | Process CPU Usage Trackers | PE File Overlay Extraction | A3X Script Extract | File + Process Imports/Exports Information | Windows Desktop Dimmer Shade | Spotlight + Focus GUI - Highlight and Dim for Eyestrain Relief | CrossHairs (FullScreen) | Rubber-Band Boxes using GUI's (_GUIBox) | GUI Fun! | IE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) | Magnifier (Vista+) Functions UDF | _DLLStructDisplay (Debug!) | _EnumChildWindows (controls etc) | _FileFindEx | _ClipGetHTML | _ClipPutHTML + ClipPutHyperlink | _FileGetShortcutEx | _FilePropertiesDialog | I/O Port Functions | File(s) Drag & Drop | _RunWithReducedPrivileges | _ShellExecuteWithReducedPrivileges | _WinAPI_GetSystemInfo | dotNETGetVersions | Drive(s) Power Status | _WinGetDesktopHandle | _StringParseParameters | Screensaver, Sleep, Desktop Lock Disable | Full-Screen Crash Recovery Wrappers/Modifications of others' contributions: _DOSWildcardsToPCRegEx (original code: RobSaunder's) | WinGetAltTabWinList (original: Authenticity) UDF's added support/programming to: _ExplorerWinGetSelectedItems | MIDIEx UDF (original code: eynstyne) (All personal code/wrappers centrally located at Ascend4nt's AutoIT Code) Link to comment Share on other sites More sharing options...
CyberMax Posted April 22, 2014 Share Posted April 22, 2014 (edited) 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 April 22, 2014 by CyberMax Link to comment Share on other sites More sharing options...
jchd Posted April 22, 2014 Share Posted April 22, 2014 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 hereRegExp tutorial: enough to get startedPCRE 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) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now