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 |
joko |
1.6 |
def Initialize(self, pidlFolder, dataobj, hkey): |
31 |
|
|
print "Init", pidlFolder, dataobj, hkey |
32 |
joko |
1.1 |
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 |
joko |
1.6 |
# clicking on a directory background leaves us with dataobj == None |
40 |
|
|
if self.dataobj: |
41 |
|
|
|
42 |
|
|
# query the items clicked on |
43 |
|
|
format_etc = win32con.CF_HDROP, None, 1, -1, pythoncom.TYMED_HGLOBAL |
44 |
|
|
sm = self.dataobj.GetData(format_etc) |
45 |
|
|
|
46 |
|
|
# how many items are selected |
47 |
|
|
num_files = shell.DragQueryFile(sm.data_handle, -1) |
48 |
|
|
|
49 |
|
|
# grab all items |
50 |
|
|
self.files_selected = [] |
51 |
|
|
for i in range(0, num_files): |
52 |
|
|
fname = shell.DragQueryFile(sm.data_handle, i) |
53 |
|
|
if fname == 0: continue |
54 |
|
|
self.files_selected.append(fname) |
55 |
|
|
|
56 |
|
|
|
57 |
|
|
# calculate items to be added to context menu |
58 |
joko |
1.1 |
items = [] |
59 |
joko |
1.6 |
|
60 |
|
|
# CMF_NORMAL: e.g. files on desktop |
61 |
|
|
# CMF_VERBSONLY: e.g. shortcuts |
62 |
|
|
# CMF_EXPLORE: regular files/folders via explorer |
63 |
|
|
|
64 |
|
|
# Check == here, since CMF_NORMAL=0 |
65 |
|
|
if ((uFlags & 0x000F) == shellcon.CMF_NORMAL) or (uFlags & shellcon.CMF_VERBSONLY) or (uFlags & shellcon.CMF_EXPLORE): |
66 |
|
|
if self.dataobj: |
67 |
|
|
items.append("Add to A&rchive") |
68 |
|
|
items.append("Named Shortcut &1") |
69 |
|
|
items.append("Named Shortcut &2") |
70 |
joko |
1.1 |
elif uFlags & CMF_DEFAULTONLY: |
71 |
|
|
print "CMF_DEFAULTONLY...\r\n" |
72 |
joko |
1.3 |
items.append("hello world - 1!") |
73 |
joko |
1.1 |
else: |
74 |
|
|
print "** unknown flags", uFlags |
75 |
joko |
1.3 |
items.append("hello world - 2!") |
76 |
joko |
1.6 |
|
77 |
|
|
|
78 |
|
|
# build context menu(s) |
79 |
|
|
idCmd = idCmdFirst |
80 |
|
|
|
81 |
joko |
1.5 |
# add seperator line |
82 |
joko |
1.1 |
win32gui.InsertMenu(hMenu, indexMenu, |
83 |
|
|
win32con.MF_SEPARATOR|win32con.MF_BYPOSITION, |
84 |
joko |
1.4 |
idCmd, None) |
85 |
joko |
1.1 |
indexMenu += 1 |
86 |
joko |
1.4 |
idCmd += 1 |
87 |
|
|
|
88 |
joko |
1.5 |
# create main menu entries |
89 |
joko |
1.1 |
for item in items: |
90 |
|
|
win32gui.InsertMenu(hMenu, indexMenu, |
91 |
|
|
win32con.MF_STRING|win32con.MF_BYPOSITION, |
92 |
|
|
idCmd, item) |
93 |
|
|
indexMenu += 1 |
94 |
|
|
idCmd += 1 |
95 |
joko |
1.5 |
|
96 |
|
|
# create submenu entries |
97 |
|
|
hSubmenu = win32gui.CreatePopupMenu() |
98 |
|
|
win32gui.InsertMenu(hSubmenu, 0, |
99 |
|
|
win32con.MF_BYPOSITION, |
100 |
|
|
idCmd, "&Settings...") |
101 |
|
|
idCmd += 1 |
102 |
|
|
|
103 |
|
|
# insert the submenu into a new container menu entry |
104 |
|
|
item, extras = win32gui_struct.PackMENUITEMINFO( |
105 |
|
|
wID = idCmd, |
106 |
|
|
hSubMenu = hSubmenu, |
107 |
|
|
text = "&Mess" |
108 |
|
|
) |
109 |
|
|
win32gui.InsertMenuItem(hMenu, indexMenu, 1, item) |
110 |
|
|
indexMenu += 1 |
111 |
|
|
idCmd += 1 |
112 |
joko |
1.1 |
|
113 |
joko |
1.5 |
# add seperator line |
114 |
joko |
1.1 |
win32gui.InsertMenu(hMenu, indexMenu, |
115 |
|
|
win32con.MF_SEPARATOR|win32con.MF_BYPOSITION, |
116 |
joko |
1.4 |
idCmd, None) |
117 |
joko |
1.1 |
indexMenu += 1 |
118 |
joko |
1.4 |
idCmd += 1 |
119 |
joko |
1.5 |
|
120 |
joko |
1.1 |
return idCmd-idCmdFirst # Must return number of menu items we added. |
121 |
|
|
|
122 |
joko |
1.4 |
def GetCommandString(self, cmdId, typ): |
123 |
|
|
#if cmdId == 0: |
124 |
|
|
# return "Hello from Python!!" |
125 |
|
|
#else: |
126 |
|
|
# return "abc" |
127 |
|
|
return self._reg_name_ + "_" + cmdId; |
128 |
|
|
|
129 |
joko |
1.1 |
def InvokeCommand(self, ci): |
130 |
|
|
mask, hwnd, verb, params, dir, nShow, hotkey, hicon = ci |
131 |
|
|
|
132 |
joko |
1.4 |
# If lpVerb really points to a string, ignore this function call and bail out. |
133 |
|
|
if win32api.HIWORD( verb ) != 0: |
134 |
|
|
return winerror.E_INVALIDARG |
135 |
|
|
|
136 |
|
|
function_id = win32api.LOWORD(verb) |
137 |
|
|
# TODO: establish mapping with building the context menus in "QueryContextMenu" |
138 |
|
|
if function_id == 1: |
139 |
joko |
1.5 |
method = "Add to archive" |
140 |
joko |
1.4 |
elif function_id == 2: |
141 |
|
|
method = "Shortcut 1" |
142 |
|
|
elif function_id == 3: |
143 |
|
|
method = "Shortcut 2" |
144 |
|
|
else: |
145 |
|
|
method = "Unknown" |
146 |
joko |
1.6 |
|
147 |
joko |
1.4 |
# do action(s) |
148 |
joko |
1.6 |
if hasattr(self, 'files_selected'): |
149 |
|
|
win32gui.MessageBox(hwnd, "\n".join(self.files_selected), method, win32con.MB_OK) |
150 |
|
|
else: |
151 |
|
|
win32gui.MessageBox(hwnd, "No selection!", method, win32con.MB_OK) |
152 |
joko |
1.1 |
|
153 |
joko |
1.2 |
|
154 |
joko |
1.1 |
def DllRegisterServer(): |
155 |
|
|
import _winreg |
156 |
joko |
1.2 |
|
157 |
|
|
# extended (via http://mail.python.org/pipermail/python-list/2003-December/198390.html) |
158 |
joko |
1.3 |
|
159 |
joko |
1.2 |
# for folders |
160 |
joko |
1.6 |
background_key = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT, "Directory\\Background\\shellex") |
161 |
|
|
background_subkey = _winreg.CreateKey(background_key, "ContextMenuHandlers") |
162 |
|
|
background_subkey2 = _winreg.CreateKey(background_subkey, ShellExtension._reg_name_) |
163 |
|
|
_winreg.SetValueEx(background_subkey2, None, 0, _winreg.REG_SZ, ShellExtension._reg_clsid_) |
164 |
|
|
|
165 |
|
|
# for folders |
166 |
joko |
1.2 |
folder_key = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT, "Folder\\shellex") |
167 |
|
|
folder_subkey = _winreg.CreateKey(folder_key, "ContextMenuHandlers") |
168 |
|
|
folder_subkey2 = _winreg.CreateKey(folder_subkey, ShellExtension._reg_name_) |
169 |
|
|
_winreg.SetValueEx(folder_subkey2, None, 0, _winreg.REG_SZ, ShellExtension._reg_clsid_) |
170 |
|
|
|
171 |
|
|
# for files |
172 |
|
|
file_key = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT, "*\\shellex") |
173 |
|
|
file_subkey = _winreg.CreateKey(file_key, "ContextMenuHandlers") |
174 |
|
|
file_subkey2 = _winreg.CreateKey(file_subkey, ShellExtension._reg_name_) |
175 |
|
|
_winreg.SetValueEx(file_subkey2, None, 0, _winreg.REG_SZ, ShellExtension._reg_clsid_) |
176 |
|
|
|
177 |
joko |
1.1 |
print ShellExtension._reg_desc_, "registration complete." |
178 |
|
|
|
179 |
joko |
1.2 |
|
180 |
joko |
1.1 |
def DllUnregisterServer(): |
181 |
|
|
import _winreg |
182 |
joko |
1.2 |
|
183 |
|
|
# extended (via http://mail.python.org/pipermail/python-list/2003-December/198390.html) |
184 |
|
|
|
185 |
joko |
1.1 |
try: |
186 |
joko |
1.2 |
# for folders |
187 |
|
|
folder_key = _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT, |
188 |
|
|
"Folder\\shellex\\ContextMenuHandlers\\" + ShellExtension._reg_name_) |
189 |
|
|
# for files |
190 |
|
|
file_key = _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT, |
191 |
|
|
"*\\shellex\\ContextMenuHandlers\\" + ShellExtension._reg_name_) |
192 |
joko |
1.1 |
except WindowsError, details: |
193 |
|
|
import errno |
194 |
|
|
if details.errno != errno.ENOENT: |
195 |
|
|
raise |
196 |
|
|
print ShellExtension._reg_desc_, "unregistration complete." |
197 |
|
|
|
198 |
|
|
if __name__=='__main__': |
199 |
|
|
from win32com.server import register |
200 |
|
|
register.UseCommandLine(ShellExtension, |
201 |
|
|
finalize_register = DllRegisterServer, |
202 |
|
|
finalize_unregister = DllUnregisterServer) |