[英]Create shortcut to file in native python
我只是嘗試創建一個小的 Python 腳本,該腳本首先安裝某個 package (PyMOL),基於 Python 安裝在用戶系統上的程序版本的快捷方式。
我要創建快捷方式的文件位於 %APPDATA%/python/pythonVERSION/scripts/pymol.exe 中。
第一步完美運行,並按預期安裝 package。 然而,第二步在原生 Python 中變得相當困難。
到目前為止,我能找到的所有解決方案都使用以下軟件包: win32com, pythoncom, swinlnk,
...
我不想安裝我只需要一次的軟件包,以便在我正在嘗試安裝 PyMOL 的每個人的 PC 上創建快捷方式。
那么有沒有辦法在本機 Python 中創建文件的快捷方式,而無需安裝任何類型的第 3 方 package?
只是為了展示一些解決方案,我已經發現:
使用 Python 在 Windows 7 中創建快捷方式文件
https://www.blog.pythonlibrary.org/2010/01/23/using-python-to-create-shortcuts/
跟進,正如我所承諾的:
大多數代碼來自: 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',
]))
)
```
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.