当前位置:   article > 正文

用Python两种方法创建带参数运行的快捷方式,一键创建桌面和开始菜单快捷方式和部署自启动_import os import pythoncom from win32com.shell imp

import os import pythoncom from win32com.shell import shell

需求背景

在网上很容易就能找到用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')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

方法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')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

可选参数

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

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

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]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

其中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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

运行方式:

一键安装

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/264727?site
推荐阅读
相关标签
  

闽ICP备14008679号