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

Method readCurrentFolderPathMac

src/main/api/plugin/shell.ts:177–221  ·  view source on GitHub ↗

* macOS: 通过 AppleScript 查询 Finder 前台窗口路径 * 先尝试获取前台窗口路径,失败则回退到桌面路径

()

Source from the content-addressed store, hash-verified

175 * 先尝试获取前台窗口路径,失败则回退到桌面路径
176 */
177 private async readCurrentFolderPathMac(): Promise<string> {
178 // 1. 获取当前活动窗口信息
179 const windowInfo = this.clipboardManager?.getCurrentWindow()
180 if (!windowInfo) {
181 console.warn('[PluginShell] readCurrentFolderPath: 未识别到当前活动窗口')
182 throw new Error('未识别到当前活动窗口')
183 }
184
185 // 2. 检查是否为 Finder 窗口
186 if (windowInfo.bundleId !== 'com.apple.finder') {
187 console.log(
188 `[PluginShell] readCurrentFolderPath: 当前窗口非 Finder (bundleId=${windowInfo.bundleId})`
189 )
190 throw new Error('当前活动窗口非 "访达" 窗口')
191 }
192
193 // 3. 先尝试获取 Finder 前台窗口路径
194 try {
195 const frontWindowPath = await this.execAppleScript(
196 'tell application "Finder" to get the POSIX path of (target of front window as alias)'
197 )
198 const result = frontWindowPath.trim().replace(/\/$/, '')
199 console.log(`[PluginShell] readCurrentFolderPath: Finder 窗口路径=${result}`)
200 return result
201 } catch {
202 // Finder 前台窗口查询失败(例如仅显示桌面),回退到桌面路径
203 console.log('[PluginShell] readCurrentFolderPath: Finder 前台窗口查询失败,回退到桌面路径')
204 }
205
206 // 4. 回退:获取桌面路径
207 try {
208 const desktopPath = await this.execAppleScript(
209 'tell application "Finder" to get the POSIX path of (path to desktop)'
210 )
211 const result = desktopPath.trim().replace(/\/$/, '')
212 console.log(`[PluginShell] readCurrentFolderPath: 桌面路径=${result}`)
213 return result
214 } catch (error: unknown) {
215 // 清理 AppleScript 错误信息:去掉错误码前缀(如 "execution error: ...")
216 const errMsg = error instanceof Error ? error.message : String(error)
217 const cleanMsg = errMsg.replace(/^\d+:\d+:\s*execution error:\s*/i, '').trim()
218 console.error('[PluginShell] readCurrentFolderPath: AppleScript 执行失败:', cleanMsg)
219 throw new Error(cleanMsg)
220 }
221 }
222
223 /**
224 * macOS: 通过 AppleScript 获取当前浏览器前台标签页 URL

Callers 1

readCurrentFolderPathMethod · 0.95

Calls 2

execAppleScriptMethod · 0.95
getCurrentWindowMethod · 0.80

Tested by

no test coverage detected