Jump to content

Mouse click on item in Windows system tray.


rasim
 Share

Recommended Posts

Hi! Probably many peoples face a problem, when there was need to click on item in Windows system tray. I found (probably) a little solution for this problem. :)

#NoTrayIcon
#Include <GuiToolBar.au3>

_SysTray_ClickItem("AIMP2", "right", 1)

If @error Then MsgBox(48, "Failure", "Required item not found")

;=========# _SysTray_ClickItem #======================================================
;
;Function Name:    _SysTray_ClickItem()
;Description:      Click on item in Windows system tray by any substring in the title
;Parameters:       $iTitle - The title of the item in Windows system tray (you can see the title of the item when mouse cursor hovered on item).
;                  $iButton - [optional] The button to click, "left" or "right". Default is the left button.
;                  $iClick - [optional] The number of times to click the mouse. Default is 1
;                  $sMove = [optional] True = Mouse will be moved, False (default) = Mouse will not be moved
;                  $iSpeed = [optional] Mouse movement speed
;Return Value(s):  Success - Returns 1
;                  Failure - Returns 0 and sets @error to 1 if required item not found
;Requirement(s):   AutoIt 3.2.10.0 and above
;Autor(s):        R.Gilman (a.k.a rasim); Siao (Thanks for idea)
;
;====================================================================================
Func _SysTray_ClickItem($iTitle, $iButton = "left", $iClick = 1, $sMove = False, $iSpeed = 1)
    Local $hToolbar, $iButCount, $aRect, $hButton, $cID, $i
    
    $hToolbar = ControlGetHandle("[Class:Shell_TrayWnd]", "", "[Class:ToolbarWindow32;Instance:1]")
    If @error Then
        Return SetError(1, 0, 0)
    EndIf
    
    $iButCount = _GUICtrlToolbar_ButtonCount($hToolbar)
    If $iButCount = 0 Then
        Return SetError(1, 0, 0)
    EndIf
    
    $hButton = ControlGetHandle("[Class:Shell_TrayWnd]", "", "Button2")
    If $hButton <> "" Then ControlClick("[Class:Shell_TrayWnd]", "", "Button2")
    
    For $i = 0 To $iButCount - 1
        $cID = _GUICtrlToolbar_IndexToCommand($hToolBar, $i)
        If StringInStr(_GUICtrlToolbar_GetButtonText($hToolBar, $cID), $iTitle) Then
            _GUICtrlToolbar_ClickButton($hToolbar, $cID, $iButton, $sMove, $iClick, $iSpeed)
            Return 1
        EndIf
    Next
    Return SetError(1, 0, 0)
EndFunc
Edited by rasim
Link to comment
Share on other sites

Rasim,

I notice that you never use the variable $cID.

Should calls like _GUICtrlToolbar_GetButtonText($hToolBar, $i)

be using $i or $cID ?

Also, it doesn't work for me. I put a MsgBox inside the loop to each at each value of _GUICtrlToolbar_GetButtonText($hToolBar, $i), and it seems to be missing some of them.

ToyleY

For $i = 0 To $iButCount - 1
        $cID = _GUICtrlToolbar_IndexToCommand($hToolBar, $i)
        If StringInStr(_GUICtrlToolbar_GetButtonText($hToolBar, $i), $iTitle) Then
            $aRect = _GUICtrlToolbar_GetButtonRect($hToolBar, $i)
            ControlClick("[Class:Shell_TrayWnd]", "", "ToolbarWindow321", $iButton, $iClick, $aRect[0], 5)
            $aRect = 0
            Return 1
        EndIf
    Next
    MsgBox(48, "Fail", "Required item not found")
EndFunc
Link to comment
Share on other sites

  • 5 months later...

Nice script. Few things -

It doesn't work for the special notification icon in Vista, ie volume, network, clock, power.

It doesn't work right if you auto-hide the systray icons. You have to give the little button a click to show all icons, then it works perfectly.

Good job!

Link to comment
Share on other sites

tig8drag

but haw can I access to other options in a system tray icon ??

This function don`t works with the context menu of any applications, if you need this, that just modify this example :P

wraithdu

It doesn't work for the special notification icon in Vista, ie volume, network, clock, power.

In Windows XP this function works fine.

It doesn't work right if you auto-hide the systray icons. You have to give the little button a click to show all icons, then it works perfectly.

Fixed, updated first post. :P
Link to comment
Share on other sites

In Windows XP this function works fine.

I know that, that's why I stated it doesn't work in Vista. This is because in Vista those four notification icons are part of a different toolbar window, INSTANCE:2. I supposed you could add a check for @OSVersion == "WIN_VISTA" and if so, also loop through ToolbarWindow322.

I'm not on an XP machine to check, but under Vista the systray button is Button1 (INSTANCE:1). And you might also want to add a Sleep(250) or so after the click on the button to show all the icons. It takes a second for the thing to expand all the way.

Keep up the good work!

Edited by wraithdu
Link to comment
Share on other sites

  • 2 weeks later...

Thanks! That's exactly what I was looking for. ;)

But could you explain it? I have little knowledge about functions. I don't know what your code does. For example, this line: $iTitle, $iButton = "left", $iClick = 2)

Local $hToolbar, $iButCount, $aRect, $hButton, $cID, $i.

Link to comment
Share on other sites

Rasim,

After I did more test, I've found some issues while try to set the Right Click to the SuperAntiSpyware tray icon

Here are the those different results:

1. no problem

2. it only bring up the task bar right click menu

3. it bring up both menu of SuperAntiSpyware and task bar

fyi, they all three got the @Error 0 and the Left Click seems doing fine.

Link to comment
Share on other sites

Rasim, I'll repeat a question I already asked you before:

When the taskbar is unlocked and dragged down out of sight, then the system tray is not visible right? In this case, your script gives NO error but FAILS to work correctly! So in order for the script to work correctly and click the system tray icon, the taskbar should be visible.

My question is, is there some way to make the script work even if the taskbar is not visible on-screen?

Thanks for the script!

- Andrew

Link to comment
Share on other sites

  • 1 month later...
  • 1 month later...

I have tried this code under XP and it locates the button I want fine but I only get the taskbar menu not the tray item right-click menu. Any more suggestions?

I will suggest an Idea

1. Desktop image capture

2. search down right for you pattern

3. go there

p.s. for some programs it works with up and down arrow, but not for all

Edited by usmiv4o

I have nothing to be proud: I am Bulgarian :~But there is no better place than 127.0.0.1Tutorial for newbies

Link to comment
Share on other sites

  • 3 weeks later...

this is a great script, how can you make it trigger on an event. I have a need where my bluetooth icon needs to be clicked to bring a screen to the front, but its the icon itself that "moves" when the hidden windows needs to be accessed.

 

Spoiler

WinSizer 2.1 (01/04/2017) - Download - [ Windows Layout Manager ]
Folder+Program (12/23/2016) - Download - [ USB Shortcut Creator ]

 

Link to comment
Share on other sites

Well what happens is the bluetooth icon moves from "some" position in the tray to the far left. it also produces a "ballon tip" but I have ballon tips removed. when you single left click on the bluetooth icon it will produce the window needing attention.

 

Spoiler

WinSizer 2.1 (01/04/2017) - Download - [ Windows Layout Manager ]
Folder+Program (12/23/2016) - Download - [ USB Shortcut Creator ]

 

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...