Create shortcut files in Windows 7 using Python

半腔热情 提交于 2021-01-20 19:36:05

问题


Is there a simple way to create shortcuts in Windows 7 on Python? I looked it up online but they did not seem that simple.

I have tried using this simple method:

hi = open("ShortcutFile.lnk", "w")
hi.close()

Which creates a shortcut file. But how would I edit the link it is connected to. Like the file it would open? Can someone give me a push in the right direction?


回答1:


Old, but I would still like to post an answer to help out anyone who might have the same question and in need of a code example.

First, download pywin32 with pip install pywin32 or download the sourceforge binaries or the pywin32 wheel file and pip install.

import win32com.client
import pythoncom
import os
# pythoncom.CoInitialize() # remove the '#' at the beginning of the line if running in a thread.
desktop = r'C:\Users\Public\Desktop' # path to where you want to put the .lnk
path = os.path.join(desktop, 'NameOfShortcut.lnk')
target = r'C:\path\to\target\file.exe'
icon = r'C:\path\to\icon\resource.ico' # not needed, but nice

shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut(path)
shortcut.Targetpath = target
shortcut.IconLocation = icon
shortcut.WindowStyle = 7 # 7 - Minimized, 3 - Maximized, 1 - Normal
shortcut.save()

I have used the WindowStyle when I have a GUI with a debug console, and I don't want the console to pop up all the time. I haven't tried it with a program that isn't consoled.

Hope this helps!




回答2:


If you looking for a platform independent version that works with Python 3 look at swinlnk.

from swinlnk.swinlnk import SWinLnk
swl = SWinLnk()
swl.create_lnk('W:\Foo\Bar', '/mnt/win_share/playground/Bar_winlink.lnk')

This python script is based on the C / Bash Tool mslink.




回答3:


You can generate a .bat file dinamically fod do that, and replacing specific parts with the corrected path for operate with spaces.

Te correct way to manage windows paths with spaces is wrap the spaced path secion with "". For example, if the path is

C:\Users\The Pc\Desktop

the corrected version is

C:\Users\"The Pc"\Desktop

The solution what I propose is this:

1- Generate a base .bat file with keywords for to replace with te correct path through Python.

2- Build the path according the current environ using the os Python library.

3- Open the base.bat file with Python and check line by line seaching for the keyword for to replace with the correctd path previously generated in the steep 2.

4- At the same time we check and replace our spetial keywords with te corrected path, we write a final .bat file with the updated lines of the base.bat file in the same for loop statament.

5- Execute the final.bat file with the subprocess Python library:


For explain it with code, I gonna use a personal screen recorder project, and it goes like this:

1) Our base.bat file (look at our [r] keywords):

=============================================================

@echo off

set dir=%LOCALAPPDATA%\srw

set sof=srw.exe

set name=Grabador de Pantalla

ECHO Set objShell = WScript.CreateObject("WScript.Shell") >>[r]

ECHO ficheroAccesoDirecto = "%USERPROFILE%\Desktop\%name%.lnk" >>[r]

ECHO Set objAccesoDirecto = objShell.CreateShortcut(ficheroAccesoDirecto) >>[r]

ECHO objAccesoDirecto.TargetPath = "%dir%\%sof%" >>[r]

ECHO objAccesoDirecto.Arguments = "" >>[r]

ECHO objAccesoDirecto.Description = "%name%" >>[r]

ECHO objAccesoDirecto.HotKey = "ALT+CTRL+G" >>[r]

ECHO objAccesoDirecto.IconLocation = "%dir%\vista\icon.ico" >>[r]

ECHO objAccesoDirecto.WindowStyle = "1" >>[r]

ECHO objAccesoDirecto.WorkingDirectory = "%dir%" >>[r]

ECHO objAccesoDirecto.Save >>[r]

ATTRIB +h +s [r]

START /B /WAIT [r]

erase /Q /a h s [r]

exit

=============================================================

2- Now we start our Python script. Building the path:

# Python Code
import os, subprocess

path = os.environ["USERPROFILE"]+"\\Desktop\\accsdirecto.vbs"
user = os.environ["USERNAME"]
path = path.replace('{}'.format(user), '"{}"'.format(user))

This code generate our spaced formated path:

C:\Users\"El Computador"\Desktop\accsdirecto.vbs

3- Open the base.bat file and creating our final.bat file:

# Python Code
base = open("base", "r")
bat = open("lnk.bat", "w")

4- Check the base.bat file line by line and replace the [r] keywords for our correct formated path:

# Python Code
for x in base:
    if "[r]" in str(x):
        x = x.replace("[r]", path)
    bat.write(x)

