How to launch a bash script from PySimpleGUI
I am in need of a GUI bash script launcher with the ability to launch multiple scripts in parallel on wsl. I found this Python GUI script on the PySimpleGUI GitHub page. I chose it because it shows all the bash scripts in a directory and apparently; seems to allow the selection of multiple scripts using ctlr+alt+leftmouseclick. It also appears that I may be able to run them simultaneously if I desire. This is exactly what I need. I am assuming this functionality exists but I am unable to test it because it doesn’t seem to be working. I have added the appropriate directory to the code and the GUI shows the list of files in the left pane. When I select either a single or multiple files from the populated list and click "RUN"; the Pane to the right shows "Launching Program". However the script doesn’t actually run. I replaced "Popen" with "check_call to see if it would matter but nothing changed. I don’t know much about python so any help correcting this problem is appreciated.
import PySimpleGUI as sg import glob import ntpath import subprocess LOCATION_OF_YOUR_SCRIPTS = '/home/userx/' # Execute the command. Will not see the output from the command until it completes. def execute_command_blocking(command, *args): expanded_args = [] for a in args: expanded_args.append(a) # expanded_args += a try: sp = subprocess.check_call([command, expanded_args], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = sp.communicate() if out: print(out.decode("utf-8")) if err: print(err.decode("utf-8")) except: out = '' return out # Executes command and immediately returns. Will not see anything the script outputs def execute_command_nonblocking(command, *args): expanded_args = [] for a in args: expanded_args += a try: sp = subprocess.check_call([command, expanded_args], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) except: pass def Launcher2(): sg.theme('GreenTan') filelist = glob.glob(LOCATION_OF_YOUR_SCRIPTS+'*.sh') namesonly = [] for file in filelist: namesonly.append(ntpath.basename(file)) layout = [ [sg.Listbox(values=namesonly, size=(30, 19), select_mode=sg.SELECT_MODE_EXTENDED, key='demolist'), sg.Output(size=(88, 20), font='Courier 10')], [sg.CBox('Wait for program to complete', default=False, key='wait')], [sg.Button('Run'), sg.Button('Shortcut 1'), sg.Button('Fav Program'), sg.Button('EXIT')], ] window = sg.Window('Script launcher', layout) # ---===--- Loop taking in user input --- # while True: event, values = window.read() if event in ('EXIT', None): break # exit button clicked if event in ('Shortcut 1', 'Fav Program'): print('Quickly launch your favorite programs using these shortcuts') print(''' Or copy files to your github folder. Or anything else you type on the command line''') # copyfile(source, dest) elif event == 'Run': for index, file in enumerate(values['demolist']): print('Launching %s' % file) window.refresh() # make the print appear immediately if values['wait']: execute_command_blocking(LOCATION_OF_YOUR_SCRIPTS + file) else: execute_command_nonblocking( LOCATION_OF_YOUR_SCRIPTS + file) window.close() if __name__ == '__main__': Launcher2()
And Here is an example of the bash script I am trying to launch.
#!/bin/bash cd /mnt/c/Mycom/Nokia/WindowsTransmitTechSupport/WindowsTransmitTechSupportCli expect Transmit1830TechSupportv0.0.65.exp XYZNAYo30 22.237.120.160 telnet 23 cli cli admin admin NA NA NA NA NA NA OFF