使用Python修改Windows快捷方式

发布于 2024-11-26 03:21:54 字数 176 浏览 4 评论 0原文

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

例如从:

H:\My Music\some_file.mp3

到:

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

How do you change a Windows shortcut using Python?

e.g. from:

H:\My Music\some_file.mp3

to:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

海夕 2024-12-03 03:21:54

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

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()

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

Here's another, more appropriate way to do this in Python with Winshell library: Using Python to create Windows shortcuts. In your case the code will look like:

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()

Existing shortcut should be deleted or rewritten. If you need it for batch processing of shortcut files then I think there's some way to read paths from existing shortcuts, but didn't managed to find it.

海之角 2024-12-03 03:21:54

乔纳森的解决方案非常有效。这是我实现此功能时生成的有用函数。只需传入快捷方式文件的名称(例如“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.

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

Jonathan's solution works perfectly. This is the useful function I produced implementing this. Simply pass in the name of the shortcut file (for example "Mozilla Firefox.lnk", it is unnecessary to specify the entire filepath), and the new shortcut destination, and it will be modified.

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)

The only dependency is the pywin32 library. Also note that one is able to specify options and arguments in their shortcut destination. To implement, just call:

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.

This example will set the destination of a shortcut on your desktop called "shortcut test" to a shortcut that opens up the file manager in the root directory of the hard drive (C:).

猫七 2024-12-03 03:21:54

此处详细介绍了另一种方法

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

Yet another method is detailed here

Use the shortcut update example. You can shortcut.GetPath(), modify it and then use shortcut.SetPath() method to set it.

脸赞 2024-12-03 03:21:54

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

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

Here is how you can create a shortcut using Windows script host: http://msdn.microsoft.com/en-us/library/fywyxt64

Try to write it to file from Python and run it dynamically.

半岛未凉 2024-12-03 03:21:54

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

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

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

The previous answer are perfectly valid however to really complete them I added the code for bulk editing because I suppose you might have a lots of link to edit.

use this if you want to edit many links at once:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文