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.
This commit is contained in:
voussoir 2020-12-23 20:07:01 -08:00
parent 1eae1ae399
commit efd64fc16d

View file

@ -4,31 +4,15 @@ from tkinter.ttk import Frame, Style, Button
import os import os
import subprocess import subprocess
import sys import sys
import winshell
from voussoirkit import pathclass
def load_programs(): def load_programs():
directory = os.path.join(os.path.dirname(__file__), 'PGUI') directory = os.path.join(os.path.dirname(__file__), 'PGUI')
shortcuts = [os.path.join(directory, p) for p in os.listdir(directory)] shortcuts = [os.path.join(directory, p) for p in os.listdir(directory)]
shortcuts = [p for p in shortcuts if p.lower().endswith('.lnk')] shortcuts = [p for p in shortcuts if p.lower().endswith('.lnk')]
programs = [] shortcuts = [pathclass.Path(p) for p in shortcuts]
for shortcut in shortcuts: return 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}'
class PGUILauncher(Frame): class PGUILauncher(Frame):
def __init__(self, parent): def __init__(self, parent):
@ -44,19 +28,19 @@ class PGUILauncher(Frame):
y = 0 y = 0
self.buttonwidth = 12 self.buttonwidth = 12
programs = load_programs() shortcuts = load_programs()
for (programindex, program) in enumerate(programs): for (index, shortcut) in enumerate(shortcuts):
print(y, x) print(y, x)
newButton = Button( newButton = Button(
self, self,
text=program.name, text=shortcut.replace_extension('').basename,
command=lambda prog=program: self.launch_program(prog), 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.configure(width=self.buttonwidth)
newButton.grid(row=y, column=x) newButton.grid(row=y, column=x)
x += 1 x += 1
if x >= 3 and (programindex != len(programs)-1): if x >= 3 and (index != len(shortcuts)-1):
x = 0 x = 0
y += 1 y += 1
print(y, x) print(y, x)
@ -64,10 +48,10 @@ class PGUILauncher(Frame):
self.pack() self.pack()
self.update() self.update()
def launch_program(self, program): def launch_program(self, shortcut):
print('opening application', program.name) print('opening application', shortcut.basename)
os.chdir(os.path.dirname(program.path)) os.chdir(shortcut.parent.absolute_path)
command = f'{program.path} {program.arguments}' command = f'"{shortcut.absolute_path}"'
subprocess.Popen(command, shell=True) subprocess.Popen(command, shell=True)
self.quit() self.quit()