Create shortcut files in Windows 7 using Python

前端 未结 5 2169
无人及你
无人及你 2021-02-08 17:46

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:

<
5条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-08 18:32

    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.

提交回复
热议问题