MCPcopy Index your code
hub / github.com/ZToolsCenter/ZTools / handleCopyPath

Function handleCopyPath

src/main/api/renderer/systemCommands.ts:471–528  ·  view source on GitHub ↗
(
  ctx: SystemCommandContext,
  execAsync: (cmd: string) => Promise<{ stdout: string; stderr: string }>,
  param?: any
)

Source from the content-addressed store, hash-verified

469}
470
471async function handleCopyPath(
472 ctx: SystemCommandContext,
473 execAsync: (cmd: string) => Promise<{ stdout: string; stderr: string }>,
474 param?: any
475): Promise<any> {
476 const filePath = getSingleFilePathParam(param)
477 console.log('[SystemCmd] 执行复制路径', filePath ? `(剪贴板文件: ${filePath})` : '(从窗口获取)')
478
479 if (filePath) {
480 clipboard.writeText(filePath)
481 console.log('[SystemCmd] 已复制路径:', filePath)
482 ctx.mainWindow?.hide()
483 return { success: true, path: filePath }
484 }
485
486 const windowInfo =
487 (param?.type === 'window' && param?.payload) || windowManager.getPreviousActiveWindow()
488
489 if (!windowInfo) {
490 return { success: false, error: '无法获取当前窗口信息' }
491 }
492
493 if (process.platform === 'win32') {
494 const folderPath = getWindowsExplorerPath(windowInfo as WindowsWindowInfo)
495 if (!folderPath) {
496 return { success: false, error: '未读取到当前 "文件资源管理器" 窗口目录' }
497 }
498
499 clipboard.writeText(folderPath)
500 console.log('[SystemCmd] 已复制路径:', folderPath)
501 ctx.mainWindow?.hide()
502 return { success: true, path: folderPath }
503 }
504
505 if (process.platform === 'darwin') {
506 try {
507 const script = `
508 tell application "Finder"
509 if (count of Finder windows) is 0 then
510 return POSIX path of (desktop as alias)
511 else
512 return POSIX path of (target of front window as alias)
513 end if
514 end tell
515 `
516 const { stdout } = await execAsync(`osascript -e '${script}'`)
517 const folderPath = stdout.trim()
518 clipboard.writeText(folderPath)
519 console.log('[SystemCmd] 已复制路径:', folderPath)
520 ctx.mainWindow?.hide()
521 return { success: true, path: folderPath }
522 } catch (error) {
523 console.error('[SystemCmd] 获取 Finder 路径失败:', error)
524 return { success: false, error: String(error) }
525 }
526 }
527 return { success: false, error: `不支持的平台: ${process.platform}` }
528}

Callers 1

executeSystemCommandFunction · 0.85

Calls 5

getSingleFilePathParamFunction · 0.85
execAsyncFunction · 0.85
hideMethod · 0.80
getWindowsExplorerPathFunction · 0.70

Tested by

no test coverage detected