mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-06-09 06:54:11 +08:00
使用官方API获取选中的文件及资源管理器路径
This commit is contained in:
parent
dc23b0482d
commit
67ce540c8b
83
preload.js
83
preload.js
@ -16,9 +16,10 @@ 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 => {
|
messageBox = (options, callback) => {
|
||||||
dialog.showMessageBox(BrowserWindow.getFocusedWindow(), options, index => {
|
dialog.showMessageBox(BrowserWindow.getFocusedWindow(), options, index => {
|
||||||
utools.showMainWindow()
|
utools.showMainWindow()
|
||||||
|
callback(index)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,15 +127,13 @@ getSelectText = () => {
|
|||||||
return selectText
|
return selectText
|
||||||
}
|
}
|
||||||
|
|
||||||
getSelectFile = () =>
|
getSelectFile = hwnd =>
|
||||||
new Promise((reslove, reject) => {
|
new Promise((reslove, reject) => {
|
||||||
if (isWin) {
|
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 "`;
|
||||||
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) => {
|
||||||
exec(cmd, { encoding: "buffer" }, (err, stdout, stderr) => {
|
if (err) reject(stderr)
|
||||||
if (err) reject(stderr)
|
reslove(iconv.decode(stdout, 'GBK').trim().replace(/\\/g, '/'));
|
||||||
reslove(iconv.decode(stdout, 'GBK').trim().replace(/\\/g, '/'));
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
var cmd = `osascript -e 'tell application "Finder" to set selectedItems to selection as alias list
|
var cmd = `osascript -e 'tell application "Finder" to set selectedItems to selection as alias list
|
||||||
@ -154,35 +153,33 @@ getSelectFile = () =>
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
GetBinPath = () => {
|
// GetBinPath = () => {
|
||||||
if (isDev) {
|
// if (isDev) {
|
||||||
return path.join(__dirname.replace(/(unsafe-\w+\.asar)/,'$1.unpacked'), 'bin', 'GetForegroundWindow.exe')
|
// return path.join(__dirname.replace(/(unsafe-\w+\.asar)/,'$1.unpacked'), 'bin', 'GetForegroundWindow.exe')
|
||||||
} else {
|
// } else {
|
||||||
return path.join(__dirname, 'bin', 'GetForegroundWindow.exe')
|
// return path.join(__dirname, 'bin', 'GetForegroundWindow.exe')
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
// 获取前台窗口句柄
|
// 获取前台窗口句柄
|
||||||
GetForegroundWindow = callback =>
|
// GetForegroundWindow = callback =>
|
||||||
exec(`"${GetBinPath()}"`, (error, stdout, stderr) => {
|
// exec(`"${GetBinPath()}"`, (error, stdout, stderr) => {
|
||||||
callback(stdout);
|
// callback(stdout);
|
||||||
});
|
// });
|
||||||
|
|
||||||
|
|
||||||
pwd = () =>
|
pwd = hwnd =>
|
||||||
new Promise((reslove, reject) => {
|
new Promise((reslove, reject) => {
|
||||||
if (isWin) {
|
if (isWin) {
|
||||||
GetForegroundWindow(hwnd => {
|
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(iconv.decode(stderr, 'GBK'));
|
||||||
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()));
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
var cmd = `osascript -l JavaScript -e '
|
var cmd = `osascript -l JavaScript -e '
|
||||||
const frontmost_app_name = Application("System Events").applicationProcesses.where({ frontmost: true }).name()[0]
|
const frontmost_app_name = Application("System Events").applicationProcesses.where({ frontmost: true }).name()[0]
|
||||||
@ -208,37 +205,29 @@ special = async cmd => {
|
|||||||
cmd = cmd.replace(/\{\{isWin\}\}/mg, repl)
|
cmd = cmd.replace(/\{\{isWin\}\}/mg, repl)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取电脑名
|
// 获取本机唯一ID
|
||||||
if (cmd.includes('{{HostName}}')) {
|
if (cmd.includes('{{LocalID}}')) {
|
||||||
let repl = os.hostname();
|
let repl = utools.getLocalId();
|
||||||
cmd = cmd.replace(/\{\{HostName\}\}/mg, repl)
|
cmd = cmd.replace(/\{\{HostName\}\}/mg, repl)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取资源管理器或访达当前目录
|
// 获取浏览器当前链接
|
||||||
if (cmd.includes('{{pwd}}')) {
|
|
||||||
let repl = await pwd();
|
|
||||||
cmd = cmd.replace(/\{\{pwd\}\}/mg, repl)
|
|
||||||
}
|
|
||||||
// 获取 Chrome 当前链接
|
|
||||||
if (cmd.includes('{{BrowserUrl}}')) {
|
if (cmd.includes('{{BrowserUrl}}')) {
|
||||||
let repl = utools.getCurrentBrowserUrl();
|
let repl = utools.getCurrentBrowserUrl();
|
||||||
cmd = cmd.replace(/\{\{BrowserUrl\}\}/mg, repl)
|
cmd = cmd.replace(/\{\{BrowserUrl\}\}/mg, repl)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取剪切板的文本
|
// 获取剪切板的文本
|
||||||
if (cmd.includes('{{ClipText}}')) {
|
if (cmd.includes('{{ClipText}}')) {
|
||||||
let repl = clipboard.readText();
|
let repl = clipboard.readText();
|
||||||
cmd = cmd.replace(/\{\{ClipText\}\}/mg, repl)
|
cmd = cmd.replace(/\{\{ClipText\}\}/mg, repl)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取选中的文本
|
// 获取选中的文本
|
||||||
if (cmd.includes('{{SelectText}}')) {
|
if (cmd.includes('{{SelectText}}')) {
|
||||||
let repl = getSelectText();
|
let repl = getSelectText();
|
||||||
cmd = cmd.replace(/\{\{SelectText\}\}/mg, repl)
|
cmd = cmd.replace(/\{\{SelectText\}\}/mg, repl)
|
||||||
}
|
}
|
||||||
// 获取选中的文件
|
|
||||||
if (cmd.includes('{{SelectFile}}')) {
|
|
||||||
let repl = await getSelectFile();
|
|
||||||
cmd = cmd.replace(/\{\{SelectFile\}\}/mg, repl)
|
|
||||||
}
|
|
||||||
return cmd;
|
return cmd;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -292,11 +281,11 @@ run = async (cmd, option, codec, terminal, callback) => {
|
|||||||
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')
|
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_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