Create shortcut with python

Asked

Viewed 228 times

-3

I’d like to know how to create a shortcut for my own script . py using Python.

I mean, my code creates a shortcut of its own.

inserir a descrição da imagem aqui

  • 3

    For the sake of argument: to) you want to create the shortcut of a file USING the Python language or b) you want to create the SHORTCUT OF YOUR Python script to run it while starting Windows? The question "create shortcut with Python" suggests that you want option A, but in the text you put "create a shortcut for my . py", which suggests option B.

  • See if that helping

  • 1

    Your question is a little dubious. In the first part it refers to a SHORTCUT. In the second part it gives us the impression that you want to insert a script .py in the Windows startup folder - perhaps - wanting the said script to boot every time the system boots. I suggest you edit your question making your intention clearer.

2 answers

1


The code below should run with administrative privileges.

If the question is how to get a python script to create a link from you and send it into the global startup folder on windows OS:

  • first your script will have to discover the path to OS startup folder.
  • then you have to create a link for yourself and put it in the startup folder.

To find the path to the startup folder it is possible to do a search on the record of the machine on which the script is running and extract the input. The key to the registry "HKEY_LOCAL_MACHINE Software Microsoft Windows Currentversion Explorer Shell Folders" contains the paths to shell:folders which are special paths in the windows environment. Among these paths is the path to shell:Common Startup that the boot folder of for all Operating System users.

Aware of this the program should then connect to the registry via the function winreg.ConnectRegistry() in the key winreg.HKEY_LOCAL_MACHINE and with the function winreg.OpenKey() open the key "Microsoft Windows Currentversion Explorer Shell Folders Software" and then count the number of values of that key with the function winreg.QueryInfoKey() and then enumerate them with winreg.EnumValue() and salvalos in a dictionary shell.

To create the link do with the function os.symlink() and pass as parameters the source the absolute path to the global __file__ and as fate shell["Common Startup"] which is value containing the path to the operating system startup folder.

import os
import winreg

shell ={}

with winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) as reg:
    with winreg.OpenKey(reg, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders") as key:
        for i in range(winreg.QueryInfoKey(key)[1]):
           nome, val, _ = winreg.EnumValue(key, i)
           shell[nome] = val

os.symlink(os.path.abspath(__file__), f"{shell['Common Startup']}\\teste.py")

If the question is how to make a python script create a link from you and send it inside the particular startup folder of the user that runs the script on the windows operating system, the process is even but changes some details changes the registration key to be searched and value name becomes only Startup.

import os
import winreg

shell ={}

with winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER) as reg:
    with winreg.OpenKey(reg, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders") as key:
        for i in range(winreg.QueryInfoKey(key)[1]):
           nome, val, _ = winreg.EnumValue(key, i)
           shell[nome] = val

os.symlink(os.path.abspath(__file__), f"{shell['Startup']}\\teste.py")

If the question is not about self-copying when creating the link just change the source cumin by the file path you want to create the link.

0

Doing a google search, this type of file really isn’t trivial to move around - it’s a binary format - and requires specialized code to respect the fields, etc...

There is an opensource lib to bring these files, which even has bindings for Python -but I tried to install here and it is Python2 yet. Anyway, in the lib project folder you have the documentation of the files . lnk (it can be easier to tidy up the files . py from her to Python3 than re-implement the reading and writing of this file type - https://github.com/libyal/liblnk/blob/main/documentation/Windows%20Shortcut%20File%20(LNK)%20format.asciidoc

Browser other questions tagged

You are not signed in. Login or sign up in order to post.