用Python两种方法创建带参数运行的快捷方式,一键创建桌面和开始菜单快捷方式和部署自启动

需求背景

在网上很容易就能找到用Python创建快捷方式的方法,但是想要设置能够带参数运行的快捷方式却遇到了困难。

比如运行:

python -m idlelib

后来经过摸索,终于找到了两种设置方法。Talk is cheap. Show you the code:

创建快捷方式

方法1

import os
import pythoncom
from win32com.shell import shell

def MakeLink1(path, target, args='', icon=''):
    link = pythoncom.CoCreateInstance(
        shell.CLSID_ShellLink, None,
        pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink)
    link.SetPath(target)
    link.SetArguments(args)
    link.SetWorkingDirectory(os.getcwd())
    link.SetIconLocation(icon, 0)
    link.QueryInterface(pythoncom.IID_IPersistFile).Save(os.path.abspath(path), 0)
    print(dir(link))

MakeLink1('link1.lnk', 'cmd', '/k ipconfig', r'C:\Windows\System32\mspaint.exe')

方法2

import win32com.client as client

def MakeLink2(path, target, args='', icon=',0'):
    shell = client.Dispatch('Wscript.Shell')
    link = shell.CreateShortCut(path)
    link.TargetPath = target
    link.Arguments = args
    link.IconLocation = icon
    link.save()
    print(dir(link))

MakeLink2('link2.lnk', 'notepad', 'test.txt', r'C:\Windows\System32\mspaint.exe')

可选参数

两个方法比较类似,第二种还更短一点。但是在第一种方法中调用查看类属性,可以看到一些提示的函数可用。而正是这些字段的提示,让我找到了如何设置快捷方式的参数部分:

打印出来可以看到有这些可用的字段:

GetArguments
GetDescription
GetHotkey
GetIDList
GetIconLocation
GetPath
GetShowCmd
GetWorkingDirectory
QueryInterface
Resolve
SetArguments
SetDescription
SetHotkey
SetIDList
SetIconLocation
SetPath
SetRelativePath
SetShowCmd
SetWorkingDirectory

然后根据推测,也可以把第二种方法的参数设置方式试出来了。

获取系统路径

在自动化部署的场景下,通常需求是将目标文件创建桌面和开始菜单快捷方式,或者按需要设置自启动选项。根据注册表里的信息,找到这些路径是比较简单的:

import winreg

def GetDir(name, local=False): # Name: 'Desktop', 'SendTo', 'Programs', 'Startup'
    path = r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders'
    if local:
        name = 'Common ' + name
        key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, path)
    else:
        key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, path)
    return winreg.QueryValueEx(key, name)[0]

其中local参数为查找所有用户通用的路径、或者当前用户的路径。

需要注意的是,如果希望在开始菜单中显示图标,需要创建快捷方式到对应目录,只是移动文件到目标路径反而是不行的,但是桌面启动中没有这样的约束。

一键部署

结合参数,可以一键部署快捷方式,或者一键卸载快捷方式,示例代码:

import os
import sys
import winreg
import win32com.client as client

file, *args = sys.argv
if '-i' in args or '-u' in args:
    filename = os.path.splitext(os.path.basename(file))[0]
    shell = client.Dispatch('Wscript.Shell')
    p = r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders'
    for name in 'Desktop', 'Programs', 'Startup':
        key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, p)
        p1 = winreg.QueryValueEx(key, name)[0]
        p2 = os.path.join(p1, filename + '.lnk')
        print(p2)
        if '-i' in args:
            link = shell.CreateShortCut(p2)
            link.TargetPath = file
            link.save()
        else:
            if os.path.isfile(p2):  
                os.remove(p2)

运行方式:

一键安装

code.py -i

一键卸载

code.py -u

参考链接

[1]: https://www.cnblogs.com/luoheng23/p/11342479.html
[2]: https://blog.csdn.net/weixin_43903378/article/details/94392277
[3]: https://blog.csdn.net/geeklevin/article/details/120685236

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值