【问题标题】:How to use python to create url shortcut on mac?如何使用python在mac上创建url快捷方式?
【发布时间】:2021-11-24 09:11:42
【问题描述】:
window版本有答案。但不是 mac。
我已经搜索了谷歌,但没有合适的结果
假设我想在桌面上创建 google.com 的快捷方式,然后我们可以运行 python 函数
createShortcut("www.google.com","~/Desktop")
什么是函数体?
【问题讨论】:
标签:
python
python-3.x
macos
shortcut
desktop-shortcut
【解决方案1】:
您可以在函数内创建一个 macos .url 文件。其格式如下:
[InternetShortcut]
URL=http://www.yourweb.com/
IconIndex=0
这是一个示例实现:
import os
def createShortcut(url, destination):
# get home directory if ~ in destination
if '~' in destination:
destination = destination.replace('~', os.path.expanduser("~"))
# macos .url file format
text = '[InternetShortcut]\nURL=https://{}\nIconIndex=0'.format(url)
# write .url file to destination
with open(destination + 'my_shortcut.url', 'w') as fw:
fw.write(text)
return
createShortcut("www.google.com", '~/Desktop/')