Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?
shortcut.save()

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

like image 574
coco4l Avatar asked Jul 31 '16 19:07

coco4l


1 Answers

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()

Reference

like image 111
wombatonfire Avatar answered Oct 23 '22 05:10

wombatonfire