| 1 |
joko |
1.1 |
# A sample context menu handler. |
| 2 |
|
|
# Adds a 'Hello from Python' menu entry to .py files. When clicked, a |
| 3 |
|
|
# simple message box is displayed. |
| 4 |
|
|
# |
| 5 |
|
|
# To demostrate: |
| 6 |
|
|
# * Execute this script to register the context menu. |
| 7 |
|
|
# * Open Windows Explorer, and browse to a directory with a .py file. |
| 8 |
|
|
# * Right-Click on a .py file - locate and click on 'Hello from Python' on |
| 9 |
|
|
# the context menu. |
| 10 |
|
|
|
| 11 |
joko |
1.4 |
# see also: http://www.codeproject.com/shell/ShellExtGuide2.asp |
| 12 |
|
|
|
| 13 |
joko |
1.1 |
import pythoncom |
| 14 |
|
|
from win32com.shell import shell, shellcon |
| 15 |
joko |
1.5 |
import win32gui, win32gui_struct |
| 16 |
joko |
1.1 |
import win32con |
| 17 |
joko |
1.4 |
import win32api, winerror |
| 18 |
joko |
1.1 |
|
| 19 |
|
|
IContextMenu_Methods = ["QueryContextMenu", "InvokeCommand", "GetCommandString"] |
| 20 |
|
|
IShellExtInit_Methods = ["Initialize"] |
| 21 |
|
|
|
| 22 |
|
|
class ShellExtension: |
| 23 |
|
|
_reg_progid_ = "Python.ShellExtension.ContextMenu" |
| 24 |
joko |
1.2 |
_reg_name_ = "MessClient" |
| 25 |
|
|
_reg_desc_ = "Mess Shell Extension" |
| 26 |
joko |
1.1 |
_reg_clsid_ = "{CED0336C-C9EE-4a7f-8D7F-C660393C381F}" |
| 27 |
|
|
_com_interfaces_ = [shell.IID_IShellExtInit, shell.IID_IContextMenu] |
| 28 |
|
|
_public_methods_ = IContextMenu_Methods + IShellExtInit_Methods |
| 29 |
|
|
|
| 30 |
|
|
def Initialize(self, folder, dataobj, hkey): |
| 31 |
|
|
print "Init", folder, dataobj, hkey |
| 32 |
|
|
self.dataobj = dataobj |
| 33 |
|
|
|
| 34 |
|
|
def QueryContextMenu(self, hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags): |
| 35 |
joko |
1.3 |
|
| 36 |
|
|
# debug |
| 37 |
joko |
1.1 |
print "QCM", hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags |
| 38 |
joko |
1.3 |
|
| 39 |
|
|
# query the items clicked on |
| 40 |
joko |
1.1 |
format_etc = win32con.CF_HDROP, None, 1, -1, pythoncom.TYMED_HGLOBAL |
| 41 |
|
|
sm = self.dataobj.GetData(format_etc) |
| 42 |
|
|
num_files = shell.DragQueryFile(sm.data_handle, -1) |
| 43 |
joko |
1.3 |
|
| 44 |
|
|
#if num_files>1: |
| 45 |
|
|
# msg = "&Hello from Python (with %d files selected)" % num_files |
| 46 |
|
|
#else: |
| 47 |
|
|
# fname = shell.DragQueryFile(sm.data_handle, 0) |
| 48 |
|
|
# msg = "&Hello from Python (with '%s' selected)" % fname |
| 49 |
|
|
self.files_selected = [] |
| 50 |
|
|
for i in range(0, num_files): |
| 51 |
|
|
fname = shell.DragQueryFile(sm.data_handle, i) |
| 52 |
|
|
if fname == 0: continue |
| 53 |
|
|
self.files_selected.append(fname) |
| 54 |
|
|
|
| 55 |
|
|
# build context menu(s) |
| 56 |
joko |
1.5 |
msg = "Add to A&rchive" |
| 57 |
joko |
1.1 |
idCmd = idCmdFirst |
| 58 |
|
|
items = [] |
| 59 |
|
|
if (uFlags & 0x000F) == shellcon.CMF_NORMAL: # Check == here, since CMF_NORMAL=0 |
| 60 |
|
|
print "CMF_NORMAL..." |
| 61 |
joko |
1.4 |
items.append(msg + " - normal") |
| 62 |
joko |
1.1 |
elif uFlags & shellcon.CMF_VERBSONLY: |
| 63 |
|
|
print "CMF_VERBSONLY..." |
| 64 |
|
|
items.append(msg + " - shortcut") |
| 65 |
|
|
elif uFlags & shellcon.CMF_EXPLORE: |
| 66 |
|
|
print "CMF_EXPLORE..." |
| 67 |
joko |
1.3 |
#items.append(msg + " - normal file, right-click in Explorer") |
| 68 |
|
|
items.append(msg) |
| 69 |
joko |
1.4 |
items.append("Named Shortcut &1") |
| 70 |
|
|
items.append("Named Shortcut &2") |
| 71 |
joko |
1.1 |
elif uFlags & CMF_DEFAULTONLY: |
| 72 |
|
|
print "CMF_DEFAULTONLY...\r\n" |
| 73 |
joko |
1.3 |
items.append("hello world - 1!") |
| 74 |
joko |
1.1 |
else: |
| 75 |
|
|
print "** unknown flags", uFlags |
| 76 |
joko |
1.3 |
items.append("hello world - 2!") |
| 77 |
joko |
1.4 |
|
| 78 |
joko |
1.5 |
# add seperator line |
| 79 |
joko |
1.1 |
win32gui.InsertMenu(hMenu, indexMenu, |
| 80 |
|
|
win32con.MF_SEPARATOR|win32con.MF_BYPOSITION, |
| 81 |
joko |
1.4 |
idCmd, None) |
| 82 |
joko |
1.1 |
indexMenu += 1 |
| 83 |
joko |
1.4 |
idCmd += 1 |
| 84 |
|
|
|
| 85 |
joko |
1.5 |
# create main menu entries |
| 86 |
joko |
1.1 |
for item in items: |
| 87 |
|
|
win32gui.InsertMenu(hMenu, indexMenu, |
| 88 |
|
|
win32con.MF_STRING|win32con.MF_BYPOSITION, |
| 89 |
|
|
idCmd, item) |
| 90 |
|
|
indexMenu += 1 |
| 91 |
|
|
idCmd += 1 |
| 92 |
joko |
1.5 |
|
| 93 |
|
|
# create submenu entries |
| 94 |
|
|
hSubmenu = win32gui.CreatePopupMenu() |
| 95 |
|
|
win32gui.InsertMenu(hSubmenu, 0, |
| 96 |
|
|
win32con.MF_BYPOSITION, |
| 97 |
|
|
idCmd, "&Settings...") |
| 98 |
|
|
idCmd += 1 |
| 99 |
|
|
|
| 100 |
|
|
# insert the submenu into a new container menu entry |
| 101 |
|
|
item, extras = win32gui_struct.PackMENUITEMINFO( |
| 102 |
|
|
wID = idCmd, |
| 103 |
|
|
hSubMenu = hSubmenu, |
| 104 |
|
|
text = "&Mess" |
| 105 |
|
|
) |
| 106 |
|
|
win32gui.InsertMenuItem(hMenu, indexMenu, 1, item) |
| 107 |
|
|
indexMenu += 1 |
| 108 |
|
|
idCmd += 1 |
| 109 |
joko |
1.1 |
|
| 110 |
joko |
1.5 |
# add seperator line |
| 111 |
joko |
1.1 |
win32gui.InsertMenu(hMenu, indexMenu, |
| 112 |
|
|
win32con.MF_SEPARATOR|win32con.MF_BYPOSITION, |
| 113 |
joko |
1.4 |
idCmd, None) |
| 114 |
joko |
1.1 |
indexMenu += 1 |
| 115 |
joko |
1.4 |
idCmd += 1 |
| 116 |
joko |
1.5 |
|
| 117 |
joko |
1.1 |
return idCmd-idCmdFirst # Must return number of menu items we added. |
| 118 |
|
|
|
| 119 |
joko |
1.4 |
def GetCommandString(self, cmdId, typ): |
| 120 |
|
|
#if cmdId == 0: |
| 121 |
|
|
# return "Hello from Python!!" |
| 122 |
|
|
#else: |
| 123 |
|
|
# return "abc" |
| 124 |
|
|
return self._reg_name_ + "_" + cmdId; |
| 125 |
|
|
|
| 126 |
joko |
1.1 |
def InvokeCommand(self, ci): |
| 127 |
|
|
mask, hwnd, verb, params, dir, nShow, hotkey, hicon = ci |
| 128 |
|
|
|
| 129 |
joko |
1.4 |
# If lpVerb really points to a string, ignore this function call and bail out. |
| 130 |
|
|
if win32api.HIWORD( verb ) != 0: |
| 131 |
|
|
return winerror.E_INVALIDARG |
| 132 |
|
|
|
| 133 |
|
|
function_id = win32api.LOWORD(verb) |
| 134 |
|
|
# TODO: establish mapping with building the context menus in "QueryContextMenu" |
| 135 |
|
|
if function_id == 1: |
| 136 |
joko |
1.5 |
method = "Add to archive" |
| 137 |
joko |
1.4 |
elif function_id == 2: |
| 138 |
|
|
method = "Shortcut 1" |
| 139 |
|
|
elif function_id == 3: |
| 140 |
|
|
method = "Shortcut 2" |
| 141 |
|
|
else: |
| 142 |
|
|
method = "Unknown" |
| 143 |
|
|
|
| 144 |
|
|
# do action(s) |
| 145 |
|
|
win32gui.MessageBox(hwnd, "\n".join(self.files_selected), method, win32con.MB_OK) |
| 146 |
joko |
1.1 |
|
| 147 |
joko |
1.2 |
|
| 148 |
joko |
1.1 |
def DllRegisterServer(): |
| 149 |
|
|
import _winreg |
| 150 |
joko |
1.2 |
|
| 151 |
|
|
# extended (via http://mail.python.org/pipermail/python-list/2003-December/198390.html) |
| 152 |
joko |
1.3 |
|
| 153 |
joko |
1.2 |
# for folders |
| 154 |
|
|
folder_key = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT, "Folder\\shellex") |
| 155 |
|
|
folder_subkey = _winreg.CreateKey(folder_key, "ContextMenuHandlers") |
| 156 |
|
|
folder_subkey2 = _winreg.CreateKey(folder_subkey, ShellExtension._reg_name_) |
| 157 |
|
|
_winreg.SetValueEx(folder_subkey2, None, 0, _winreg.REG_SZ, ShellExtension._reg_clsid_) |
| 158 |
|
|
|
| 159 |
|
|
# for files |
| 160 |
|
|
file_key = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT, "*\\shellex") |
| 161 |
|
|
file_subkey = _winreg.CreateKey(file_key, "ContextMenuHandlers") |
| 162 |
|
|
file_subkey2 = _winreg.CreateKey(file_subkey, ShellExtension._reg_name_) |
| 163 |
|
|
_winreg.SetValueEx(file_subkey2, None, 0, _winreg.REG_SZ, ShellExtension._reg_clsid_) |
| 164 |
|
|
|
| 165 |
joko |
1.1 |
print ShellExtension._reg_desc_, "registration complete." |
| 166 |
|
|
|
| 167 |
joko |
1.2 |
|
| 168 |
joko |
1.1 |
def DllUnregisterServer(): |
| 169 |
|
|
import _winreg |
| 170 |
joko |
1.2 |
|
| 171 |
|
|
# extended (via http://mail.python.org/pipermail/python-list/2003-December/198390.html) |
| 172 |
|
|
|
| 173 |
joko |
1.1 |
try: |
| 174 |
joko |
1.2 |
# for folders |
| 175 |
|
|
folder_key = _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT, |
| 176 |
|
|
"Folder\\shellex\\ContextMenuHandlers\\" + ShellExtension._reg_name_) |
| 177 |
|
|
# for files |
| 178 |
|
|
file_key = _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT, |
| 179 |
|
|
"*\\shellex\\ContextMenuHandlers\\" + ShellExtension._reg_name_) |
| 180 |
joko |
1.1 |
except WindowsError, details: |
| 181 |
|
|
import errno |
| 182 |
|
|
if details.errno != errno.ENOENT: |
| 183 |
|
|
raise |
| 184 |
|
|
print ShellExtension._reg_desc_, "unregistration complete." |
| 185 |
|
|
|
| 186 |
|
|
if __name__=='__main__': |
| 187 |
|
|
from win32com.server import register |
| 188 |
|
|
register.UseCommandLine(ShellExtension, |
| 189 |
|
|
finalize_register = DllRegisterServer, |
| 190 |
|
|
finalize_unregister = DllUnregisterServer) |