Jump to content

ControlClick tray icon context menu


Recommended Posts

I need to programatically connect to a VPN - I use ProtonVPN. It has some API support but Linux only. (Wasn't able to to make OpenVPN work...)

So I decided to give the old AutoIt a shot.

I need to right click the icon in tray (hidden) and using the context menu pick 'Connect'.

image.png.4c38972f119f8823a9dbea6d2fe97374.png

  1. First issue is that the icon doesn't have a ButtonText.
  2. Second issue is that even when I try to click an icon which has a button text, the click doesn't click the icon but rather clicks at the spot where the icon would be if the tray container was open. When I run the script and quickly open the tray container (^ icon) the click goes through just fine.
  3. No idea on how to get and click the context menu buttons.

My code:

#include <GuiToolBar.au3>

Sleep(1000)
; Find systray handle for "User Promoted Notification Area"
Local $hSysTray_Handle = ControlGetHandle('[Class:Shell_TrayWnd]', '', '[Class:ToolbarWindow32;Instance:1]')
If @error Then
    MsgBox(16, "Error", "System tray not found")
EndIf
; Find systray handle for "Overflow Notification Area" i.e. hidden icons
Local $hSysTray_Handle_Hidden = ControlGetHandle('[Class:NotifyIconOverflowWindow]', '', '[Class:ToolbarWindow32;Instance:1]')
If @error Then
    MsgBox(16, "Error", "Overflow Notification Area not found")
EndIf

$iSysTray_ButCount = _GUICtrlToolbar_ButtonCount($hSysTray_Handle_Hidden)
For $iSysTray_ButtonNumber = 0 To $iSysTray_ButCount - 1
    if _GUICtrlToolbar_GetButtonText($hSysTray_Handle_Hidden, $iSysTray_ButtonNumber) = "StrokeIt" Then
        ConsoleWrite("Found" & @CRLF)
        _GUICtrlToolbar_ClickButton($hSysTray_Handle_Hidden, $iSysTray_ButtonNumber, "right", False, 1, 0)
        ExitLoop
    EndIf
Next

 

Edited by Seminko
typos
Link to comment
Share on other sites

Here how you can do it (used Bluetooth as an example):

#NoTrayIcon
#include <Constants.au3>
#Include <GuiToolBar.au3>

_Systray_Hidden ()
ConsoleWrite ("Error = " & @error & @CRLF)

Func _Systray_Hidden ()
  Local $hWnd = WinGetHandle ('[Class:Shell_TrayWnd]')
  If Not $hWnd Then Return SetError (1)
  Local $hCtrl = ControlGetHandle($hWnd, "", "Button2")
  If Not $hCtrl Then Return SetError (2)
  ControlClick ($hWnd, "", $hCtrl)
  Sleep (600)
  $hWnd = WinGetHandle ('[Class:NotifyIconOverflowWindow]')
  Local $hSysTray = ControlGetHandle($hWnd, '', "ToolbarWindow321")
  If Not $hSysTray Then Return SetError (3)

  Local $iSystray_ButCount = _GUICtrlToolbar_ButtonCount($hSysTray)
  If Not $iSystray_ButCount Then Return SetError (4)
  For $i = 0 To $iSystray_ButCount-1
    ConsoleWrite (_GUICtrlToolbar_GetButtonText($hSysTray, $i) & @CRLF)
    If StringInStr (_GUICtrlToolbar_GetButtonText($hSysTray, $i), "BlueTooth") Then
      _GUICtrlToolbar_ClickButton($hSysTray, $i, "left")
      Sleep (850)
      ControlSend("[CLASS:#32768]","","","{UP 2}{ENTER}")
      Return 1
    EndIf
  Next
  Return SetError (5)
EndFunc

There shouldn't be more than 1 button without text.

If you need more control, use UIAutomation.

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
 Share

  • Recently Browsing   0 members

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