From 89339c86a46cd07c7abf864c9b012ac28e060158 Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Mon, 18 Oct 2021 19:09:26 -0400 Subject: [PATCH] Lots of fixes to the Make Shortcut, checked in icon for shortcut maker demo --- DemoPrograms/Demo_Make_Windows_Shortcut.ico | Bin 0 -> 4286 bytes DemoPrograms/Demo_Make_Windows_Shortcut.pyw | 157 ++++++++++++++++---- 2 files changed, 126 insertions(+), 31 deletions(-) create mode 100644 DemoPrograms/Demo_Make_Windows_Shortcut.ico diff --git a/DemoPrograms/Demo_Make_Windows_Shortcut.ico b/DemoPrograms/Demo_Make_Windows_Shortcut.ico new file mode 100644 index 0000000000000000000000000000000000000000..2b86085e5fb009f003f9556e2967558c973209dd GIT binary patch literal 4286 zcmd5vc}zCdbX2eIGk7Tshv%+&kx-nVp@y4Pgj>^YelK!_!y@Qz3*U07IbxaGY_9T)|jI z-5&4&+ydJGDeI1D2vDyV!6U|3fI6ZH(wMG}cTvaMlrai2)nNM_XaRRX2k`kO#!|&S zh?GBzn_jer?W{Tsmx>8gD=&3oK|QTyn`48-8YUa(X8|JWS-m|uRyB4p(Rkhpl2Fg) z*pp*bqKZCsQh6x5|GGx+;RDG9Ehx|~bN;gEyoal-qMR|#=?RW=>V4XKnDhlXeeN0e zPCXn&=1}VuxOVw3^lg5_<%)Fu8pw6xCeER68hq|ZzAd=}886fH9(V_9+@PcS4N0cZ zC!Koe2){q4{+z}3f9r>HL%8yqHfK```+ZOS`L**mg88~%TwB$M-BYU|REz*v@A@)7>TCu)g1J_f=LmTy+L!`PV%Xmnk59`0~Nn1~oiJTa8|COG4rftuD z9(;Y-^QYR@_MG&Q<)P)go*O}eXO;)LQR~{Yw literal 0 HcmV?d00001 diff --git a/DemoPrograms/Demo_Make_Windows_Shortcut.pyw b/DemoPrograms/Demo_Make_Windows_Shortcut.pyw index 02ebb3b7..2621da5c 100644 --- a/DemoPrograms/Demo_Make_Windows_Shortcut.pyw +++ b/DemoPrograms/Demo_Make_Windows_Shortcut.pyw @@ -5,38 +5,56 @@ import os """ Demo Program = Make Windows Shortcut - Creates a shortcut to your python file. + Creates a shortcut to your python file (or EXE file or anything that you want to make a shortcut to) Input just the .PY or .PYW file or... Optionally Add: - The interpreter to use - An icon for your shortcut - - A new name for the shortcut that's different than your Python filename + - A new name for the shortcut Copyright 2021 PySimpleGUI """ +''' +MM""""""""`M +MM mmmmmmmM +M' MMMM .d8888b. 88d888b. 88d888b. dP dP +MM MMMMMMMM 88' `88 88' `88 88' `88 88 88 +MM MMMMMMMM 88. .88 88 88. .88 88. .88 +MM MMMMMMMM `88888P' dP 88Y888P' `8888P88 +MMMMMMMMMMMM 88 .88 + dP d8888P +.8888b oo dP +88 " 88 +88aaa dP 88 .d8888b. .d8888b. +88 88 88 88ooood8 Y8ooooo. +88 88 88 88. ... 88 +dP dP dP `88888P' `88888P' +''' - -def create_shortcut(path, target='', icon=''): +def create_shortcut_for_python_file(arguments='', target='', icon='', new_name=''): """ Create a shortcut for a given target filename - :param path str: full path and filename to make link to + :param arguments str: full arguments and filename to make link to :param target str: what to launch (e.g. python) :param icon str: .ICO file :return: filename of the created shortcut file :rtype: str """ - filename, ext = os.path.splitext(path) - working_dir = os.path.dirname(filename) + filename, ext = os.path.splitext(arguments) + working_dir = os.path.dirname(target) shell = Dispatch('WScript.Shell') - shortcut_filename = filename + ".lnk" + if new_name: + shortcut_filename = new_name + ".lnk" + else: + shortcut_filename = filename + ".lnk" + shortcut_filename = os.path.join(os.path.dirname(arguments), shortcut_filename) shortcut = shell.CreateShortCut(f'{shortcut_filename}') - target_path = f'{target}' - shortcut.Targetpath = target_path - shortcut.Arguments = f'"{path}"' + shortcut.Targetpath = str(target) + shortcut.Arguments = f'"{arguments}"' shortcut.WorkingDirectory = working_dir if icon == '': pass @@ -46,44 +64,121 @@ def create_shortcut(path, target='', icon=''): return shortcut_filename +''' +MM""""""""`M dP dP +MM mmmmmmmM 88 88 +M' MMMM .d8888b. 88d888b. .d8888b. d8888P 88d888b. .d8888b. 88d888b. +MM MMMMMMMM 88' `88 88' `88 88' `88 88 88' `88 88ooood8 88' `88 +MM MMMMMMMM 88. .88 88 88. .88 88 88 88 88. ... 88 +MM MMMMMMMM `88888P' dP `88888P' dP dP dP `88888P' dP +MMMMMMMMMMMM + +.8888b oo dP +88 " 88 +88aaa dP 88 .d8888b. .d8888b. +88 88 88 88ooood8 Y8ooooo. +88 88 88 88. ... 88 +dP dP dP `88888P' `88888P' +''' +def create_shortcut_exe_or_other(arguments='', target='', icon='', new_name=''): + """ + Create a shortcut for a given target filename + :param arguments str: full arguments and filename to make link to + :param target str: what to launch (e.g. python) + :param icon str: .ICO file + :return: filename of the created shortcut file + :rtype: str + """ + filename, ext = os.path.splitext(target) + working_dir = os.path.dirname(target) + shell = Dispatch('WScript.Shell') + if new_name: + shortcut_filename = new_name + ".lnk" + else: + shortcut_filename = filename + ".lnk" + shortcut_filename = os.path.join(os.path.dirname(target), shortcut_filename) + shortcut = shell.CreateShortCut(shortcut_filename) + shortcut.Targetpath = str(target) + shortcut.Arguments = f'"{arguments}"' + shortcut.WorkingDirectory = working_dir + if icon == '': + pass + else: + shortcut.IconLocation = icon + shortcut.save() + return shortcut_filename + + +def shortcut_exists(target, new_name=''): + filename, ext = os.path.splitext(target) + filename = new_name if new_name!='' else filename + shortcut_filename = filename + ".lnk" + shortcut_filename = os.path.join(os.path.dirname(target), shortcut_filename) + print('looking for exists filename', shortcut_filename) + return os.path.exists(shortcut_filename) + + +def shortcut_delete(target, new_name=''): + filename, ext = os.path.splitext(target) + filename = new_name if new_name!='' else filename + shortcut_filename = filename + ".lnk" + shortcut_filename = os.path.join(os.path.dirname(target), shortcut_filename) + os.remove(shortcut_filename) + + +''' + oo + +88d8b.d8b. .d8888b. dP 88d888b. +88'`88'`88 88' `88 88 88' `88 +88 88 88 88. .88 88 88 88 +dP dP dP `88888P8 dP dP dP +''' + def main(): + sg.set_options(keep_on_top=True) python_command = sg.execute_py_get_interpreter() sg.theme('dark grey 13') - txt_size = 22 + txt_size = 40 layout = [[sg.Text('Create a link to your Python file (Click Go or return key to start)', font='_ 15')], - [sg.T('Python file', s=txt_size), sg.Input(key='-IN FILE-'), sg.FileBrowse(file_types=(("Python Files", "*.py *.pyw *.PY *.PYW"),), )], + [sg.T('Python (.PY/.PYW), EXE or other file - (absolute path)', s=txt_size), sg.Input(key='-IN FILE-'), sg.FileBrowse(file_types=(("Python Files", "*.py *.pyw *.PY *.PYW"),), )], [sg.T('Icon file (optional)', s=txt_size), sg.Input(key='-ICON-'), sg.FileBrowse(file_types=(("Icon Files", "*.ico *.ICO",),), )], [sg.T('Shortcut Name (optional)', s=txt_size), sg.Input(key='-SHORTCUT NAME-')], - [sg.T('Python Command (optional)', s=txt_size), sg.Input(key='-PYTHON COMMAND-')], - [sg.Button('Go', bind_return_key=True), sg.Button('Exit')]] + [sg.T('Python Command (optional)', s=txt_size), sg.Input(python_command, key='-PYTHON COMMAND-')], + [sg.T('Arguments (optional)', s=txt_size), sg.Input(key='-ARGUMENTS-')], + [sg.Button('Create Shortcut', bind_return_key=True), sg.Button('Exit')]] window = sg.Window('Create Shortcut To Python File', layout, icon=main_icon, right_click_menu=sg.MENU_RIGHT_CLICK_EDITME_VER_EXIT) while True: event, values = window.read() # print(event, values) - if event == sg.WIN_CLOSED or event == 'Exit': + if event in (sg.WIN_CLOSED, 'Exit'): break - if event == 'Go': - try: - if values['-PYTHON COMMAND-']: - py_cmd = values['-PYTHON COMMAND-'] + if event == 'Create Shortcut': + infile = values['-IN FILE-'].lower() + py_cmd = values['-PYTHON COMMAND-'] + if shortcut_exists(infile, values['-SHORTCUT NAME-']): + if sg.popup_yes_no('The shortcut already exists. Do you want to overwrite it?') == 'Yes': + shortcut_delete(infile, values['-SHORTCUT NAME-']) else: - py_cmd = python_command - if '.pyw' in values['-IN FILE-'].lower(): # if a .pyw file specified, use pythonw to launch it - if 'pythonw' not in py_cmd: - py_cmd = py_cmd.replace('python.exe', 'pythonw.exe') - shortcut_name = create_shortcut(values['-IN FILE-'], target=fr'{py_cmd}', icon=values['-ICON-']) - if values['-SHORTCUT NAME-']: - new_shortcut_name = os.path.join(os.path.dirname(shortcut_name), values['-SHORTCUT NAME-'] + '.lnk') - os.rename(shortcut_name, new_shortcut_name) - shortcut_name = new_shortcut_name - window.close() + continue + try: + if '.pyw' in infile or '.py' in infile: + if values['-PYTHON COMMAND-']: + if '.pyw' in values['-IN FILE-'].lower(): # if a .pyw file specified, use pythonw to launch it + if 'pythonw' not in py_cmd: + py_cmd = py_cmd.replace('python.exe', 'pythonw.exe') + if py_cmd: + shortcut_name = create_shortcut_for_python_file(target=fr'{py_cmd}', arguments=values['-IN FILE-'], icon=values['-ICON-'], new_name=values['-SHORTCUT NAME-']) + else: + shortcut_name = create_shortcut_exe_or_other(target=values['-IN FILE-'], icon=values['-ICON-'], new_name=values['-SHORTCUT NAME-']) + else: + shortcut_name = create_shortcut_exe_or_other(target=infile, arguments=values['-ARGUMENTS-'], icon=values['-ICON-'], new_name=values['-SHORTCUT NAME-']) choice = sg.popup('Done!', 'Created shortcut:', shortcut_name, custom_text=('Take me there', 'Close')) if choice == 'Take me there': sg.execute_command_subprocess(r'explorer.exe', os.path.dirname(shortcut_name)) - break except Exception as e: sg.popup_error('Error encountered', e) elif event == 'Edit Me':