Python,使用两个路径和参数创建快捷方式
Python, create shortcut with two paths and argument
我正在尝试通过 python 创建一个快捷方式,它将在另一个程序中使用参数启动一个文件。例如:
"C:\file.exe" "C:\folder\file.ext" argument
我试过的代码:
from win32com.client import Dispatch
import os
shell = Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut(path)
shortcut.Targetpath = r'"C:\file.exe" "C:\folder\file.ext"'
shortcut.Arguments = argument
shortcut.WorkingDirectory = "C:\" #or "C:\folder\file.ext" in this case?
shortcut.save()
但是我遇到了一个错误:
AttributeError: Property '<unknown>.Targetpath' can not be set.
我尝试了不同格式的字符串,google 似乎不知道这个问题的解决方案
from comtypes.client import CreateObject
from comtypes.gen import IWshRuntimeLibrary
shell = CreateObject("WScript.Shell")
shortcut = shell.CreateShortCut(path).QueryInterface(IWshRuntimeLibrary.IWshShortcut)
shortcut.TargetPath = "C:\file.exe"
args = ["C:\folder\file.ext", argument]
shortcut.Arguments = " ".join(args)
shortcut.Save()
以下是如何在 Python 3.6 上执行此操作(@wombatonfire 的解决方案的第二次导入已找不到)。
首先我做了 pip install comtypes
,然后:
import comtypes
from comtypes.client import CreateObject
from comtypes.persist import IPersistFile
from comtypes.shelllink import ShellLink
# Create a link
s = CreateObject(ShellLink)
s.SetPath('C:\myfile.txt')
# s.SetArguments('arg1 arg2 arg3')
# s.SetWorkingDirectory('C:\')
# s.SetIconLocation('path\to\.exe\or\.ico\file', 1)
# s.SetDescription('bla bla bla')
# s.Hotkey=1601
# s.ShowCMD=1
p = s.QueryInterface(IPersistFile)
p.Save("C:\link to myfile.lnk", True)
# Read information from a link
s = CreateObject(ShellLink)
p = s.QueryInterface(IPersistFile)
p.Load("C:\link to myfile.lnk", True)
print(s.GetPath())
# print(s.GetArguments())
# print(s.GetWorkingDirectory())
# print(s.GetIconLocation())
# print(s.GetDescription())
# print(s.Hotkey)
# print(s.ShowCmd)
有关详细信息,请参阅 site-packages/comtypes/shelllink.py。
我正在尝试通过 python 创建一个快捷方式,它将在另一个程序中使用参数启动一个文件。例如:
"C:\file.exe" "C:\folder\file.ext" argument
我试过的代码:
from win32com.client import Dispatch
import os
shell = Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut(path)
shortcut.Targetpath = r'"C:\file.exe" "C:\folder\file.ext"'
shortcut.Arguments = argument
shortcut.WorkingDirectory = "C:\" #or "C:\folder\file.ext" in this case?
shortcut.save()
但是我遇到了一个错误:
AttributeError: Property '<unknown>.Targetpath' can not be set.
我尝试了不同格式的字符串,google 似乎不知道这个问题的解决方案
from comtypes.client import CreateObject
from comtypes.gen import IWshRuntimeLibrary
shell = CreateObject("WScript.Shell")
shortcut = shell.CreateShortCut(path).QueryInterface(IWshRuntimeLibrary.IWshShortcut)
shortcut.TargetPath = "C:\file.exe"
args = ["C:\folder\file.ext", argument]
shortcut.Arguments = " ".join(args)
shortcut.Save()
以下是如何在 Python 3.6 上执行此操作(@wombatonfire 的解决方案的第二次导入已找不到)。
首先我做了 pip install comtypes
,然后:
import comtypes
from comtypes.client import CreateObject
from comtypes.persist import IPersistFile
from comtypes.shelllink import ShellLink
# Create a link
s = CreateObject(ShellLink)
s.SetPath('C:\myfile.txt')
# s.SetArguments('arg1 arg2 arg3')
# s.SetWorkingDirectory('C:\')
# s.SetIconLocation('path\to\.exe\or\.ico\file', 1)
# s.SetDescription('bla bla bla')
# s.Hotkey=1601
# s.ShowCMD=1
p = s.QueryInterface(IPersistFile)
p.Save("C:\link to myfile.lnk", True)
# Read information from a link
s = CreateObject(ShellLink)
p = s.QueryInterface(IPersistFile)
p.Load("C:\link to myfile.lnk", True)
print(s.GetPath())
# print(s.GetArguments())
# print(s.GetWorkingDirectory())
# print(s.GetIconLocation())
# print(s.GetDescription())
# print(s.Hotkey)
# print(s.ShowCmd)
有关详细信息,请参阅 site-packages/comtypes/shelllink.py。