18 |
|
|
19 |
class ShellExtension: |
class ShellExtension: |
20 |
_reg_progid_ = "Python.ShellExtension.ContextMenu" |
_reg_progid_ = "Python.ShellExtension.ContextMenu" |
21 |
_reg_desc_ = "Python Sample Shell Extension (context menu)" |
_reg_name_ = "MessClient" |
22 |
|
_reg_desc_ = "Mess Shell Extension" |
23 |
_reg_clsid_ = "{CED0336C-C9EE-4a7f-8D7F-C660393C381F}" |
_reg_clsid_ = "{CED0336C-C9EE-4a7f-8D7F-C660393C381F}" |
24 |
_com_interfaces_ = [shell.IID_IShellExtInit, shell.IID_IContextMenu] |
_com_interfaces_ = [shell.IID_IShellExtInit, shell.IID_IContextMenu] |
25 |
_public_methods_ = IContextMenu_Methods + IShellExtInit_Methods |
_public_methods_ = IContextMenu_Methods + IShellExtInit_Methods |
29 |
self.dataobj = dataobj |
self.dataobj = dataobj |
30 |
|
|
31 |
def QueryContextMenu(self, hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags): |
def QueryContextMenu(self, hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags): |
32 |
|
|
33 |
|
# debug |
34 |
print "QCM", hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags |
print "QCM", hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags |
35 |
# Query the items clicked on |
|
36 |
|
# query the items clicked on |
37 |
format_etc = win32con.CF_HDROP, None, 1, -1, pythoncom.TYMED_HGLOBAL |
format_etc = win32con.CF_HDROP, None, 1, -1, pythoncom.TYMED_HGLOBAL |
38 |
sm = self.dataobj.GetData(format_etc) |
sm = self.dataobj.GetData(format_etc) |
39 |
num_files = shell.DragQueryFile(sm.data_handle, -1) |
num_files = shell.DragQueryFile(sm.data_handle, -1) |
40 |
if num_files>1: |
|
41 |
msg = "&Hello from Python (with %d files selected)" % num_files |
#if num_files>1: |
42 |
else: |
# msg = "&Hello from Python (with %d files selected)" % num_files |
43 |
fname = shell.DragQueryFile(sm.data_handle, 0) |
#else: |
44 |
msg = "&Hello from Python (with '%s' selected)" % fname |
# fname = shell.DragQueryFile(sm.data_handle, 0) |
45 |
|
# msg = "&Hello from Python (with '%s' selected)" % fname |
46 |
|
self.files_selected = [] |
47 |
|
for i in range(0, num_files): |
48 |
|
fname = shell.DragQueryFile(sm.data_handle, i) |
49 |
|
if fname == 0: continue |
50 |
|
self.files_selected.append(fname) |
51 |
|
|
52 |
|
# build context menu(s) |
53 |
|
msg = "A&rchiveMe" |
54 |
idCmd = idCmdFirst |
idCmd = idCmdFirst |
55 |
items = [] |
items = [] |
56 |
if (uFlags & 0x000F) == shellcon.CMF_NORMAL: # Check == here, since CMF_NORMAL=0 |
if (uFlags & 0x000F) == shellcon.CMF_NORMAL: # Check == here, since CMF_NORMAL=0 |
61 |
items.append(msg + " - shortcut") |
items.append(msg + " - shortcut") |
62 |
elif uFlags & shellcon.CMF_EXPLORE: |
elif uFlags & shellcon.CMF_EXPLORE: |
63 |
print "CMF_EXPLORE..." |
print "CMF_EXPLORE..." |
64 |
items.append(msg + " - normal file, right-click in Explorer") |
#items.append(msg + " - normal file, right-click in Explorer") |
65 |
|
items.append(msg) |
66 |
elif uFlags & CMF_DEFAULTONLY: |
elif uFlags & CMF_DEFAULTONLY: |
67 |
print "CMF_DEFAULTONLY...\r\n" |
print "CMF_DEFAULTONLY...\r\n" |
68 |
|
items.append("hello world - 1!") |
69 |
else: |
else: |
70 |
print "** unknown flags", uFlags |
print "** unknown flags", uFlags |
71 |
|
items.append("hello world - 2!") |
72 |
win32gui.InsertMenu(hMenu, indexMenu, |
win32gui.InsertMenu(hMenu, indexMenu, |
73 |
win32con.MF_SEPARATOR|win32con.MF_BYPOSITION, |
win32con.MF_SEPARATOR|win32con.MF_BYPOSITION, |
74 |
0, None) |
0, None) |
88 |
|
|
89 |
def InvokeCommand(self, ci): |
def InvokeCommand(self, ci): |
90 |
mask, hwnd, verb, params, dir, nShow, hotkey, hicon = ci |
mask, hwnd, verb, params, dir, nShow, hotkey, hicon = ci |
91 |
win32gui.MessageBox(hwnd, "Hello", "Wow", win32con.MB_OK) |
win32gui.MessageBox(hwnd, "\n".join(self.files_selected), "ArchiveMe", win32con.MB_OK) |
92 |
|
|
93 |
def GetCommandString(self, cmd, typ): |
def GetCommandString(self, cmd, typ): |
94 |
return "Hello from Python!!" |
return "Hello from Python!!" |
95 |
|
|
96 |
|
|
97 |
def DllRegisterServer(): |
def DllRegisterServer(): |
98 |
import _winreg |
import _winreg |
99 |
key = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT, |
|
100 |
"Python.File\\shellex") |
# extended (via http://mail.python.org/pipermail/python-list/2003-December/198390.html) |
101 |
subkey = _winreg.CreateKey(key, "ContextMenuHandlers") |
|
102 |
subkey2 = _winreg.CreateKey(subkey, "PythonSample") |
# for folders |
103 |
_winreg.SetValueEx(subkey2, None, 0, _winreg.REG_SZ, ShellExtension._reg_clsid_) |
folder_key = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT, "Folder\\shellex") |
104 |
|
folder_subkey = _winreg.CreateKey(folder_key, "ContextMenuHandlers") |
105 |
|
folder_subkey2 = _winreg.CreateKey(folder_subkey, ShellExtension._reg_name_) |
106 |
|
_winreg.SetValueEx(folder_subkey2, None, 0, _winreg.REG_SZ, ShellExtension._reg_clsid_) |
107 |
|
|
108 |
|
# for files |
109 |
|
file_key = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT, "*\\shellex") |
110 |
|
file_subkey = _winreg.CreateKey(file_key, "ContextMenuHandlers") |
111 |
|
file_subkey2 = _winreg.CreateKey(file_subkey, ShellExtension._reg_name_) |
112 |
|
_winreg.SetValueEx(file_subkey2, None, 0, _winreg.REG_SZ, ShellExtension._reg_clsid_) |
113 |
|
|
114 |
print ShellExtension._reg_desc_, "registration complete." |
print ShellExtension._reg_desc_, "registration complete." |
115 |
|
|
116 |
|
|
117 |
def DllUnregisterServer(): |
def DllUnregisterServer(): |
118 |
import _winreg |
import _winreg |
119 |
|
|
120 |
|
# extended (via http://mail.python.org/pipermail/python-list/2003-December/198390.html) |
121 |
|
|
122 |
try: |
try: |
123 |
key = _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT, |
# for folders |
124 |
"Python.File\\shellex\\ContextMenuHandlers\\PythonSample") |
folder_key = _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT, |
125 |
|
"Folder\\shellex\\ContextMenuHandlers\\" + ShellExtension._reg_name_) |
126 |
|
# for files |
127 |
|
file_key = _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT, |
128 |
|
"*\\shellex\\ContextMenuHandlers\\" + ShellExtension._reg_name_) |
129 |
except WindowsError, details: |
except WindowsError, details: |
130 |
import errno |
import errno |
131 |
if details.errno != errno.ENOENT: |
if details.errno != errno.ENOENT: |