Modify Windows unicode shortcuts using Python
Asked Answered
R

2

1

Following this question, I've settled on the following Python code to modify Windows shortcuts.
It works for English based shortcuts but it doesn't for unicode based shortcuts.

How could this (or any other) snippet be modified to support unicode?

import re, os, pythoncom
from win32com.shell import shell, shellcon

shortcut_path = os.path.join(path_to_shortcut, shortcut_filename)
shortcut = pythoncom.CoCreateInstance (shell.CLSID_ShellLink, None, pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink)
persist_file = shortcut.QueryInterface (pythoncom.IID_IPersistFile)
persist_file.Load (shortcut_path)
destination1 = shortcut.GetPath(0)[0]
destination2 = os.path.join(destination_path, destination_filename)
shortcut.SetPath(destination2)
persist_file.Save(shortcut_path, 0)

Assume the following are unicode: path_to_shortcut, shortcut_filename, destination_path, destination_filename

Rightful answered 2/8, 2011 at 17:31 Comment(2)
What exactly happens on a unicode shortcut?Teahouse
Does this problem happen only when you try to work with shortcuts, or in cases of using shell classes? Maybe the question should be extended to using Unicode in general, not only for Windows shortcuts?Blatt
S
0

Perhaps looking here may help: Python Unicode HOWTO

I'm guessing you'd need to be sure that each of those strings was properly encoded as Unicode and any changes need to preserve that encoding. That article should provide all the information you'll need.

Storied answered 2/8, 2011 at 20:29 Comment(0)
G
0

I know 2 ways to edit unicode shortcuts, winshell and pylnk3.
I would recommend winshell because pylnk3 has some bug.

winshell

import winshell

shortcut = winshell.Shortcut("D:\\測試.lnk")

# read
print(shortcut.path)
print(shortcut.working_directory)

# edit
shortcut.path = "D:\\測試.png"
shortcut.working_directory = "D:\\"
shortcut.write()

pylnk3

import pylnk3

# read
lnk = pylnk3.parse("D:\\測試.lnk")
print(lnk.path)
print(lnk.work_dir)

# edit
pylnk3.for_file(
    lnk_name = "D:\\測試.lnk",
    target_file = "D:\\測試.png",
    work_dir = "D:\\",
)
Glia answered 14/4 at 11:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.