Open win32-run-script in Script Kit
// Name: Run .bat/.ps1/.sh// Description: Process Output to Kit via streamimport "@johnlindquist/kit"// @ts-expect-errorimport { backToMainShortcut, highlightJavaScript } from "@johnlindquist/kit"// --- Create a shell script to run -----------------// `tmpPath` will store the file here:// ~/.kenv/tmp/process-shell-script-output/example.*// Note: linux shell will only work with WSL or you can provide the Args for ps1 using the .sh extension if you have gitbashconst fileName = "example"const selectedLang = {name: '',args: '',ext: '',echo: '',set setVal(keyValueList: string[]) {this[keyValueList[0]] = keyValueList[1];}};const objGen = (_lang: string, _ext: string, _args?: string) => {_args = _args ? _args : ""return {name: _lang,description: `Run Script using ${_lang}`,value: _lang,id: _ext,arguments: _args,preview: async () => highlightJavaScript(tmpPath(`${fileName}.${_ext}`))}}const LangOptions = [objGen("PowerShell","ps1","powershell -NoProfile -NonInteractive –ExecutionPolicy Bypass -File "), objGen("Batch", "bat"),objGen("Bash", "sh")]const promptEditor = ["yes", "no"]const selectedValue = await arg("Use editor?", promptEditor)const useEditor = selectedValue === "yes" ? true : false// define select optionsawait arg({placeholder: "Select Scripting Language...",enter: "Select",shortcuts: [backToMainShortcut],onChoiceFocus: async (input, { focused }) => {selectedLang.setVal = ["args", focused["arguments"]]selectedLang.setVal = ["ext", focused.id]selectedLang.setVal = ["name", focused.name]selectedLang.setVal = ["echo", selectedLang.ext == "bat" ? "@echo off" : ""]}}, LangOptions)let shellScriptPath = tmpPath(`${fileName}.${selectedLang.ext}`)const editorConfig = {hint: `Write code for ${selectedLang.ext} file.`,description: 'Save to Run',onInputSubmit: async (input: any) => {selectedLang.ext == "sh" ? submit(`${input}exit`) : submit(input)}}// Using ping to simulate waiting for a long process and because it's natively supported across PS and Bat files// Note: If you use a code that would natively not run in bat like "ls" it willlet scriptContents = useEditor ? await editor(editorConfig) : `${selectedLang.echo}echo "hello"echo "Done"${selectedLang.ext == "sh" ? "exit" : ""}`await writeFile(shellScriptPath, scriptContents);// Just a wrapper to highlight with code in PS styleconst codeWrapper = (string: string, extension: any) => `\`\`\`${extension}${string}\`\`\``let output = ``// This is used to avoid kit window closing on process exitlet divPromise = div()const outHandler = async (out: any) => {output += `${out}\n`;setDiv(await highlight(`${codeWrapper(output, selectedLang.ext)}`))};// Note: We have to use this janky way of executing PS as it would launch in Notepad or fail entirely.const execArgs = selectedLang.ext == "sh" ? `cd ${tmpPath()} && bash ${fileName}.sh` : `${selectedLang.args}${shellScriptPath}`// inspect(execArgs)let { stdout } = execLog(execArgs, outHandler)setAlwaysOnTop(true);setIgnoreBlur(true);await divPromise