Trying and failing to invoke context menu in windows

0

in windows 10 I am trying to invoke context menu commands like "Share" and "Edit with Photos" but I am not having any luck. My code ends with the error:

$ python context.py
Traceback (most recent call last):
  File "context.py", line 41, in <module>
    CM.InvokeCommand(CI)
pywintypes.com_error: (-2147023741, 'No application is associated with the specified file for this operation.', None, None)

Any ideas how to fix this? I am by no means a win32 expert by the way. My current code is:

from win32com.shell import shell, shellcon
import win32gui
import win32con

desktop_folder = shell.SHGetDesktopFolder()
if not desktop_folder:
    raise Exception('Failed to get desktop folder')

file_path = 'C:\\Projects\\Python'
file_name = 'tutankhamun.jpg'

hwnd = win32gui.GetForegroundWindow() 

eaten, parent_pidl, attr = desktop_folder.ParseDisplayName(hwnd, None, file_path)
parent_folder = desktop_folder.BindToObject(parent_pidl, None, shell.IID_IShellFolder)

if file_name:
    eaten, pidl, attr = parent_folder.ParseDisplayName(hwnd, None, file_name)
    i, CM = parent_folder.GetUIObjectOf(hwnd, [pidl], shell.IID_IContextMenu, 0)
else:
    i, CM = desktop_folder.GetUIObjectOf(hwnd, [parent_pidl], shell.IID_IContextMenu, 0)

if not CM:
    raise Exception('Unable to get context menu')
else:
    CI = (0, #mask
            hwnd, #hwnd
            'Edit with Photos', #verb
            '', #Parameters
            file_path, #Directory
            win32con.SW_SHOWNORMAL, #Show (SW_HIDE)
            0, #Hotkey
            None #Icon
    )
    hMenu = win32gui.CreatePopupMenu() 
    MIN_SHELL_ID = 1 
    MAX_SHELL_ID = 30000 
    # Flags |= 0x00000080; #to show the extended context menu. 
    mask, hwnd, verb, params, dir, nShow, hotkey, hicon = CI
    CM.QueryContextMenu(hMenu, 0, MIN_SHELL_ID, MAX_SHELL_ID, shellcon.CMF_EXPLORE) 
    CM.InvokeCommand(CI)
python
asked on Stack Overflow Jun 5, 2019 by daibatzu • edited Jun 5, 2019 by daibatzu

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0