JYoun Posted February 9, 2022 Share Posted February 9, 2022 Hi, I starting to create some test automation script using AutoIt v3 and still novice on it. I want to right-click an icon in system tray on Windows 10 or 11 using _GUICtrlToolbar_ClickButton(), but it does not click a proper icon. This is my code for testing what button is clicked : #Include <GuiToolBar.au3> Global $hSysTray_Handle Get_Systray_Info() _GUICtrlToolbar_ClickButton($hSysTray_Handle, 5, "right", True) Func Get_Systray_Info() ; Find systray handle $hSysTray_Handle = ControlGetHandle('[Class:Shell_TrayWnd]', 'User Promoted Notification Area', '[Class:ToolbarWindow32;Instance:2]') If @error Then MsgBox(16, "Error", "System tray not found") Exit EndIf Local $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) $sButtonText = _GUICtrlToolbar_GetButtonText($hSysTray_Handle, $iSystray_ButtonNumber) ConsoleWrite("Button Index : " & $iSystray_ButtonNumber & @CRLF) ConsoleWrite("Button Text : "& $sButtonText & @CRLF) Next EndFunc As for console output, index 5 is a button that I want to click. But when `_GUICtrlToolbar_ClickButton($hSysTray_Handle, 5, "right", True)` has executed, the button index 4 is clicked. If I change the index parameter of `_GUICtrlToolbar_ClickButton` to 6, it clicks button #6. And for index parameter 4, it clicks button #3. I suspecting that AutoIt cannot calculate width of systray buttons properly. Is there any method to define or adjust the width of buttons? Thank you. Link to comment Share on other sites More sharing options...
Nine Posted February 9, 2022 Share Posted February 9, 2022 On my Win10, it is instance 3 not 2. And all buttons are correctly pushed. “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...
Solution ad777 Posted February 10, 2022 Solution Share Posted February 10, 2022 (edited) @JYoun this how you Sort Button by Command: #include <GuiToolBar.au3> ;;Tested in Win 7 [Instance:1] Global $hSysTray_Handle = ControlGetHandle('[Class:Shell_TrayWnd]', 'User Promoted Notification Area', '[Class:ToolbarWindow32;Instance:1]') _GUICtrlToolbar_SortByButton($hSysTray_Handle) Func _GUICtrlToolbar_SortByButton($hSysTray_Handle) Local $iSystray_ButCount = _GUICtrlToolbar_ButtonCount($hSysTray_Handle) For $iSystray_ButtonNumber = 0 To $iSystray_ButCount - 1 _GUICtrlToolbar_MoveButton($hSysTray_Handle, _GUICtrlToolbar_CommandToIndex($hSysTray_Handle, $iSystray_ButtonNumber), $iSystray_ButtonNumber) Next EndFunc ;==>_GUICtrlToolbar_SortByButton As for console output, index 5 is a button that I want to click. But when `_GUICtrlToolbar_ClickButton($hSysTray_Handle, 5, "right", True)` has executed, the button index 4 is clicked. and this how you click Button by command: Global $hSysTray_Handle = ControlGetHandle('[Class:Shell_TrayWnd]', 'User Promoted Notification Area', '[Class:ToolbarWindow32;Instance:1]') _GUICtrlToolbar_ClickButtonByCommand($hSysTray_Handle, 1) Func _GUICtrlToolbar_ClickButtonByCommand($hSysTray_Handle, $Button, $Click = "right", $tp = True) _GUICtrlToolbar_ClickButton($hSysTray_Handle, _GUICtrlToolbar_IndexToCommand($hSysTray_Handle, _GUICtrlToolbar_CommandToIndex($hSysTray_Handle, $Button)), $Click, $tp) EndFunc ;==>_GUICtrlToolbar_ClickButtonByCommand i did little fix on your script: #include <GuiToolBar.au3> Global $hSysTray_Handle Get_Systray_Info() Func Get_Systray_Info() ; Find systray handle $hSysTray_Handle = ControlGetHandle('[Class:Shell_TrayWnd]', 'User Promoted Notification Area', '[Class:ToolbarWindow32;Instance:1]') If @error Then MsgBox(16, "Error", "System tray not found") Exit EndIf Local $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) $sButtonText = _GUICtrlToolbar_GetButtonText($hSysTray_Handle, $iSystray_ButtonNumber) ConsoleWrite("Button Text : " & $sButtonText & @CRLF) ConsoleWrite("Button Index : " & $iSystray_ButtonNumber & @CRLF) Next EndFunc ;==>Get_Systray_Info Edited February 10, 2022 by ad777 iam ِAutoit programmer. best thing in life is to use your Brain to Achieve everything you want. Link to comment Share on other sites More sharing options...
JYoun Posted February 21, 2022 Author Share Posted February 21, 2022 (edited) @Nine Thank you for reply. My testing environment is Win11, and I'll check again on Win10. @ad777 Thank you very much for solution. Actually it does not exactly works in my environment(still clicks right next of target icon), but I got some clue from your solution - move target icon to the corner of system tray and click it using IndexToCommand and CommandToIndex. Seems works fine currently and I'll try to test in another environments. Edited February 21, 2022 by JYoun 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