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

Method readCurrentBrowserUrlMac

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

* macOS: 通过 AppleScript 获取当前浏览器前台标签页 URL * 参考 uTools 行为: * - Safari 读取 front document * - 其他受支持浏览器读取 front window 的 active tab

()

Source from the content-addressed store, hash-verified

227 * - 其他受支持浏览器读取 front window 的 active tab
228 */
229 private async readCurrentBrowserUrlMac(): Promise<string> {
230 const windowInfo = this.clipboardManager?.getCurrentWindow()
231 if (!windowInfo) {
232 console.warn('[PluginShell] readCurrentBrowserUrl: 未识别到当前活动窗口')
233 throw new Error('未识别到当前活动窗口')
234 }
235
236 const bundleId = windowInfo.bundleId
237 if (!bundleId || !(bundleId in MAC_BROWSER_APP_MAP)) {
238 console.log(
239 `[PluginShell] readCurrentBrowserUrl: 当前窗口非受支持浏览器 (bundleId=${bundleId})`
240 )
241 throw new Error('当前活动窗口非可识别浏览器')
242 }
243
244 const appName = MAC_BROWSER_APP_MAP[bundleId as keyof typeof MAC_BROWSER_APP_MAP]
245 const script =
246 bundleId === 'com.apple.Safari'
247 ? 'tell application "Safari" to return URL of front document'
248 : `tell application "${appName}" to return URL of active tab of front window`
249
250 try {
251 const result = (await this.execAppleScript(script)).trim()
252 if (!result) {
253 console.error('[PluginShell] readCurrentBrowserUrl: AppleScript 返回空 URL')
254 throw new Error('未读取到 URL')
255 }
256 console.log(
257 `[PluginShell] readCurrentBrowserUrl: macOS 浏览器 URL 读取成功 (bundleId=${bundleId})`
258 )
259 return result
260 } catch (error: unknown) {
261 const errMsg = error instanceof Error ? error.message : String(error)
262 const cleanMsg = errMsg
263 .replace(/^\d+:\d+:\s*execution error:\s*/i, '')
264 .replace(/\(-?\d+\)\s*$/i, '')
265 .trim()
266 console.error('[PluginShell] readCurrentBrowserUrl: AppleScript 执行失败:', cleanMsg)
267 throw new Error(cleanMsg || '未读取到 URL')
268 }
269 }
270
271 /**
272 * Windows: 检查当前窗口是否为文件资源管理器,并获取路径

Callers 1

readCurrentBrowserUrlMethod · 0.95

Calls 2

execAppleScriptMethod · 0.95
getCurrentWindowMethod · 0.80

Tested by

no test coverage detected