From fb9b67534607399fb94a09f8a8ceb7b758df0d87 Mon Sep 17 00:00:00 2001 From: fofolee Date: Mon, 19 Feb 2024 15:49:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0runPowerShell=E5=92=8CrunAppl?= =?UTF-8?q?eScript?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin/preload.js | 61 +++++++++++++++---- .../monaco/types/quickcommand.api.d.ts | 29 ++++++++- 2 files changed, 75 insertions(+), 15 deletions(-) diff --git a/plugin/preload.js b/plugin/preload.js index 2660025..f2a1cb4 100644 --- a/plugin/preload.js +++ b/plugin/preload.js @@ -210,19 +210,56 @@ window.quickcommand = { } } -// 运行vbs脚本 -if (process.platform == 'win32') quickcommand.runVbs = function(script) { +if (process.platform === 'win32') { + // 运行vbs脚本 + quickcommand.runVbs = function (script) { return new Promise((reslove, reject) => { - var tempfile = getQuickcommandTempFile('vbs', 'TempVBSScript') - fs.writeFile(tempfile, iconv.encode(script, 'gbk'), err => { - child_process.exec(`cscript.exe /nologo "${tempfile}"`, { - encoding: "buffer" - }, (err, stdout, stderr) => { - if (err) reject(iconv.decode(stderr, 'gbk')) - else reslove(iconv.decode(stdout, 'gbk')) - }); - }) - }) + var tempfile = getQuickcommandTempFile("vbs", "TempVBSScript"); + fs.writeFile(tempfile, iconv.encode(script, "gbk"), (err) => { + child_process.exec( + `cscript.exe /nologo "${tempfile}"`, + { + encoding: "buffer", + }, + (err, stdout, stderr) => { + if (err) reject(iconv.decode(stderr, "gbk")); + else reslove(iconv.decode(stdout, "gbk")); + } + ); + }); + }); + }; + // 运行powershell脚本 + quickcommand.runPowerShell = function (script) { + return new Promise((reslove, reject) => { + let base64str = Buffer.from(script, "utf16le").toString("base64"); + child_process.exec( + `powershell.exe -e "${base64str}"`, + { + encoding: "buffer", + }, + (err, stdout, stderr) => { + if (err) reject(iconv.decode(stderr, "gbk")); + else reslove(iconv.decode(stdout, "gbk")); + } + ); + }); + }; +} + +if (process.platform === 'darwin') { + // 运行AppleScript脚本 + quickcommand.runAppleScript = function (script) { + return new Promise((reslove, reject) => { + child_process.execFile( + 'osascript'['-e', script], + (err, stdout, stderr) => { + if (err) reject(stderr); + else reslove(stdout); + } + ); + }); + }; } // python -c diff --git a/src/plugins/monaco/types/quickcommand.api.d.ts b/src/plugins/monaco/types/quickcommand.api.d.ts index efaf5ba..dcd42bd 100644 --- a/src/plugins/monaco/types/quickcommand.api.d.ts +++ b/src/plugins/monaco/types/quickcommand.api.d.ts @@ -313,16 +313,39 @@ interface quickcommandApi { ): void; /** - * windows 下运行 VBS 脚本并返回运行结果 + * windows 下运行 VBS 脚本并返回运行结果 (Promise) * * ```js - * quickcommand.runVbs(`CreateObject("SAPI.SpVoice").Speak"Hello"`) + * quickcommand.runVbs(`CreateObject("SAPI.SpVoice").Speak"Hello, World!"`) * ``` * - * @param script VBS 代码 + * @param script VBS 脚本代码 */ runVbs(script: string): Promise; + /** + * windows 下运行 Powershell 脚本并返回运行结果 (Promise) + * + * ```js + * quickcommand.runPowerShell(`$voice = New-Object -ComObject SAPI.SPVOICE + * $voice.Speak('Hello, World!')`) + * ``` + * + * @param script Powershell 脚本代码 + */ + runPowerShell(script: string): Promise; + + /** + * MacOS 下运行 AppleScript 脚本并返回运行结果 (Promise) + * + * ```js + * quickcommand.runAppleScript(`say "Hello, World!"`) + * ``` + * + * @param script AppleScript 代码 + */ + runAppleScript(script: string): Promise; + /** * 在终端运行,不支持 Linux *