coco4l coco4l - 4 months ago 34
Python Question

Python, create shortcut with two paths and argument

I'm trying to create a shortcut through python that will launch a file in another program with an argument. E.g:

"C:\file.exe" "C:\folder\file.ext" argument

The code I've tried messing with:

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?

But i get an error thrown my way:

AttributeError: Property '<unknown>.Targetpath' can not be set.

I've tried different formats of the string and google doesn't seem to know the solution to this problem

Answer Source
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)