Vishnu Indusekharan
Vishnu Indusekharan

Reputation: 41

How to perform a context menu action on a file using python 3

How can i do a context menu action on a particular file?
I managed to open the explorer and get the list of files through python using pywinauto.

On that file I need to perform a context menu action, is it possible through pywinauto?

import pywinauto

path = "C:\\Users\\Vishnu\\Desktop\\DM-test\\"

pywinauto.Application().Start(r'explorer.exe')
explorer = pywinauto.Application().Connect(path='explorer.exe')
NewWindow = explorer.Window_(top_level_only=True, active_only=True,  class_name='CabinetWClass')
NewWindow.AddressBandRoot.ClickInput()
NewWindow.TypeKeys(path+'{ENTER}', with_spaces=True, set_foreground=False)

The code above will open the explorer and navigate to the dir. This is the Context menu action required on the file:
this is the Context menu action required on the file

I managed to find the reg value and changed my code to pass that action to the file, It works perfect!!

pywinauto.Application().start(r'"C:\Program Files (x86)\Qualcomm\QCAT 6.x\Bin\QCAT.exe" -txt "{}"'.format(fileName))

Upvotes: 2

Views: 2009

Answers (1)

Vasily Ryabov
Vasily Ryabov

Reputation: 9991

Arrgh! Nobody reads the docs... The example is provided in the main Readme: MS UI Automation Example. For your case it should look like that:

# no need to type the path, explorer.exe has a cmd param for that
pywinauto.Application().start(r'explorer.exe "{}"'.format(path))

# backend is important!!!
app = Application(backend="uia").connect(path="explorer.exe")
NewWindow = explorer.Window_(top_level_only=True, active_only=True,  class_name='CabinetWClass')

file_item = NewWindow.ItemsView.get_item('dmlog20180517-121505slot0.dlf')
file_item.right_click_input()
app.ContextMenu["Convert to QCAT Text"].invoke()

# further actions depend on a process / dialog started...

More details about backends: Getting Started Guide.

Upvotes: 4

Related Questions