Zohar Posted March 13, 2021 Share Posted March 13, 2021 (edited) Hi all I am using Windows 10 Home. I need to get the number of running programs - programs that are displayed on the Taskbar, and not the total # of processes running, or # of Windows that currently exist in the system. So for example: If you have just restarted your PC, and Windows finished booting, and you have not ran any program, then the result should be 0, and If you now ran Notepad and Firefox, then the # should be 2. Important note: What I really need is not the accurate # of programs running, but only to know if it's bigger than 0. The reason: I have a small script that Restarts or Shuts Down the computer, and I want that script to verify that there are no running programs (in the Taskbar, and not via ProcesssList etc), so I will not Restart/Shutdown If it's >0.. Thank you Edited August 14, 2021 by Zohar Link to comment Share on other sites More sharing options...
Nine Posted March 13, 2021 Share Posted March 13, 2021 (edited) UIAutomation is the way to go. Fortunately, I have an already written script for it : expandcollapse popup#include <Constants.au3> #include "Includes\CUIAutomation2.au3" ; https://www.autoitscript.com/forum/topic/153520-iuiautomation-ms-framework-automate-chrome-ff-ie/ Opt("MustDeclareVars", 1) Global $pElement, $pCondition, $pCondition1, $pCondition2, $pElementArray, $iElements, $vValue Global $hCtrl, $oUIAutomation, $oElement, $oElementArray ; Get taskbar handle $hCtrl = ControlGetHandle("[Class:Shell_TrayWnd]", "", "MSTaskListWClass1") ; Get UIAutomation object $oUIAutomation = ObjCreateInterface($sCLSID_CUIAutomation, $sIID_IUIAutomation, $dtagIUIAutomation) ; Get taskbar element $oUIAutomation.ElementFromHandle($hCtrl, $pElement) $oElement = ObjCreateInterface($pElement, $sIID_IUIAutomationElement, $dtagIUIAutomationElement) ; Get condition (ControlType = Button) $oUIAutomation.CreatePropertyCondition($UIA_ControlTypePropertyId, $UIA_ButtonControlTypeId, $pCondition1) ; Get condition (ControlType = MenuItem) $oUIAutomation.CreatePropertyCondition($UIA_ControlTypePropertyId, $UIA_MenuItemControlTypeId, $pCondition2) ; Create OR condition $oUIAutomation.CreateOrCondition($pCondition1, $pCondition2, $pCondition) ; Find all buttons and menu items $oElement.FindAll($TreeScope_Children, $pCondition, $pElementArray) $oElementArray = ObjCreateInterface($pElementArray, $sIID_IUIAutomationElementArray, $dtagIUIAutomationElementArray) $oElementArray.Length($iElements) ; Get array of buttons and menu items Global $aElements[$iElements] For $i = 0 To $iElements - 1 $oElementArray.GetElement($i, $pElement) $aElements[$i] = ObjCreateInterface($pElement, $sIID_IUIAutomationElement, $dtagIUIAutomationElement) Next Local $iCount = 0 For $i = 0 To UBound($aElements) - 1 $aElements[$i].GetCurrentPropertyValue($UIA_NamePropertyId, $vValue) ConsoleWrite("Name:" & $vValue) $aElements[$i].GetCurrentPropertyValue($UIA_LegacyIAccessibleStatePropertyId, $vValue) ConsoleWrite (" / State " & $vValue & @CRLF) If $vValue Then $iCount += 1 Next MsgBox ($MB_SYSTEMMODAL, "Info", "There is " & $iCount & " programs running in System Toolbar") Edited March 13, 2021 by Nine Created OR condition Zohar 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Zohar Posted March 13, 2021 Author Share Posted March 13, 2021 Thank you very much Nine. Will this code be affected by your Taskbar Setting for Expanding/Not Expanding each Program on the Taskbar? And BTW, Is there a chance that Microsoft also created some API or APIs around this matter Link to comment Share on other sites More sharing options...
Nine Posted March 13, 2021 Share Posted March 13, 2021 If expanding, you should have more programs counted. In my script, MenuItems are counted as a single program... No idea if it is planned by MS to make such API. “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Zohar Posted March 13, 2021 Author Share Posted March 13, 2021 (edited) I see. I will experiment with both then. BTW, There are all the _GUICtrl*() functions in AutoIt, such as _GUICtrlListView_GetItemCount(), etc.. So when do you prefer to use UIAutomation functions and when to use the _GUICtrl* functions? Edited March 13, 2021 by Zohar Link to comment Share on other sites More sharing options...
Nine Posted March 13, 2021 Share Posted March 13, 2021 Depends mostly on how easy controls are accessible by standard functions. Win7 vs Win10. Properties and methods availability. I know UIA is a tad more complex to use and documentation is somewhat dispersed in MSDN. But it stands very powerful. “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Zohar Posted March 13, 2021 Author Share Posted March 13, 2021 I understand. BTW another method that I thought about now, is to use PixelGet() to look for the blue line that appears above any running program, in the Taskbar. This of course depends on the Taskbar not being set to Auto-Hide.. Link to comment Share on other sites More sharing options...
Nine Posted March 13, 2021 Share Posted March 13, 2021 Well, good luck with that. Way too dependent of theme and OS and screen resolution and etc, etc... “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Deye Posted March 13, 2021 Share Posted March 13, 2021 10 hours ago, Zohar said: Important note: What I really need is not the accurate # of programs running, but only to know if it's bigger than 0. isn't this all you needed or is it now about expanding buttons as well let me know if in some case this wont work (you would simply need to add new cases, added or ignored - windows styles ..) #include <WinAPISysWin.au3> #include <WindowsConstants.au3> MsgBox("", "Buttons Count", TaskbarButtonsCount()) Func TaskbarButtonsCount() Local $iCount = 0, $style, $ahw = _WinAPI_EnumWindowsTop() For $i = 1 To $ahw[0][0] - 1 If Not _WinAPI_GetWindowLong($ahw[$i][0], $GWL_HWNDPARENT) And _ Not BitAND(_WinAPI_GetWindowLong($ahw[$i][0], $GWL_STYLE), $WS_POPUP) Then $iCount += 1 EndIf Next Return $iCount EndFunc Zohar 1 Link to comment Share on other sites More sharing options...
Zohar Posted March 13, 2021 Author Share Posted March 13, 2021 (edited) A Really nice different approach.. I will now try it, thank you very much to both of you. Regarding:>or is it now about expanding buttons as well I mentioned expanded button in the Taskbar, because my Taskbar might be in either state of that setting: Never Expanded, or Always Expanded, so I wondered If changing the setting from one to another will still get the counting function to work. I will compare both ways now, and see which one gives the most reliable result. Edited March 13, 2021 by Zohar 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