mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-06-30 04:42:45 +08:00
解决批处理换行符BUG,输出方式增加在终端显示,改进获取选中文件的方法
This commit is contained in:
parent
02f4f06088
commit
3764db3f50
93
preload.js
93
preload.js
@ -16,10 +16,9 @@ logo = nativeImage.createFromPath(path.join(__dirname, 'logo.png'));
|
|||||||
// fix PATH
|
// fix PATH
|
||||||
process.env.PATH += ':/usr/local/bin:/usr/local/sbin'
|
process.env.PATH += ':/usr/local/bin:/usr/local/sbin'
|
||||||
|
|
||||||
messageBox = (options, callback) => {
|
messageBox = options => {
|
||||||
dialog.showMessageBox(BrowserWindow.getFocusedWindow(), options, index => {
|
dialog.showMessageBox(BrowserWindow.getFocusedWindow(), options, index => {
|
||||||
utools.showMainWindow()
|
utools.showMainWindow()
|
||||||
callback(index);
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,33 +118,55 @@ restoreClip = historyData => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getSelectText = () => {
|
getSelectText = () => {
|
||||||
var historyData = storeClip()
|
var historyData = storeClip();
|
||||||
|
clipboard.writeText('');
|
||||||
copy();
|
copy();
|
||||||
var selectText = clipboard.readText()
|
var selectText = clipboard.readText()
|
||||||
restoreClip(historyData)
|
restoreClip(historyData)
|
||||||
return selectText
|
return selectText
|
||||||
}
|
}
|
||||||
|
|
||||||
getSelectFile = () => {
|
getSelectFile = () =>
|
||||||
var historyData = storeClip()
|
new Promise((reslove, reject) => {
|
||||||
copy();
|
if (isWin) {
|
||||||
var filePath;
|
GetForegroundWindow(hwnd => {
|
||||||
if (isWin) {
|
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 "`;
|
||||||
filePath = clipboard.readBuffer('FileNameW').toString('ucs2');
|
exec(cmd, { encoding: "buffer" }, (err, stdout, stderr) => {
|
||||||
filePath = filePath.replace(new RegExp(String.fromCharCode(0), 'g'), '');
|
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 {
|
} else {
|
||||||
filePath = clipboard.read('public.file-url').replace('file://', '');
|
return path.join(__dirname, 'bin', 'GetForegroundWindow.exe')
|
||||||
}
|
}
|
||||||
restoreClip(historyData)
|
|
||||||
return filePath;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取前台窗口句柄
|
// 获取前台窗口句柄
|
||||||
GetForegroundWindow = callback =>
|
GetForegroundWindow = callback =>
|
||||||
exec(`"${path.join(__dirname, 'bin', 'GetForegroundWindow.exe')}"`, (error, stdout, stderr) => {
|
exec(`"${GetBinPath()}"`, (error, stdout, stderr) => {
|
||||||
callback(stdout);
|
callback(stdout);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
pwd = () =>
|
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:///','')"`;
|
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) => {
|
exec(cmd, { encoding: "buffer" }, (err, stdout, stderr) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log(stderr);
|
console.log(iconv.decode(stderr, 'GBK'));
|
||||||
reslove(`${os.homedir().replace(/\\/g, '/')}/Desktop`)
|
reslove(`${os.homedir().replace(/\\/g, '/')}/Desktop`)
|
||||||
} else {
|
} else {
|
||||||
reslove(decodeURIComponent(iconv.decode(stdout, 'GBK').trim()));
|
reslove(decodeURIComponent(iconv.decode(stdout, 'GBK').trim()));
|
||||||
@ -215,37 +236,67 @@ special = async cmd => {
|
|||||||
}
|
}
|
||||||
// 获取选中的文件
|
// 获取选中的文件
|
||||||
if (cmd.includes('{{SelectFile}}')) {
|
if (cmd.includes('{{SelectFile}}')) {
|
||||||
let repl = getSelectFile();
|
let repl = await getSelectFile();
|
||||||
cmd = cmd.replace(/\{\{SelectFile\}\}/mg, repl)
|
cmd = cmd.replace(/\{\{SelectFile\}\}/mg, repl)
|
||||||
}
|
}
|
||||||
return cmd;
|
return cmd;
|
||||||
}
|
}
|
||||||
|
|
||||||
run = async (cmd, option, codec, callback) => {
|
run = async (cmd, option, codec, terminal, callback) => {
|
||||||
var tmp = os.tmpdir(),
|
var tmp = os.tmpdir(),
|
||||||
bin = option.bin,
|
bin = option.bin,
|
||||||
argv = option.argv,
|
argv = option.argv,
|
||||||
ext = option.ext;
|
ext = option.ext;
|
||||||
cmd = await special(cmd);
|
cmd = await special(cmd);
|
||||||
let script = path.join(tmp, `QuickCommandTempScript.${ext}`)
|
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);
|
fs.writeFileSync(script, cmd);
|
||||||
var argvs = [script]
|
var argvs = [script]
|
||||||
if (argv) {
|
if (argv) {
|
||||||
argvs = argv.split(' ')
|
argvs = argv.split(' ')
|
||||||
argvs.push(script);
|
argvs.push(script);
|
||||||
}
|
}
|
||||||
|
var child;
|
||||||
if (bin) {
|
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 {
|
} 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 = [],
|
var chunks = [],
|
||||||
err_chunks = [];
|
err_chunks = [];
|
||||||
child.stdout.on('data', chunk => {
|
child.stdout.on('data', chunk => {
|
||||||
|
if (ext == 'bat' || ext == 'ps1') chunk = iconv.decode(chunk, 'GBK')
|
||||||
chunks.push(chunk)
|
chunks.push(chunk)
|
||||||
})
|
})
|
||||||
child.stderr.on('data', err_chunk => {
|
child.stderr.on('data', err_chunk => {
|
||||||
|
if (ext == 'bat' || ext == 'ps1') err_chunk = iconv.decode(err_chunk, 'GBK')
|
||||||
err_chunks.push(err_chunk)
|
err_chunks.push(err_chunk)
|
||||||
})
|
})
|
||||||
child.on('close', code => {
|
child.on('close', code => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user