In pywinauto, how can I right click my app icon in the system tray to open the context menu?

3.8k Views Asked by At

How can i right click the app's icon in the systemtray and select one of the popup menuitem using pywinauto?

I have my app on the Windows Desktop systemtray,which can't be loaded using the .exe file. So i have to right click on the systemtray icon and select one of the popup menu item in order to get the app's GUI.I was trying to achieve this using pywinauto using python 64 bit.

Here is my code.

app = Application(backend="uia").connect(path="explorer")
sys_tray = app.window(class_name="Shell_TrayWnd")
loc = sys_tray.child_window(title='App name').click()

This is changing the mouse position to the required App's icon,but its not right clicking on that and i want to select one menu item from that pop up also. how can i get this?

2

There are 2 best solutions below

17
On BEST ANSWER

There is method .click_input(button="right") which moves real cursor and performs real click. In your case it would look so (on Windows 10 version 1803):

#from __future__ import print_function
from pywinauto import Desktop

d = Desktop(backend='uia')
#d.Taskbar.dump_tree()
main_tray_toolbar = d.Taskbar.child_window(title="User Promoted Notification Area", control_type="ToolBar")
#print(main_tray_toolbar.texts())

icon = main_tray_toolbar.child_window(title_re="Cisco AnyConnect Secure Mobility Client.*", control_type="Button")
icon.click_input(button="right")

#d.ContextMenu.dump_tree()
d.ContextMenu.wait('visible', timeout=10) # flexibly wait up to 10 sec.
d.ContextMenu.child_window(title="About", control_type="MenuItem").invoke()

Helpful debug prints are commented (all .child_window specifications have been just copied from dump_tree() output). There is also method d.windows() that is available for master branch only (pywinauto 0.6.6 is coming in nearest 2 weeks).

0
On

Comment on Vasily's answer: because the names of the windows and toolbars are localized, the code will not work on systems whose locale is not English. I got this to work on a French Windows 10 system by replacing

main_tray_toolbar = d.Taskbar.child_window(title="User Promoted Notification Area", control_type="ToolBar")

with

main_tray_toolbar = d.window(class_name='Shell_TrayWnd').child_window(class_name='ToolbarWindow32', control_type="ToolBar")