Finally we colse the .bat files:

# Python Code
base.close()
bat.close()

5- Execute the final.bat file with subprocess Python library:

# Python Code
subprocess.run("final.bat", stdout=subprocess.PIPE)

Our whole Python script look like this:

# Python code
import os, subprocess

path = os.environ["USERPROFILE"]+"\\Desktop\\accsdirecto.vbs" user =
os.environ["USERNAME"] path = path.replace('{}'.format(user),
'"{}"'.format(user))

base = open("base", "r") bat = open("lnk.bat", "w")

for x in base:
    if "[r]" in str(x):
        x = x.replace("[r]", path)
    bat.write(x)

base.close()
bat.close()

subprocess.run("final.bat", stdout=subprocess.PIPE)

In the end, our final.bat file generated looks like this:

=============================================================

@echo off

set dir=%LOCALAPPDATA%\srw

set sof=srw.exe

set name=Grabador de Pantalla

ECHO Set objShell = WScript.CreateObject("WScript.Shell") >>C:\Users\"El Computador"\Desktop\accsdirecto.vbs

ECHO ficheroAccesoDirecto = "%USERPROFILE%\Desktop\%name%.lnk" >>C:\Users\"El Computador"\Desktop\accsdirecto.vbs

ECHO Set objAccesoDirecto = objShell.CreateShortcut(ficheroAccesoDirecto) >>C:\Users\"El Computador"\Desktop\accsdirecto.vbs

ECHO objAccesoDirecto.TargetPath = "%dir%\%sof%" >>C:\Users\"El Computador"\Desktop\accsdirecto.vbs

ECHO objAccesoDirecto.Arguments = "" >>C:\Users\"El Computador"\Desktop\accsdirecto.vbs

ECHO objAccesoDirecto.Description = "%name%" >>C:\Users\"El Computador"\Desktop\accsdirecto.vbs

ECHO objAccesoDirecto.HotKey = "ALT+CTRL+G" >>C:\Users\"El Computador"\Desktop\accsdirecto.vbs

ECHO objAccesoDirecto.IconLocation = "%dir%\vista\icon.ico" >>C:\Users\"El Computador"\Desktop\accsdirecto.vbs

ECHO objAccesoDirecto.WindowStyle = "1" >>C:\Users\"El Computador"\Desktop\accsdirecto.vbs

ECHO objAccesoDirecto.WorkingDirectory = "%dir%" >>C:\Users\"El Computador"\Desktop\accsdirecto.vbs

ECHO objAccesoDirecto.Save >>C:\Users\"El Computador"\Desktop\accsdirecto.vbs

ATTRIB +h +s C:\Users\"El Computador"\Desktop\accsdirecto.vbs

START /B /WAIT C:\Users\"El Computador"\Desktop\accsdirecto.vbs

erase /Q /a h s C:\Users\"El Computador"\Desktop\accsdirecto.vbs

exit

=============================================================

Finally, I leave you a video testing the code in a Windos 7 virtual machine environment under GNU/Linux Fedora host. I hope this help you.

-Greetings from Chile.




回答4:


What you are doing currently is creating the equivalent of an empty file (in this case a shortcut) on your computer within your current working directory.

To make this file a usable shortcut, you will need to write some information to the file you are creating that tells windows what this shortcut file is going to do. In this case, that would be what type of shortcut you want it to run and any other arguments necessary to accomplish the task at hand.

I believe pywin32 will come in handy for most of these tasks: http://sourceforge.net/projects/pywin32/

If you are creating an internet shortcut, an example of that can be found on this page (using pywin32): http://www.blog.pythonlibrary.org/2010/01/23/using-python-to-create-shortcuts/

If you are looking to create something other than an internet shortcut, you will need need to lookup the appropriate windows command for the type of shortcut you want to run (e.g. open a particular application or specific file).




回答5:


@Casey answer put me on the right track, but I wasted a lot of time before figuring out that you also need to set the working directory (called 'start in' in windows properties). If you do not, your shortcut will work but your application will not find the files it may need from the same folder.

This resolved all my issues:

            path_input = ''
            path_output = ''
            path_cwd = ''
            path_icon = ''

            shell = win32com.client.Dispatch("WScript.Shell")
            shortcut = shell.CreateShortCut(path_output)
            shortcut.Targetpath = path_input
            # shortcut.IconLocation = icon
            shortcut.Workingdirectory = path_cwd
            shortcut.WindowStyle = 1  # 7 - Minimized, 3 - Maximized, 1 - Normal
            shortcut.save()


来源:https://stackoverflow.com/questions/26986470/create-shortcut-files-in-windows-7-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!