Python - Create Shortcut with arguments

Dustin picture Dustin · Jul 11, 2013 · Viewed 7.3k times · Source

Using win32com.client, I'm attempting to create a simple shortcut in a folder. The shortcut however I would like to have arguments, except I keep getting the following error.

Traceback (most recent call last):
  File "D:/Projects/Ms/ms.py", line 153, in <module>
    scut.TargetPath = '"C:/python27/python.exe" "D:/Projects/Ms/msd.py" -b ' + str(loop7)

File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 570, in __setattr__
    raise AttributeError("Property '%s.%s' can not be set." % (self._username_, attr))
AttributeError: Property '<unknown>.TargetPath' can not be set.

My code looks like this. I've tried multiple different variates but can't seem to get it right. What am I doing wrong?

ws = win32com.client.Dispatch("wscript.shell")
scut = ws.CreateShortcut("D:/Projects/Ms/TestDir/testlink.lnk")
scut.TargetPath = '"C:/python27/python.exe" "D:/Projects/Ms/msd.py" -b 0'
scut.Save()

Answer

falsetru picture falsetru · Jul 11, 2013

Your code works for me without error. (Windows XP 32bit, Python 2.7.5, pywin32-216).

(I slightly modified your code because TargetPath should contain only executable path.)

import win32com.client
ws = win32com.client.Dispatch("wscript.shell")
scut = ws.CreateShortcut('run_idle.lnk')
scut.TargetPath = '"c:/python27/python.exe"'
scut.Arguments = '-m idlelib.idle'
scut.Save()

I got AttributeError similar to yours when I tried following (assign list to Arguments property.)

>>> scut.Arguments = []
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\python27\lib\site-packages\win32com\client\dynamic.py", line 570, in __setattr__
    raise AttributeError("Property '%s.%s' can not be set." % (self._username_, attr))
AttributeError: Property '<unknown>.Arguments' can not be set.