栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

文章目录
  • 需求背景
  • 创建快捷方式
    • 方法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:WindowsSystem32mspaint.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:WindowsSystem32mspaint.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'SoftwareMicrosoftWindowsCurrentVersionExplorerShell 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'SoftwareMicrosoftWindowsCurrentVersionExplorerShell 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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/338908.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号