* AppleScript 辅助工具类 * 提供常用的 macOS 系统交互功能
| 8 | * 提供常用的 macOS 系统交互功能 |
| 9 | */ |
| 10 | class AppleScriptHelper { |
| 11 | /** |
| 12 | * 执行 AppleScript 脚本 |
| 13 | * @param script AppleScript 脚本内容 |
| 14 | * @returns 脚本执行结果 |
| 15 | */ |
| 16 | private async execute(script: string): Promise<string> { |
| 17 | try { |
| 18 | // 转义单引号以防止脚本注入 |
| 19 | const escapedScript = script.replace(/'/g, "'\\''") |
| 20 | const { stdout } = await execAsync(`osascript -e '${escapedScript}'`) |
| 21 | return stdout.trim() |
| 22 | } catch (error) { |
| 23 | console.error('[AppleScript] 执行 AppleScript 失败:', error) |
| 24 | throw error |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * 获取访达(Finder)当前打开的路径 |
| 30 | * @returns 访达当前路径,如果访达未激活或没有打开窗口则返回 null |
| 31 | */ |
| 32 | async getFinderPath(): Promise<string | null> { |
| 33 | try { |
| 34 | const script = ` |
| 35 | tell application "System Events" |
| 36 | set frontApp to name of first application process whose frontmost is true |
| 37 | end tell |
| 38 | |
| 39 | if frontApp is "Finder" then |
| 40 | tell application "Finder" |
| 41 | if (count of windows) > 0 then |
| 42 | return POSIX path of (target of front window as alias) |
| 43 | else |
| 44 | return "" |
| 45 | end if |
| 46 | end tell |
| 47 | else |
| 48 | return "" |
| 49 | end if |
| 50 | ` |
| 51 | const result = await this.execute(script) |
| 52 | return result || null |
| 53 | } catch (error) { |
| 54 | console.error('[AppleScript] 获取访达路径失败:', error) |
| 55 | return null |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * 获取当前激活的应用程序信息 |
| 61 | * @returns 当前激活应用的信息对象 |
| 62 | */ |
| 63 | async getFrontmostApp(): Promise<{ |
| 64 | name: string |
| 65 | bundleId: string |
| 66 | path: string |
| 67 | } | null> { |
nothing calls this directly
no outgoing calls
no test coverage detected