Search code examples
macosapplescriptfindermacos-big-surapple-silicon

How to reopen all files listed in macOS "recently closed" or "open recent" menu?


Rarely but occasionally, such as after a kernel panic, a macOS application doesn't automatically reopen with previously-open files/windows. In those cases, however, the "Recent" or "Open Recent" menu item almost always has those files or windows listed... but opening all of them can be a chore, clicking one, which then closes the menu, reopening the menu, clicking the next, etc.

Is there any way to more-efficiently open all of the files in that list?

I have tried searching to see if there is any way to select multiple menu items at once and have tried searching for an AppleScript solution, both with no luck.


Solution

  • The following AppleScript solution works in Finder, Preview, and TextEdit. (Change the script's second line as needed.)

    It likely works in other applications as well.

    --------------------------------------------------------
    # Auth: Christopher Stone
    # dCre: 2021/10/30 19:17
    # dMod: 2021/10/30 19:17 
    # Appl: Preview, System Events
    # Task: Open All Recent Items Listed in the Recent Items Menu.
    # Libs: None
    # Osax: None
    # Tags: @Applescript, @Script, @Preview, @System_Events, @Open, @Recent, @Items
    # Test: Tested only on macOS 10.14.6.
    --------------------------------------------------------
    # User note: This AppleScript works in macOS 12.0.1.
    # I have had success using it with Preview, Finder, and TextEdit.
    --------------------------------------------------------
    
    tell application "System Events"
        tell application process "Preview"
            tell menu bar 1
                tell menu "File"
                    tell menu item "Open Recent"
                        tell menu 1
                            set recentMenuItems to UI elements whose title is not "Clear Menu" and title is not ""
                            
                            repeat with menuItem in recentMenuItems
                                tell menuItem
                                    perform action "AXPress"
                                end tell
                            end repeat
                            
                        end tell
                    end tell
                end tell
            end tell
        end tell
    end tell
    
    --------------------------------------------------------