From 3764db3f5068bed206ab2b7f18f13ba7acd03be1 Mon Sep 17 00:00:00 2001 From: fofolee Date: Thu, 9 Apr 2020 18:06:14 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E6=89=B9=E5=A4=84=E7=90=86?= =?UTF-8?q?=E6=8D=A2=E8=A1=8C=E7=AC=A6BUG=EF=BC=8C=E8=BE=93=E5=87=BA?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=E5=A2=9E=E5=8A=A0=E5=9C=A8=E7=BB=88=E7=AB=AF?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=EF=BC=8C=E6=94=B9=E8=BF=9B=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E9=80=89=E4=B8=AD=E6=96=87=E4=BB=B6=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- preload.js | 93 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 72 insertions(+), 21 deletions(-) diff --git a/preload.js b/preload.js index 90838e3..3918e19 100644 --- a/preload.js +++ b/preload.js @@ -16,10 +16,9 @@ logo = nativeImage.createFromPath(path.join(__dirname, 'logo.png')); // fix PATH process.env.PATH += ':/usr/local/bin:/usr/local/sbin' -messageBox = (options, callback) => { +messageBox = options => { dialog.showMessageBox(BrowserWindow.getFocusedWindow(), options, index => { utools.showMainWindow() - callback(index); }) } @@ -119,33 +118,55 @@ restoreClip = historyData => { } getSelectText = () => { - var historyData = storeClip() + var historyData = storeClip(); + clipboard.writeText(''); copy(); var selectText = clipboard.readText() restoreClip(historyData) return selectText } -getSelectFile = () => { - var historyData = storeClip() - copy(); - var filePath; - if (isWin) { - filePath = clipboard.readBuffer('FileNameW').toString('ucs2'); - filePath = filePath.replace(new RegExp(String.fromCharCode(0), 'g'), ''); +getSelectFile = () => + new Promise((reslove, reject) => { + if (isWin) { + GetForegroundWindow(hwnd => { + var cmd = `powershell.exe -NoProfile "(New-Object -COM 'Shell.Application').Windows() | Where-Object { $_.HWND -eq ${hwnd} } | Select-Object -Expand Document | select @{ n='SelectItems'; e={$_.SelectedItems()} } | select -Expand SelectItems | select -Expand Path "`; + exec(cmd, { encoding: "buffer" }, (err, stdout, stderr) => { + if (err) reject(stderr) + reslove(iconv.decode(stdout, 'GBK').trim().replace(/\\/g, '/')); + }); + }) + } else { + var cmd = `osascript -e 'tell application "Finder" to set selectedItems to selection as alias list + if selectedItems is {} then return + set parentPath to do shell script "dirname " & quoted form of POSIX path of (item 1 of selectedItems) + set pathData to "" + repeat with theItem in selectedItems + set pathData to pathData & POSIX path of theItem & linefeed + end repeat + set stdout to pathData + ' + ` + exec(cmd, (err, stdout, stderr) => { + if (err) reject(stderr) + reslove(stdout.trim()); + }); + } + }) + +GetBinPath = () => { + if (isDev) { + return path.join(__dirname.replace(/(unsafe-\w+\.asar)/,'$1.unpacked'), 'bin', 'GetForegroundWindow.exe') } else { - filePath = clipboard.read('public.file-url').replace('file://', ''); + return path.join(__dirname, 'bin', 'GetForegroundWindow.exe') } - restoreClip(historyData) - return filePath; } // 获取前台窗口句柄 GetForegroundWindow = callback => - exec(`"${path.join(__dirname, 'bin', 'GetForegroundWindow.exe')}"`, (error, stdout, stderr) => { + exec(`"${GetBinPath()}"`, (error, stdout, stderr) => { callback(stdout); }); - pwd = () => @@ -155,7 +176,7 @@ pwd = () => 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(stderr); + console.log(iconv.decode(stderr, 'GBK')); reslove(`${os.homedir().replace(/\\/g, '/')}/Desktop`) } else { reslove(decodeURIComponent(iconv.decode(stdout, 'GBK').trim())); @@ -215,37 +236,67 @@ special = async cmd => { } // 获取选中的文件 if (cmd.includes('{{SelectFile}}')) { - let repl = getSelectFile(); + let repl = await getSelectFile(); cmd = cmd.replace(/\{\{SelectFile\}\}/mg, repl) } return cmd; } -run = async (cmd, option, codec, callback) => { +run = async (cmd, option, codec, terminal, callback) => { var tmp = os.tmpdir(), bin = option.bin, argv = option.argv, ext = option.ext; cmd = await special(cmd); let script = path.join(tmp, `QuickCommandTempScript.${ext}`) - // if (ext == 'bat' || ext == 'ps1') cmd = iconv.encode(cmd, 'cp936'); + // 批处理和 powershell 默认编码为 GBK, 解决批处理的换行问题 + if (ext == 'bat' || ext == 'ps1') cmd = iconv.encode(cmd.replace(/\n/g, '\r\n'), 'GBK'); fs.writeFileSync(script, cmd); var argvs = [script] if (argv) { argvs = argv.split(' ') argvs.push(script); } + var child; if (bin) { - var child = spawn(bin, argvs, { encoding: 'buffer' }) + // 在终端中输出 + if (terminal) { + if (isWin) { + child = spawn('cmd', ['/c', 'start', 'cmd', '/k', bin].concat(argvs), { encoding: 'buffer' }) + } else { + var appleScript = `if application "Terminal" is running then + tell application "Terminal" + # do script without "in window" will open a new window + do script "clear;${bin} ${argv} ${script}" + activate + end tell + else + tell application "Terminal" + # window 1 is guaranteed to be recently opened window + do script "clear;${bin} ${argv} ${script}" in window 1 + activate + end tell + end if`; + child = spawn('osascript', ['-e', appleScript], { encoding: 'buffer' }) + } + } else { + child = spawn(bin, argvs, { encoding: 'buffer' }) + } } else { - var child = spawn(script, { encoding: 'buffer' }) + if (terminal) { + child = spawn('cmd', ['/c', 'start', 'cmd', '/k', script], { encoding: 'buffer' }) + } else { + child = spawn(script, { encoding: 'buffer' }) + } } var chunks = [], err_chunks = []; child.stdout.on('data', chunk => { + if (ext == 'bat' || ext == 'ps1') chunk = iconv.decode(chunk, 'GBK') chunks.push(chunk) }) child.stderr.on('data', err_chunk => { + if (ext == 'bat' || ext == 'ps1') err_chunk = iconv.decode(err_chunk, 'GBK') err_chunks.push(err_chunk) }) child.on('close', code => {