如何使用python在mac上创建url快捷方式?

Posted

技术标签:

【中文标题】如何使用python在mac上创建url快捷方式?【英文标题】:How to use python to create url shortcut on mac? 【发布时间】:2021-11-24 09:11:42 【问题描述】:

window版本有答案。但不是 mac。

我已经搜索了谷歌,但没有合适的结果

假设我想在桌面上创建 google.com 的快捷方式,然后我们可以运行 python 函数

createShortcut("www.google.com","~/Desktop")

什么是函数体?

【问题讨论】:

我认为您可以将whatever.url 文件作为文本编写,格式如下:superuser.com/a/689444/1161705 【参考方案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/')

【讨论】:

以上是关于如何使用python在mac上创建url快捷方式?的主要内容,如果未能解决你的问题,请参考以下文章

Python 操作Redis

python爬虫入门----- 阿里巴巴供应商爬虫

Python词典设置默认值小技巧

《python学习手册(第4版)》pdf

Django settings.py 的media路径设置

Python中的赋值,浅拷贝和深拷贝的区别