Skip to content

Instantly share code, notes, and snippets.

@bitsgalore
Last active December 1, 2023 10:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bitsgalore/7579ab3fecbd4a143feacd1fb44a5858 to your computer and use it in GitHub Desktop.
Save bitsgalore/7579ab3fecbd4a143feacd1fb44a5858 to your computer and use it in GitHub Desktop.
Create Windows Desktop shortcut to executable in Python Scripts directory
#! /usr/bin/env python
import os
import sys
import shutil
import sysconfig
import winreg
from win32com.client import Dispatch
def get_reg(name,path):
# Read variable from Windows Registry
# From http://stackoverflow.com/a/35286642
try:
registry_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, path, 0,
winreg.KEY_READ)
value, regtype = winreg.QueryValueEx(registry_key, name)
winreg.CloseKey(registry_key)
return value
except WindowsError:
return None
# Package name
packageName = 'iromlab'
# Scripts directory (location of launcher script)
scriptsDir = sysconfig.get_path('scripts')
# Target of shortcut
target = os.path.join(scriptsDir, packageName + '.exe')
# Name of link file
linkName = packageName + '.lnk'
# Read location of Windows desktop folder from registry
regName = 'Desktop'
regPath = r'Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders'
desktopFolder = os.path.normpath(get_reg(regName,regPath))
# Path to location of link file
pathLink = os.path.join(desktopFolder, linkName)
shell = Dispatch('WScript.Shell')
shortcut = shell.CreateShortCut(pathLink)
shortcut.Targetpath = target
shortcut.WorkingDirectory = scriptsDir
shortcut.IconLocation = target
shortcut.save()
@Pointy-Orb
Copy link

Can I use this code? (with credit of course)

@bitsgalore
Copy link
Author

@Pointy-Orb Of course, no problem at all!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment