Unicode-aware file shortcuts in Windows

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stanislaw Findeisen

    #1

    Unicode-aware file shortcuts in Windows

    Does anyone know how to create file shortcuts in Windows?

    The only way I know is like:

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

    import win32com.client

    wScriptShellObj ect = win32com.client .Dispatch("WScr ipt.Shell")
    shortcutName = unicode("shortc ut.lnk", "utf8")
    shortcut = wScriptShellObj ect.CreateShort cut(shortcutNam e)
    shortcut.Target Path = <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\Stasze k\Progs\Python-Windows\test_1> cf.py
    Traceback (most recent call last):
    File "E:\Documen ts and
    Settings\Stasze k\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.Sa ve', 'Unable to save shortcut "E:\\Docume nts and
    Settings\\Stasz ek\\Progs\\Pyth on-Windows\\test_1 \\Te\x9a\xed me c\xfd
    z\xf3lazcseL\\l ink 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\Stasze k\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_initial s" in the |
    | From: address field with my initials - that is, "SF". |
    +-------------------------------------------------------+

    --

    Enter through the narrow gate! (Mt 7:13-14)
  • Neil Hodgson

    #2
    Re: Unicode-aware file shortcuts in Windows

    Stanislaw Findeisen:
    [color=blue]
    > E:\Documents and Settings\Stasze k\Progs\Python-Windows\test_1> cf.py
    > Traceback (most recent call last):
    > File "E:\Documen ts and
    > Settings\Stasze k\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.Sa ve', 'Unable to save shortcut "E:\\Docume nts and
    > Settings\\Stasz ek\\Progs\\Pyth on-Windows\\test_1 \\Te\x9a\xed me c\xfd
    > z\xf3lazcseL\\l ink do vecer Z\xd3LAKe e\x9a\xed bla.lnk".', None, 0,
    > -2147024893), None)[/color]

    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="JScri pt">
    WScript.Echo("S tart")
    var WshShell = WScript.CreateO bject("WScript. Shell");
    var oShellLink = WshShell.Create Shortcut("C:\\T ěší mě čý
    Å¼Ã³Å‚Ä…ÅºÄ‡Å›Ä ™Å\\link do večer ŻÓŁĄKÄ™ ěší bla.lnk");
    oShellLink.Targ etPath = "c:\\bin\\bb.ap i";
    oShellLink.Save ();
    WScript.Echo("E nd")
    </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.Sav e: 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

    Comment

    • John Bauman

      #3
      Re: Unicode-aware file shortcuts in Windows


      "Stanislaw Findeisen" <my_initials@ng logic.com> wrote in message
      news:dgfjg8$j0n $1@achot.icm.ed u.pl...[color=blue]
      > Does anyone know how to create file shortcuts in Windows?
      >
      > The only way I know is like:
      >
      > ---------------------------------------------------------------
      >
      > import win32com.client
      >
      > wScriptShellObj ect = win32com.client .Dispatch("WScr ipt.Shell")
      > shortcutName = unicode("shortc ut.lnk", "utf8")
      > shortcut = wScriptShellObj ect.CreateShort cut(shortcutNam e)
      > shortcut.Target Path = <FULL PATH TO TARGET FILE>
      > shortcut.Save()
      >
      > ---------------------------------------------------------------[/color]

      I see that another way is available here:
      http://msdn.microsoft.com/library/de...s/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.


      Comment

      • Martin v. Löwis

        #4
        Re: Unicode-aware file shortcuts in Windows

        John Bauman wrote:[color=blue]
        > I see that another way is available here:
        > http://msdn.microsoft.com/library/de...s/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.[/color]

        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

        Comment

        Working...