Unicode-aware file shortcuts in Windows

  • Thread starter Stanislaw Findeisen
  • Start date
S

Stanislaw Findeisen

Does anyone know how to create file shortcuts in Windows?

The only way I know is like:

---------------------------------------------------------------

import win32com.client

wScriptShellObject = win32com.client.Dispatch("WScript.Shell")
shortcutName = unicode("shortcut.lnk", "utf8")
shortcut = wScriptShellObject.CreateShortcut(shortcutName)
shortcut.TargetPath = <FULL PATH TO TARGET FILE>
shortcut.Save()

---------------------------------------------------------------

Unfortunately, this causes problems with Unicode characters:

(1) If there are Unicode characters in target file name, not all of them
get correctly copied to the shortcut's "target" attribute, thus
producing an invalid shortcut.

(2) If there are Unicode characters in shortcut name, not all of them
get correctly copied, thus producing a shortcut with a name different
than desired.

(3) If there are Unicode characters somewhere in the path to the
shortcut itself (above the shortcut name component), then they also get
corrupted, which effects in an invalid (non-existent) path and errors
like this one:

---------------------------------------------------------------

E:\Documents and Settings\Staszek\Progs\Python-Windows\test_1>cf.py
Traceback (most recent call last):
File "E:\Documents and
Settings\Staszek\Progs\Python-Windows\test_1\cf.py", line 7, in ?
shortcut.Save()
File "<COMObject <unknown>>", line 2, in Save
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0,
'WshShortcut.Save', 'Unable to save shortcut "E:\\Documents and
Settings\\Staszek\\Progs\\Python-Windows\\test_1\\Te\x9a\xed me c\xfd
z\xf3lazcseL\\link do vecer Z\xd3LAKe e\x9a\xed bla.lnk".', None, 0,
-2147024893), None)

---------------------------------------------------------------

The original path to shortcut file referenced in error description above
contains Polish and Czech characters, and it was:

---------------------------------------------------------------

E:\Documents and Settings\Staszek\Progs\Python-Windows\test_1\Těší mÄ› Äý
żółąźćśęÅ\link do veÄer ŻÓÅÄ„KÄ™ ěší bla.lnk

---------------------------------------------------------------

As it can be seen, some of those national characters in the error
description string are escaped using the \x.. sequence, while others are
simply "flatten" (like first "e" in "Te\x9a\xed", or "lazcse").

So it seems as if WScript.Shell object in Python didn't fully support
Unicode?... Or perhaps it does, but one has to know how to activate it.

Any help would be appreciated.

I am using standard Windows console cmd.exe in Windows 2000, Python 2.4,
and Mark Hammond's Python for Windows Extensions build 204.

+-------------------------------------------------------+
| When replying, please replace "my_initials" in the |
| From: address field with my initials - that is, "SF". |
+-------------------------------------------------------+
 
N

Neil Hodgson

Stanislaw Findeisen:
E:\Documents and Settings\Staszek\Progs\Python-Windows\test_1>cf.py
Traceback (most recent call last):
File "E:\Documents and
Settings\Staszek\Progs\Python-Windows\test_1\cf.py", line 7, in ?
shortcut.Save()
File "<COMObject <unknown>>", line 2, in Save
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0,
'WshShortcut.Save', 'Unable to save shortcut "E:\\Documents and
Settings\\Staszek\\Progs\\Python-Windows\\test_1\\Te\x9a\xed me c\xfd
z\xf3lazcseL\\link do vecer Z\xd3LAKe e\x9a\xed bla.lnk".', None, 0,
-2147024893), None)

I see similar problems using WSH so I think the problem is in
WScript.Shell, rather than in Python's access to COM. Slightly
simplified and with the directory created:

<package>
<job id="js">
<script language="JScript">
WScript.Echo("Start")
var WshShell = WScript.CreateObject("WScript.Shell");
var oShellLink = WshShell.CreateShortcut("C:\\Těší mÄ› Äý
żółąźćśęÅ\\link do veÄer ŻÓÅÄ„KÄ™ ěší bla.lnk");
oShellLink.TargetPath = "c:\\bin\\bb.api";
oShellLink.Save();
WScript.Echo("End")
</script>
</job>
</package>

C:\bin>cscript ul.wsf
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

Start
C:\bin\ul.wsf(9, 11) WshShortcut.Save: Unable to save shortcut "C:\Teší
me cý zólazcseL\link do vecer ZÓLAKe eší bla.lnk".


C:\bin>dir c:\T*
Volume in drive C has no label.
Volume Serial Number is A04B-224D

Directory of c:\

02/06/2005 10:30 PM 67 test.py
17/09/2005 09:56 AM <DIR> Těší mÄ› Äý żółąźćśęÅ
1 File(s) 67 bytes
1 Dir(s) 7,014,309,888 bytes free

C:\bin>dir "c:\Těší mÄ› Äý żółąźćśęÅ"
Volume in drive C has no label.
Volume Serial Number is A04B-224D

Directory of c:\Těší mÄ› Äý żółąźćśęÅ

17/09/2005 09:56 AM <DIR> .
17/09/2005 09:56 AM <DIR> ..
0 File(s) 0 bytes
2 Dir(s) 7,014,309,888 bytes free

C:\bin>

Happy googling.

Neil
 
J

John Bauman

Stanislaw Findeisen said:
Does anyone know how to create file shortcuts in Windows?

The only way I know is like:

---------------------------------------------------------------

import win32com.client

wScriptShellObject = win32com.client.Dispatch("WScript.Shell")
shortcutName = unicode("shortcut.lnk", "utf8")
shortcut = wScriptShellObject.CreateShortcut(shortcutName)
shortcut.TargetPath = <FULL PATH TO TARGET FILE>
shortcut.Save()

---------------------------------------------------------------

I see that another way is available here:
http://msdn.microsoft.com/library/d.../shell_int_programming/shortcuts/shortcut.asp
I haven't tried, and I don't have the knowledge to convert the C++ to
Python, but it works at a lower level and may get around the bugs. It does
seem that the name of the file isn't Unicode in their method, either, so I'm
not sure if it'll work.
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

John said:
I see that another way is available here:
http://msdn.microsoft.com/library/d.../shell_int_programming/shortcuts/shortcut.asp
I haven't tried, and I don't have the knowledge to convert the C++ to
Python, but it works at a lower level and may get around the bugs. It does
seem that the name of the file isn't Unicode in their method, either, so I'm
not sure if it'll work.

There are two versions of the IShellLink interface: IShellLinkA and
IShellLinkW. You need to use the *W version to pass characters outside
the ANSI code page.

Regards,
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top