MCPcopy Create free account
hub / github.com/ZToolsCenter/ZTools / launchApp

Function launchApp

src/main/core/commandLauncher/windowsLauncher.ts:39–203  ·  view source on GitHub ↗
(
  appPath: string,
  confirmDialog?: ConfirmDialogOptions
)

Source from the content-addressed store, hash-verified

37}
38
39export async function launchApp(
40 appPath: string,
41 confirmDialog?: ConfirmDialogOptions
42): Promise<void> {
43 // 如果需要确认,先显示确认对话框
44 if (confirmDialog) {
45 const result = await dialog.showMessageBox({
46 type: confirmDialog.type,
47 buttons: confirmDialog.buttons,
48 defaultId: confirmDialog.defaultId ?? 0,
49 cancelId: confirmDialog.cancelId ?? 0,
50 title: confirmDialog.title,
51 message: confirmDialog.message,
52 detail: confirmDialog.detail,
53 noLink: true
54 })
55
56 // 如果用户点击取消按钮,则不执行
57 if (result.response === confirmDialog.cancelId) {
58 console.log('[Launcher] 用户取消了操作')
59 return
60 }
61 }
62
63 // 检查是否是 UWP 应用(uwp: 前缀)
64 if (appPath.startsWith('uwp:')) {
65 const appId = appPath.slice(4)
66 try {
67 UwpManager.launchUwpApp(appId)
68 console.log(`[Launcher] 成功启动 UWP 应用: ${appId}`)
69 return
70 } catch (error) {
71 console.error('[Launcher] 启动 UWP 应用失败:', error)
72 throw error
73 }
74 }
75
76 // 检查是否是协议链接(如 ms-settings:, steam://, battlenet:// 等)
77 // 协议链接必须使用 shell.openExternal(),shell.openPath() 会卡住
78 if (/^[a-zA-Z][a-zA-Z0-9+\-.]*:/.test(appPath) && !appPath.includes('\\')) {
79 try {
80 await shell.openExternal(appPath)
81 console.log(`[Launcher] 成功打开协议链接: ${appPath}`)
82 return
83 } catch (error) {
84 console.error('[Launcher] 打开协议链接失败:', error)
85 throw error
86 }
87 }
88
89 // 检查是否是 PowerShell 命令(需要特殊处理,直接执行而不是通过 cmd.exe)
90 if (appPath.startsWith('PowerShell.exe ') || appPath.startsWith('powershell.exe ')) {
91 try {
92 // 使用 shell: true 让系统自动解析命令行参数(包括引号)
93 const subprocess = spawn(appPath, [], {
94 detached: true,
95 stdio: 'ignore',
96 shell: true

Callers

nothing calls this directly

Calls 3

execCommandFunction · 0.85
launchUwpAppMethod · 0.80
openExternalMethod · 0.80

Tested by

no test coverage detected