添加runPowerShell和runAppleScript

This commit is contained in:
fofolee 2024-02-19 15:49:29 +08:00
parent cd152e4c88
commit fb9b675346
2 changed files with 75 additions and 15 deletions

View File

@ -210,19 +210,56 @@ window.quickcommand = {
} }
} }
// 运行vbs脚本 if (process.platform === 'win32') {
if (process.platform == 'win32') quickcommand.runVbs = function(script) { // 运行vbs脚本
quickcommand.runVbs = function (script) {
return new Promise((reslove, reject) => { return new Promise((reslove, reject) => {
var tempfile = getQuickcommandTempFile('vbs', 'TempVBSScript') var tempfile = getQuickcommandTempFile("vbs", "TempVBSScript");
fs.writeFile(tempfile, iconv.encode(script, 'gbk'), err => { fs.writeFile(tempfile, iconv.encode(script, "gbk"), (err) => {
child_process.exec(`cscript.exe /nologo "${tempfile}"`, { child_process.exec(
encoding: "buffer" `cscript.exe /nologo "${tempfile}"`,
}, (err, stdout, stderr) => { {
if (err) reject(iconv.decode(stderr, 'gbk')) encoding: "buffer",
else reslove(iconv.decode(stdout, 'gbk')) },
}); (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 // python -c

View File

@ -313,16 +313,39 @@ interface quickcommandApi {
): void; ): void;
/** /**
* windows VBS * windows VBS (Promise)
* *
* ```js * ```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<string>; runVbs(script: string): Promise<string>;
/**
* windows Powershell (Promise)
*
* ```js
* quickcommand.runPowerShell(`$voice = New-Object -ComObject SAPI.SPVOICE
* $voice.Speak('Hello, World!')`)
* ```
*
* @param script Powershell
*/
runPowerShell(script: string): Promise<string>;
/**
* MacOS AppleScript (Promise)
*
* ```js
* quickcommand.runAppleScript(`say "Hello, World!"`)
* ```
*
* @param script AppleScript
*/
runAppleScript(script: string): Promise<string>;
/** /**
* Linux * Linux
* *