适配新版本

This commit is contained in:
fofolee 2020-05-02 13:06:06 +08:00
parent 54a9d012fa
commit e412c405b3
2 changed files with 51 additions and 58 deletions

View File

@ -29,7 +29,7 @@ utools.onPluginEnter( async ({ code, type, payload }) => {
}
// 获取资源管理器或访达当前目录
if (cmd.includes('{{pwd}}')) {
let repl = await pwd(payload.id);
let repl = utools.getCurrentFolderPath().replace(/\\/g, '\\\\');
cmd = cmd.replace(/\{\{pwd\}\}/mg, repl)
}
// 获取窗口信息
@ -92,20 +92,18 @@ function runCmd(cmd, option, output) {
window.run(cmd, option, terminal, (stdout, stderr) => {
if (stderr) {
// 报错
messageBox({
var index = utools.showMessageBox({
type: 'error',
title: '啊嘞?!',
icon: window.logo,
message: stderr,
buttons: ['转至脚本目录', '退出']
}, index => {
if (index == 0) {
locate(resolve(tmpdir, `QuickCommandTempScript.${option.ext}`));
}
copyTo(stderr);
message("已复制报错信息");
utools.outPlugin();
})
if (index == 0) {
locate(resolve(tmpdir, `QuickCommandTempScript.${option.ext}`));
}
copyTo(stderr);
message("已复制报错信息");
utools.outPlugin();
} else if (stdout) {
// 有输出
switch (output) {
@ -119,13 +117,7 @@ function runCmd(cmd, option, output) {
copyTo(stdout);
break;
case "send":
// 暂存用户剪贴板
var historyData = storeClip();
copyTo(stdout);
paste();
setTimeout(() => {
restoreClip(historyData);
}, 500);
send(stdout)
break;
case "notice":
// 发送系统通知

View File

@ -2,35 +2,26 @@ const fs = require('fs');
const os = require('os');
const { spawn, exec, execSync } = require("child_process")
const iconv = require('iconv-lite')
const { clipboard, shell} = require('electron')
const robot = utools.robot
const { clipboard } = require('electron')
const path = require("path")
const { dialog, BrowserWindow, nativeImage } = require('electron').remote
pluginInfo = JSON.parse(fs.readFileSync(path.join(__dirname, 'plugin.json')));
logo = nativeImage.createFromPath(path.join(__dirname, 'logo.png'));
// fix PATH
process.env.PATH += ':/usr/local/bin:/usr/local/sbin'
messageBox = (options,callback) => {
dialog.showMessageBox(BrowserWindow.getFocusedWindow(), options, index => {
utools.showMainWindow()
callback(index);
})
}
open = path => {
shell.openItem(path)
utools.shellOpenItem(path)
}
locate = path => {
shell.showItemInFolder(path);
utools.shellShowItemInFolder(path);
}
visit = url => {
shell.openExternal(url);
utools.shellOpenExternal(url);
}
system = cmd => {
@ -41,6 +32,8 @@ message = msg => {
utools.showNotification(msg)
}
keyTap = utools.simulateKeyboardTap
sleep = ms => new Promise((r, j)=>setTimeout(r, ms))
// ------------------------
@ -59,21 +52,19 @@ getBase64Ico = path => {
}
openFolder = options => {
return dialog.showOpenDialogSync(BrowserWindow.getFocusedWindow(), options);
return utools.showOpenDialog(options);
}
saveFile = (options, content) => {
dialog.showSaveDialog(BrowserWindow.getFocusedWindow(), options, filename => {
filename && fs.writeFile(filename, content, 'utf8', err => {
err && console.log(err)
})
var filename = utools.showSaveDialog(options)
filename && fs.writeFile(filename, content, 'utf8', err => {
err && console.log(err)
})
}
copy = () => {
var ctlKey = isWin ? 'control' : 'command';
robot.keyTap('c', ctlKey);
robot.setKeyboardDelay(20);
utools.simulateKeyboardTap('c', ctlKey);
}
copyTo = text => {
@ -82,9 +73,19 @@ copyTo = text => {
paste = () => {
var ctlKey = isWin ? 'control' : 'command';
robot.keyTap('v', ctlKey);
utools.simulateKeyboardTap('v', ctlKey);
}
send = text => {
var historyData = storeClip();
copyTo(text);
paste();
setTimeout(() => {
restoreClip(historyData);
}, 500);
}
// 保存剪贴板
storeClip = () => {
var formats = clipboard.availableFormats("clipboard");
@ -160,26 +161,26 @@ getSelectFile = hwnd =>
}
})
pwd = hwnd =>
new Promise((reslove, reject) => {
if (isWin) {
var cmd = `powershell.exe -NoProfile "((New-Object -COM 'Shell.Application').Windows() | Where-Object { $_.HWND -eq (${hwnd}) } | Select-Object -Expand LocationURL).replace('file:///','')"`;
exec(cmd, { encoding: "buffer" }, (err, stdout, stderr) => {
if (err) {
console.log(iconv.decode(stderr, 'GBK'));
reslove(`${os.homedir().replace(/\\/g, '/')}/Desktop`)
} else {
reslove(decodeURIComponent(iconv.decode(stdout, 'GBK').trim()));
}
});
} else {
var cmd = `osascript -e 'tell application "Finder" to get the POSIX path of (target of front window as alias)'`
exec(cmd, (err, stdout, stderr) => {
if (err) reject(stderr)
reslove(stdout.trim());
});
}
});
// pwd = hwnd =>
// new Promise((reslove, reject) => {
// if (isWin) {
// var cmd = `powershell.exe -NoProfile "((New-Object -COM 'Shell.Application').Windows() | Where-Object { $_.HWND -eq (${hwnd}) } | Select-Object -Expand LocationURL).replace('file:///','')"`;
// exec(cmd, { encoding: "buffer" }, (err, stdout, stderr) => {
// if (err) {
// console.log(iconv.decode(stderr, 'GBK'));
// reslove(`${os.homedir().replace(/\\/g, '/')}/Desktop`)
// } else {
// reslove(decodeURIComponent(iconv.decode(stdout, 'GBK').trim()));
// }
// });
// } else {
// var cmd = `osascript -e 'tell application "Finder" to get the POSIX path of (target of front window as alias)'`
// exec(cmd, (err, stdout, stderr) => {
// if (err) reject(stderr)
// reslove(stdout.trim());
// });
// }
// });
special = cmd => {
// 判断是否 windows 系统