在原生python中创建文件的快捷方式



我刚刚尝试创建一个小型Python脚本,该脚本首先基于系统上安装的Python版本安装某个包(PyMOL(,然后在用户桌面上创建该程序的快捷方式。

我要创建快捷方式的文件位于%APPDATA%/python/pythonVERSION/scripts/pymol.exe.中

第一步工作完美,并按预期安装软件包。然而,第二步在原生Python中相当困难。

到目前为止,我能找到的所有解决方案都使用以下软件包:win32com, pythoncom, swinlnk,。。。

我不想安装那些我只需要一次就可以在每个人的电脑上创建快捷方式的软件包,我正在尝试安装PyMOL。

那么,有没有一种方法可以在本地Python中创建文件的快捷方式,而不必安装任何第三方软件包?

只是为了展示一些解决方案,我已经发现:

使用Python 在Windows 7中创建快捷方式文件

https://www.blog.pythonlibrary.org/2010/01/23/using-python-to-create-shortcuts/

如何在Windows上创建文件夹的快捷方式?

使用Python 3.7.1 在Windows 10中创建快捷方式文件

Python,创建具有两个路径和参数的快捷方式

正如我所承诺的那样:

大多数代码来自:https://github.com/bristi/swinlnk/blob/e4bccd2be6e3e6fd501c627aead5602b132243fd/swinlnk/swinlnk.py

我只是简化了它,以满足我的需要。它可能会更容易,只是安装模块,但现在它做到了,我需要它做什么。它在当前用户的桌面上创建了一个到PyMOL默认安装路径的快捷方式,在我的情况下是%APPDATA%/Python/PythonVER/Scripts/PyMOL.exe.

在检查快捷方式是指向文件、文件夹还是网络共享方面,代码大多已经简化,因此只有当您想在本地驱动器上创建指向file的快捷方式时,这才有效。

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',
]))
)
```

最新更新