Jump to content
Sign in to follow this  
CreatoX

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

Recommended Posts

Hi,

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 ****
#AutoIt3Wrapper_Compile_Both=y
#AutoIt3Wrapper_UseX64=y
#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)
WEnd

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")
    Exit
Else
    Sleep(250)
    ; right click and select fourth menu item (up)
    _GUICtrlToolbar_ClickButton($hSysTray_Handle, $iSystray_ButtonNumber, "right")
    Sleep(1000)
    Send("{UP 4}")
    $sItemName = GetPopUpSelText() ; This will read the currently selected item <<<<<<<<<<<<<<<<<<<<<<<<<<<<
    Send("{ENTER}")
    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 ()
     Else
        SplashTextOn ( "", "Option Not Found!" , 160 , 40, -1, -1, 1,"",10, 600 )
        SoundPlay(@WindowsDir & "\media\chord.wav", 0)
        Sleep (3000)
        SplashOff ()
    EndIf
EndIf

EndFunc

;............

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")
        Exit
     EndIf
    ; 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)
    EndIf

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

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

EndFunc


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")
      Exit
   EndIf

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

EndFunc


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)
                EndIf
            Next
        EndIf
    EndIf
    Return ""

EndFunc

Thank you!

Edited by ckovoor

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...