跟进,正如我所承诺的:
大多数代码来自:https ://github.com/bristi/swinlnk/blob/e4bccd2be6e3e6fd501c627aead5602b132243fd/swinlnk/swinlnk.py
我只是简化了它,以适应我的需要。只安装模块可能会更容易,但现在它可以了,我需要它来做。它在当前用户的桌面上创建一个指向 PyMOL 默认安装路径的快捷方式,在我的例子中是 %APPDATA%/Python/PythonVER/Scripts/pymol.exe。
在检查快捷方式是否指向文件、文件夹或网络共享方面,代码大部分已被简化,因此仅当您想在本地驱动器上创建文件的快捷方式时才有效。
def ascii2hex(ascii_string):
data = [format(ord(x), '02x') for x in ascii_string]
datastring = ''.join(data)
return datastring
def convert_clsid_to_data(clsid):
slices = [
(6, 2),
(4, 2),
(2, 2),
(0, 2),
(11, 2),
(9, 2),
(16, 2),
(14, 2),
(19, 4),
(24, 12),
]
data = [clsid[x:x + y] for x, y in slices]
datastring = ''.join(data)
return datastring
def gen_idlist(id_item):
id_item_len = len(id_item) * 2
item_size = format(int(id_item_len / 4) + 2, '04x')
slices = [
(2, 2),
(0, 2),
]
data = [item_size[x:x + y] for x, y in slices]
datastring = ''.join(data) + id_item
return datastring
def create_desktop_shortcut(package):
package = str(package).split('-cp')
version = package[-2]
convert_clsid_to_data(
"20d04fe0-3aea-1069-a2d8-08002b30309d"
)
PREFIX_LOCAL_ROOT = '2f'
PREFIX_FILE = '320000000000000000000000'
item_data = '1f50' + 'e04fd020ea3a6910a2d808002b30309d'
appdata = Path.home()
appdata = appdata / "AppData/Roaming"
if len(version) == 2:
pymol_path = 'Python/Python' + str(version) + '/Scripts/pymol.exe'
p = PureWindowsPath(appdata / pymol_path)
else:
print('NEW VERSION OF PYTHON, please implement shortcut creation.')
exit
target_root = p.drive
if len(p.parts) > 1:
target_leaf = str(p)[len(p.drive)+1:]
type_target = "file"
file_attributes = F'20000000'
target_root = ascii2hex(target_root)
target_root = target_root + ('00' * 21)
target_leaf = ascii2hex(target_leaf)
prefix_root = '2f'
END_OF_STRING = '00'
prefix_of_target = '320000000000000000000000'
idlist_items = ''.join([
gen_idlist(item_data),
gen_idlist(prefix_root + target_root + END_OF_STRING),
gen_idlist(
prefix_of_target + target_leaf + END_OF_STRING
),
])
idlist = gen_idlist(idlist_items)
pymol_desktop_shortcut = Path.home() / 'Desktop/PyMOL.lnk'
if pymol_desktop_shortcut.is_file():
exit('Shortcut already exists. Exiting.')
with open(pymol_desktop_shortcut, 'wb') as fout:
fout.write(
binascii.unhexlify(''.join([
'4c000000',
'0114020000000000c000000000000046',
'01010000',
'20000000',
'0000000000000000',
'0000000000000000',
'0000000000000000',
'00000000',
'00000000',
'01000000',
'0000',
'0000',
'00000000',
'00000000',
idlist,
'0000',
]))
)
```