apple script 激活指定的vscode的窗口,以‘notes’开头的窗口

tell application "System Events"
    try
        -- 优先通过 bundle identifier 获取 VSCode 进程,必要时回退到名称匹配
        set vscodeProcesses to {}
        try
            set vscodeProcesses to every application process whose bundle identifier is "com.microsoft.VSCode"
        end try
        if (count of vscodeProcesses) is 0 then
            set vscodeProcesses to every application process whose name contains "Code"
        end if
        if (count of vscodeProcesses) is 0 then return "❌ 未找到正在运行的 VSCode"
        
        -- 统计被激活的 notes 窗口数量
        set notesWindowCount to 0
        
        repeat with proc in vscodeProcesses
            try
                set windowList to windows of proc
                repeat with w in windowList
                    set winTitle to (name of w) as string
                    ignoring case
                        if winTitle starts with "notes" then
                            set notesWindowCount to notesWindowCount + 1
                            -- 若窗口被最小化则先还原
                            try
                                if (value of attribute "AXMinimized" of w) is true then
                                    set value of attribute "AXMinimized" of w to false
                                    delay 0.1
                                end if
                            end try
                            -- 先让进程到最前,再抬升具体窗口
                            set frontmost of proc to true
                            delay 0.05
                            try
                                perform action "AXRaise" of w
                            end try
                        end if
                    end ignoring
                end repeat
            end try
        end repeat
        
        if notesWindowCount is 0 then
            return "❌ 未找到标题包含 'notes' 的 VSCode 窗口"
        else
            return "✅ 已激活 " & notesWindowCount & " 个标题包含 'notes' 的 VSCode 窗口"
        end if
        
    on error errMsg
        return "❌ 脚本执行出错: " & errMsg
    end try
end tell

 

posted @ 2026-02-04 19:11  meetrice  阅读(1)  评论(0)    收藏  举报