Jump to content

[Solved] How to Get the Number of Running Programs that are Displayed in the Taskbar


 Share

Recommended Posts

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 by Zohar
Link to comment
Share on other sites

UIAutomation is the way to go.  Fortunately, I have an already written script for it :

#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 by Nine
Created OR condition
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by Zohar
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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

 

Link to comment
Share on other sites

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 by Zohar
Link to comment
Share on other sites

  • Zohar changed the title to [Solved] How to Get the Number of Running Programs that are Displayed in the Taskbar

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...