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

您所在的位置:网站首页 如何在快捷方式添加参数文件 用Python两种方法创建带参数运行的快捷方式,一键创建桌面和开始菜单快捷方式和部署自启动

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

2024-07-06 18:57| 来源: 网络整理| 查看: 265

文章目录 需求背景创建快捷方式方法1方法2可选参数 获取系统路径一键部署参考链接

需求背景

在网上很容易就能找到用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



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3