From efd64fc16db5f4a955ffde54f325207eedd755cf Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Wed, 23 Dec 2020 20:07:01 -0800 Subject: [PATCH] Launch the lnk file itself instead of pulling out path. I guess I'm not sure why I ever wrote it to pull out the exe path instead of just calling the lnk directly. The purpose of this change is to maintain other attributes about the lnk such as custom icon which will appear on the taskbar, and compatibility settings. --- PGUI.pyw | 42 +++++++++++++----------------------------- 1 file changed, 13 insertions(+), 29 deletions(-) diff --git a/PGUI.pyw b/PGUI.pyw index 0bb15d9..f67887a 100644 --- a/PGUI.pyw +++ b/PGUI.pyw @@ -4,31 +4,15 @@ from tkinter.ttk import Frame, Style, Button import os import subprocess import sys -import winshell +from voussoirkit import pathclass def load_programs(): directory = os.path.join(os.path.dirname(__file__), 'PGUI') shortcuts = [os.path.join(directory, p) for p in os.listdir(directory)] shortcuts = [p for p in shortcuts if p.lower().endswith('.lnk')] - programs = [] - for shortcut in shortcuts: - name = os.path.splitext(os.path.basename(shortcut))[0] - shortcut = winshell.Shortcut(shortcut) - program = Program(name, shortcut.path, shortcut.arguments) - programs.append(program) - return programs - - -class Program(): - def __init__(self, name, path, arguments): - self.name = name - self.path = os.path.abspath(path) - self.arguments = arguments - - def __str__(self): - return f'{self.name}: {self.path}' - + shortcuts = [pathclass.Path(p) for p in shortcuts] + return shortcuts class PGUILauncher(Frame): def __init__(self, parent): @@ -44,19 +28,19 @@ class PGUILauncher(Frame): y = 0 self.buttonwidth = 12 - programs = load_programs() - for (programindex, program) in enumerate(programs): + shortcuts = load_programs() + for (index, shortcut) in enumerate(shortcuts): print(y, x) newButton = Button( self, - text=program.name, - command=lambda prog=program: self.launch_program(prog), + text=shortcut.replace_extension('').basename, + command=lambda sc=shortcut: self.launch_program(sc), ) - print(f'creating button for {program.name} at {program.path}') + print(f'Creating button for {shortcut.basename}') newButton.configure(width=self.buttonwidth) newButton.grid(row=y, column=x) x += 1 - if x >= 3 and (programindex != len(programs)-1): + if x >= 3 and (index != len(shortcuts)-1): x = 0 y += 1 print(y, x) @@ -64,10 +48,10 @@ class PGUILauncher(Frame): self.pack() self.update() - def launch_program(self, program): - print('opening application', program.name) - os.chdir(os.path.dirname(program.path)) - command = f'{program.path} {program.arguments}' + def launch_program(self, shortcut): + print('opening application', shortcut.basename) + os.chdir(shortcut.parent.absolute_path) + command = f'"{shortcut.absolute_path}"' subprocess.Popen(command, shell=True) self.quit()