Jump to content

Manipulate system tray program? (right click, choose option)


Recommended Posts


Thanks for the help. I set a function to call and also used a While.....WEnd loop to keep the script running, so now I have a program which also runs optionally with a hotkey:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Constants.au3>
; AutoIt Version: 3.0
; Language:       English
; Platform:       Win9x/NT
; Author:         Melba23 (modified by Chakko Kovoor)
; Script Function:
;   toggles enable/disable macros (switch)on Input Director menu brought up when you right-click on its tray icon, with confirmation of action
;   running this script toggles the switch and also sets up hotkey F4 for further toggles

#include <WindowsConstants.au3>
#include <GUIToolbar.au3>
#include <WinAPI.au3>
#Include <GuiMenu.au3>
#include <GuiButton.au3>

Call ( "Togglemacro" )

HotKeySet ("{F4}","Togglemacro")
While 1
    Sleep (1000)

Func Togglemacro()

Opt("WinTitleMatchMode", 2)
Global $hSysTray_Handle, $iSystray_ButtonNumber, $sItemName

Global $sToolTipTitle = "Input Director (Master)" ; <<<<<<<<<<<<<<<< Enter some tooltip text for the icon you want here

$iSystray_ButtonNumber = Get_Systray_Index($sToolTipTitle)

If $iSystray_ButtonNumber = -1 Then
    MsgBox(16, "Error", "Icon not found in system tray")
    ; right click and select fourth menu item (up)
    _GUICtrlToolbar_ClickButton($hSysTray_Handle, $iSystray_ButtonNumber, "right")
    Send("{UP 4}")
    $sItemName = GetPopUpSelText() ; This will read the currently selected item <<<<<<<<<<<<<<<<<<<<<<<<<<<<
    If $sItemName = "Enable Macros" Then
        SplashTextOn ( "", "Macros ENABLED" , 160 , 40, -1, -1, 1,"",10, 600 )
        SoundPlay(@WindowsDir & "\media\chimes.wav", 0)
        Sleep (3000)
        SplashOff ()
     Elseif $sItemName = "Disable Macros" Then
        SplashTextOn ( "", "Macros DISABLED" , 160 , 40, -1, -1, 1,"",10, 600 )
        SoundPlay(@WindowsDir & "\media\chimes.wav", 0)
        Sleep (3000)
        SplashOff ()
        SplashTextOn ( "", "Option Not Found!" , 160 , 40, -1, -1, 1,"",10, 600 )
        SoundPlay(@WindowsDir & "\media\chord.wav", 0)
        Sleep (3000)
        SplashOff ()



Func Get_Systray_Index($sToolTipTitle)

    ; Find systray handle
    $hSysTray_Handle = ControlGetHandle('[Class:Shell_TrayWnd]', '', '[Class:ToolbarWindow32;Instance:1]')

    If @error Then
        MsgBox(16, "Error", "System tray not found")
    ; Get systray item count
    Local $iSystray_ButCount = _GUICtrlToolbar_ButtonCount($hSysTray_Handle)
    If $iSystray_ButCount = 0 Then
        ; check overflow section of systray
         Return Get_OverflowSystray_Index($sToolTipTitle)

    ; Look for wanted tooltip
    For $iSystray_ButtonNumber = 0 To $iSystray_ButCount - 1
        If StringInStr(_GUICtrlToolbar_GetButtonText($hSysTray_Handle, $iSystray_ButtonNumber), $sToolTipTitle) <> 0 Then ExitLoop

    If $iSystray_ButtonNumber = $iSystray_ButCount Then
        Return Get_OverflowSystray_Index($sToolTipTitle)
        Return $iSystray_ButtonNumber ; Found


Func Get_OverflowSystray_Index($sToolTipTitle)

   $hSysTray_Handle = ControlGetHandle('[Class:NotifyIconOverflowWindow]', '', '[Class:ToolbarWindow32;Instance:1]')
   $iSystray_ButCount = _GUICtrlToolbar_ButtonCount($hSysTray_Handle)
   If $iSystray_ButCount = 0 Then
      MsgBox(16, "Error", "No items found in system tray")

   For $iSystray_ButtonNumber = 0 To $iSystray_ButCount - 1
     If StringInStr(_GUICtrlToolbar_GetButtonText($hSysTray_Handle, $iSystray_ButtonNumber), $sToolTipTitle) <> 0 Then ExitLoop
   If $iSystray_ButtonNumber < $iSystray_ButCount Then
      $hSysTray_OverflowButton = ControlGetHandle('[Class:Shell_TrayWnd]', '', '[CLASS:Button; INSTANCE:1]')
      Return $iSystray_ButtonNumber ; Found
      Return -1


Func GetPopUpSelText()

    Local $aPopUp_List = _WinAPI_EnumWindowsPopup()
    Local $hWnd = $aPopUp_List[1][0]
    Local $sClass = $aPopUp_List[1][1]
    If $sClass = "#32768" Then ; This is a "standard" Windows API popup menu
        $hMenu = _SendMessage($hWnd, $MN_GETHMENU, 0, 0)
        If _GUICtrlMenu_IsMenu($hMenu) Then
            $iCount = _GUICtrlMenu_GetItemCount($hMenu)
            For $j = 0 To $iCount - 1
                If _GUICtrlMenu_GetItemHighlighted($hMenu, $j) Then
                    Return _GUICtrlMenu_GetItemText($hMenu, $j)
    Return ""


Thank you!

Edited by ckovoor
Link to comment
Share on other sites

  • 7 years later...

I have been looking to automate the enabling and disabling Input Director and I came across this thread.  I have tried the most recent code shown here but this code can not find the "Input Director" icon in the tray.  I have all of my hidden tray icons visible and this code still does not find the icon.  Any ideas or help would be appreciated.


Link to comment
Share on other sites

You might be better to use UIAutomation. Here the code to locate your button :

; https://www.autoitscript.com/forum/topic/201683-ui-automation-udfs/
#include "Includes\CUIAutomation2.au3"
#include <Constants.au3>

Opt("MustDeclareVars", 1)

If @OSVersion <> "WIN_7" Then Exit MsgBox ($MB_SYSTEMMODAL,"","This script only works on Win 7")

Local $aMenu = _Systray_SelectOption("Name of your button here") ; <<<<<<<<<<<<<<<<<< change this

Func _Systray_SelectOption($sButton)
  Local $hWnd = WinGetHandle('[Class:Shell_TrayWnd]')
  If Not $hWnd Then Return SetError(1)

  Local $hCtrl = ControlGetHandle($hWnd, '', "ToolbarWindow321") ; button must be visible
  If Not $hCtrl Then Return SetError(2)

  ; Create UI Automation object
  Local $oUIAutomation = ObjCreateInterface($sCLSID_CUIAutomation, $sIID_IUIAutomation, $dtagIUIAutomation)
  If Not IsObj($oUIAutomation) Then Return ConsoleWrite("$oUIAutomation ERR" & @CRLF)
  ConsoleWrite("$oUIAutomation OK" & @CRLF)

  ; Get Desktop element
  Local $pDesktop, $oDesktop
  $oDesktop = ObjCreateInterface($pDesktop, $sIID_IUIAutomationElement, $dtagIUIAutomationElement)
  If Not IsObj($oDesktop) Then Return ConsoleWrite("$oDesktop ERR" & @CRLF)
  ConsoleWrite("$oDesktop OK" & @CRLF)

  ; Get element by handle
  Local $pElement, $oElement
  $oUIAutomation.ElementFromHandle($hCtrl, $pElement)
  $oElement = ObjCreateInterface($pElement, $sIID_IUIAutomationElement, $dtagIUIAutomationElement)
  If Not IsObj($oElement) Then Return ConsoleWrite("$oElement ERR" & @CRLF)
  ConsoleWrite("$oElement OK" & @CRLF)

  ; Get condition (ControlType = Button)
  Local $pCondition, $oCondition
  $oUIAutomation.CreatePropertyCondition($UIA_ControlTypePropertyId, $UIA_ButtonControlTypeId, $pCondition)
  $oCondition = ObjCreateInterface($pCondition, $sIID_IUIAutomationPropertyCondition, $dtagIUIAutomationPropertyCondition)
  If Not IsObj($oCondition) Then Return ConsoleWrite("$oCondition ERR" & @CRLF)
  ConsoleWrite("$oCondition OK" & @CRLF)

  ; Find all buttons
  Local $pElementArray, $oElementArray, $iElements
  $oElement.FindAll($TreeScope_Children, $oCondition, $pElementArray)
  $oElementArray = ObjCreateInterface($pElementArray, $sIID_IUIAutomationElementArray, $dtagIUIAutomationElementArray)
  If Not IsObj($oElementArray) Then Return ConsoleWrite("$oElementArray ERR" & @CRLF)
  ConsoleWrite("$oElementArray OK" & @CRLF)

  ; Get array of buttons
  Local $aElements[$iElements]
  For $i = 0 To $iElements - 1
    $oElementArray.GetElement($i, $pElement)
    $aElements[$i] = ObjCreateInterface($pElement, $sIID_IUIAutomationElement, $dtagIUIAutomationElement)

  ; Get name and position for each button
  Local $sValue
  Local $tRect = DllStructCreate("long Left;long Top;long Right;long Bottom")
  Local $tPoint = DllStructCreate("long X;long Y"), $bBool
  For $i = 0 To UBound($aElements) - 1
    $aElements[$i].GetCurrentPropertyValue($UIA_NamePropertyId, $sValue)
    ConsoleWrite("Name = " & $sValue)
    ConsoleWrite("/ L:" & $tRect.Left & " T:" & $tRect.Top & " R:" & $tRect.Right & " B:" & $tRect.Bottom & @CRLF)
    $aElements[$i].GetClickablePoint($tPoint, $bBool)
    If StringInStr($sValue, $sButton) Then
      MouseClick("Right", $tPoint.X, $tPoint.Y, 1, 1)
      ConsoleWrite("Found and clicked !" & @CRLF)


Link to comment
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

  • Recently Browsing   0 members

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