3

I have this python3.6 code that creates Windows shortcuts:

from win32com.client import Dispatch
path_to_target = r"C:\Program Files\ピチャーム\pycharm64.exe"
path_to_shortcut = r"C:\Program Files\pycharm.lnk"
shell = Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut(path_to_shortcut)
            
shortcut.Targetpath = path_to_target  # exception here
shortcut.save()

If path_to_target contains any non ascii characters I get an exception: Property '<unknown>.Targetpath' can not be set.

The code works OK and creates the proper shortcut if path_to_target is only ascii characters.

How can I create shortcuts to targets that have unicode characters?

Is there alternative API to create Windows shortcuts?

Periodic Maintenance
  • 1,698
  • 4
  • 20
  • 32

2 Answers2

5

This can be done by making sure not to use shell but the direct ShellLink object

import comtypes
import comtypes.shelllink
import comtypes.client
import comtypes.persist

shortcut = comtypes.client.CreateObject(comtypes.shelllink.ShellLink)
shortcut_w = shortcut.QueryInterface(comtypes.shelllink.IShellLinkW)
shortcut_file = shortcut.QueryInterface(comtypes.persist.IPersistFile)

shortcut_w.SetPath ("C:\\Temp\\हिंदी टायपिंग.txt")
shortcut_file.Save("C:\\Temp\\हिंदी.lnk", True)

Created

Update 1

Thanks to @Axois comments, I have verified that your original code works if you set the unicode support

Unicode support

PS: Comments in this question pointed me in the right direction

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
  • Adding on to this answer, the reason why you are getting a unicode error can be fixed by going to `Region & language` -> `Administrative language settings` -> `Change system locale` -> check `Use Unicode UTF-8 for worldwide language support` – Axois Jul 28 '19 at 13:52
  • Thanks @Axois, never even knew there is a setting like that on Windows – Tarun Lalwani Jul 28 '19 at 14:03
  • No worries, I was stumped initially at the beginning too, because I've got the same error as him (`Property '<unknown>.Targetpath' can not be set.`) and i solved it by applying that settings. However I got stuck when trying to create the shortcut in the `C:\ `. Creating shortcuts with that japanese text works fine in the desktop. – Axois Jul 28 '19 at 14:06
  • That may be because windows prevent programs creating stuff in `C:\ ` if they don't have admin access – Tarun Lalwani Jul 28 '19 at 14:12
  • I see, im going to post mine as the answer if you dont mind, because I have spent some time and effort researching around for answers too. – Axois Jul 28 '19 at 14:14
  • Sure, you should go ahead and post your findings – Tarun Lalwani Jul 28 '19 at 14:15
  • @TarunLalwani: this code looks promising, but does not work. the line `from comtypes.client import IShellLinkW` produces ImportError. Also `objShortCut` is not defined. – Periodic Maintenance Jul 29 '19 at 07:08
  • I will fix that, just refactored a bit before posting tested code – Tarun Lalwani Jul 29 '19 at 07:23
  • @PeriodicMaintenance, please check the updated code now, have run and tested it. – Tarun Lalwani Jul 29 '19 at 07:50
0

Try pythoncom instead which is Unicode compatible. Also not the target directory for the shortcut must exist before hand.

Tested with python 3.6 64-bit

import os
import pythoncom
from win32com.shell import shell

shortcut_dir = r"C:\test\ピチャーム"
if not os.path.exists(shortcut_dir):
    os.makedirs(shortcut_dir)

path_to_shortcut = shortcut_dir + r"\_test.lnk"
path_to_target = r"C:\test\target.exe"

shortcut = pythoncom.CoCreateInstance(
    shell.CLSID_ShellLink, None, pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink)
persist_file = shortcut.QueryInterface (pythoncom.IID_IPersistFile)
shortcut.SetPath(path_to_target)
persist_file.Save (path_to_shortcut, 0)
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • I'm accepting this answer since it's the best answer, and it works! You can also add a working directory: `shortcut.SetWorkingDirectory(working_dir_path)`. – Periodic Maintenance Jul 29 '19 at 10:33
  • @PeriodicMaintenance, this is the same code as mine instead of using `comtypes` it uses another library `pythoncom` – Tarun Lalwani Jul 29 '19 at 10:43
  • @PeriodicMaintenance Tarun's answer is the exact same as Barmak. And since he posted first, I think its only fair to award the bounty to him. – Axois Jul 29 '19 at 10:51
  • @TarunLalwani, Tarun's code is NOT the same as Barmak - certainly not exact same. 1st, as Tarun's said, it uses a different library that needs to be pip installed separately. 2nd Barmak's code worked out of the box without need to fix. – Periodic Maintenance Jul 29 '19 at 14:56
  • @PeriodicMaintenance, no worries. You can choose whichever answer you find best and award that answer the bounty. – Tarun Lalwani Jul 29 '19 at 15:01