diff --git a/README.md b/README.md index 62e0bb0..63dfd6e 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# 快捷命令 V1.1.0 +# 快捷命令 V1.1.1 ## 简介 @@ -11,6 +11,7 @@ - 新增`发送系统通知`的输出模式 By [imxiny](https://github.com/imxiny) - 修复 php 无法输入 { robot.keyTap('v', ctlKey); } +// 保存剪贴板 +storeClip = () => { + var formats = clipboard.availableFormats("clipboard"); + if (formats.includes("text/plain")) { + return ['text', clipboard.readText()] + } + if (formats.includes("image/png") || formats.includes("image/jpeg")) { + return ['image', clipboard.readImage()] + } + var file; + if (isWin) { + file = clipboard.readBuffer('FileNameW').toString('ucs2').replace(/\\/g, '/'); + file = file.replace(new RegExp(String.fromCharCode(0), 'g'), ''); + } else { + file = clipboard.read('public.file-url').replace('file://', ''); + } + if (file) { + return ['file', file] + } + return [] +} + +// 恢复剪贴板 +restoreClip = historyData => { + if (historyData[0] == 'text') { + clipboard.writeText(historyData[1]); + return + } + if (historyData[0] == 'image') { + clipboard.writeImage(historyData[1]); + return + } + if (historyData[0] == 'file') { + utools.copyFile(historyData[1]) + return + } + clipboard.writeText('') +} + getSelectText = () => { + var historyData = storeClip() copy(); - return clipboard.readText() + var selectText = clipboard.readText() + restoreClip(historyData) + return selectText } getSelectFile = () => { + var historyData = storeClip() copy(); var filePath; if (isWin) { @@ -94,21 +136,32 @@ getSelectFile = () => { } else { filePath = clipboard.read('public.file-url').replace('file://', ''); } + restoreClip(historyData) return filePath; } -getAddr = () => { - robot.keyTap('d', 'alt'); - robot.setKeyboardDelay(10); - return getSelectText().replace(/\\/g, '/'); -} +// 获取前台窗口句柄 +GetForegroundWindow = callback => + exec(`"${path.join(__dirname, 'bin', 'GetForegroundWindow.exe')}"`, (error, stdout, stderr) => { + callback(stdout); + }); + + pwd = () => new Promise((reslove, reject) => { if (isWin) { - var addr = getAddr(); - if (!exists(addr)) addr = `${os.homedir().replace(/\\/g, '/')}/Desktop`; - reslove(addr); + GetForegroundWindow(hwnd => { + 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); + reslove(`${os.homedir().replace(/\\/g, '/')}/Desktop`) + } else { + reslove(decodeURIComponent(iconv.decode(stdout, 'GBK').trim())); + } + }); + }) } else { var cmd = `osascript -l JavaScript -e ' const frontmost_app_name = Application("System Events").applicationProcesses.where({ frontmost: true }).name()[0]