【问题标题】:Modify Windows shortcuts using Python使用 Python 修改 Windows 快捷方式
【发布时间】:2011-10-11 23:06:41
【问题描述】:

如何使用 Python 更改 Windows 快捷方式?

例如来自:

H:\My Music\some_file.mp3

到:

D:\Users\Myself\My Music\some_file.mp3

【问题讨论】:

标签: python windows shortcuts


【解决方案1】:

这是在 Python 中使用 Winshell 库执行此操作的另一种更合适的方法:Using Python to create Windows shortcuts。在您的情况下,代码将如下所示:

import os, winshell
from win32com.client import Dispatch

desktop = winshell.desktop()
path = os.path.join(desktop, "some_file.mp3.lnk")
target = r"D:\Users\Myself\My Music\some_file.mp3"
wDir = r"D:\Users\Myself\My Music"
icon = r"D:\Users\Myself\My Music\some_file.mp3"

shell = Dispatch('WScript.Shell')
shortcut = shell.CreateShortCut(path)
shortcut.Targetpath = target
shortcut.WorkingDirectory = wDir
shortcut.IconLocation = icon
shortcut.save()

应该删除或重写现有的快捷方式。如果您需要它来批量处理快捷方式文件,那么我认为有一些方法可以从现有快捷方式中读取路径,但没有设法找到它。

【讨论】:

  • winshell 可能并不遥远。它仍然不是内置的。为了获取用户桌面位置,您还可以这样做:os.path.join(os.getenv('userprofile'), 'desktop')
【解决方案2】:

Jonathan 的解决方案非常有效。这是我为实现它而生成的有用功能。只需传入快捷方式文件的名称(例如“Mozilla Firefox.lnk”,无需指定整个文件路径)和新的快捷方式目的地,即可修改。

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

def short_target(filename,dest):
    shortcut = pythoncom.CoCreateInstance (
        shell.CLSID_ShellLink,
        None,
        pythoncom.CLSCTX_INPROC_SERVER,
        shell.IID_IShellLink
    )
    desktop_path = shell.SHGetFolderPath (0, shellcon.CSIDL_DESKTOP, 0, 0)
    shortcut_path = os.path.join (desktop_path, filename)
    persist_file = shortcut.QueryInterface (pythoncom.IID_IPersistFile)
    persist_file.Load (shortcut_path)
    shortcut.SetPath(dest)
    mydocs_path = shell.SHGetFolderPath (0, shellcon.CSIDL_PERSONAL, 0, 0)
    shortcut.SetWorkingDirectory (mydocs_path)
    persist_file.Save (shortcut_path, 0)

唯一的依赖是 pywin32 库。另请注意,可以在其快捷方式目标中指定选项和参数。要实现,只需调用:

short_target("shortcut test.lnk",'C:\\')   #note that the file path must use double backslashes rather than single ones. This is because backslashes are used for special characters in python (\n=enter, etc) so a string must contain two backslashes for it to be registered as one backslash character.

本示例将桌面上名为“shortcut test”的快捷方式的目标设置为打开硬盘根目录 (C:) 中文件管理器的快捷方式。

【讨论】:

  • 我正在寻找一种将SetWorkingDirectory 转至“”的方法,以获取像普通程序一样工作的快捷方式。这对我有帮助。
【解决方案3】:

以下是使用 Windows 脚本宿主创建快捷方式的方法:http://msdn.microsoft.com/en-us/library/fywyxt64

尝试从 Python 将其写入文件并动态运行。

【讨论】:

    【解决方案4】:

    还有一个方法详解here

    使用快捷方式更新示例。可以shortcut.GetPath(),修改后使用shortcut.SetPath()方法设置。

    【讨论】:

      【解决方案5】:

      前面的答案完全有效,但是为了真正完成它们,我添加了用于批量编辑的代码,因为我想您可能有很多要编辑的链接。

      如果您想一次编辑多个链接,请使用此选项:

      import os, sys
      import glob
      import pythoncom
      from win32com.shell import shell, shellcon
      
      def shortcut_target (filename):
        link = pythoncom.CoCreateInstance (
          shell.CLSID_ShellLink,    
          None,
          pythoncom.CLSCTX_INPROC_SERVER,    
          shell.IID_IShellLink
        )
        persist_file = link.QueryInterface (pythoncom.IID_IPersistFile)
        persist_file.Load (filename)
        #
        # GetPath returns the name and a WIN32_FIND_DATA structure
        # which we're ignoring. The parameter indicates whether
        # shortname, UNC or the "raw path" are to be
        # returned. Bizarrely, the docs indicate that the 
        # flags can be combined.
        #
        name, _ = link.GetPath (shell.SLGP_UNCPRIORITY)
      
        target = name
        target = target.replace('H:\My Music', 'D:\Users\Myself\My Music')
      
        link.SetPath(target)
        persist_file.Save(filename, 0)
      
        return name
      
      def shell_glob (pattern):
        for filename in glob.glob (pattern):
          if filename.endswith (".lnk"):
            print shortcut_target(filename)
      
      
      
      desktop = "H:\My Music\"
      for filename in shell_glob (os.path.join (desktop, "*")):
        print filename
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-06
        • 1970-01-01
        • 2020-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-29
        • 2014-10-06
        相关资源
        最近更新 更多