In macOS 10.14 (Mojave), can Finder remember previously opened tabs like Safari pin tabs?

First of all, I am not asking to re-open Finder tabs on OS reboot, we all know it works.

I am asking for a solution when I accidentally clicked on the red-cross (aka close window button) on Finder window, it cause Finder to lose all currently opened tabs.

I know this is the expected behavior, but can I change it somehow? I have come to rely on it in macOS 10.12-10.13 (Sierra and High Sierra), when Finder window always remember previously opened tabs even if you close it yourself.

Some say the behavior in 10.12/10.13 is a bug, other argues it was working in Mavericks 10.9, but was broken by Apple in Yosemite 10.10, and later fixed then re-break. Both could be true. But is there a solution to get this behavior back?

The best I could find is a AppleScript that open tabs, but it doesn't really restore tabs as it simply doesn't have the information to restore a previously closed window.

Is there a solution out there I am not aware of?


Solution 1:

Me too, also annoyed by this new behavior. Not really an answer, but here is a snippet of the AppleScript I'm using to save the opened tabs to an array fot

tell application "Finder"
    set windowCount to number of windows
end tell

set fot to {}

tell application "Finder"
    repeat with x from 1 to windowCount
        set tn to (target of window x as alias)
        copy tn to the end of the fot
    end repeat
end tell

The array can then dumped to a temporary file e.g. foo, where foo follows the Applescript format of posix file paths using colons, e.g. set foo to "full:path:to:file", then

try
    set dataStream to open for access file foo with write permission
    set eof of dataStream to 0
    write fot to dataStream starting at eof as list
    close access dataStream
on error
    try
        close access file foo
    end try
end try

You can then read the file (e.g. foo) at a later time to restore the tabs,

set foo to "full:path:to:file"
set readdat to (read file fphds as list)

repeat with i from 1 to length of readdat
    set tb to item i of readdat
    tell application "Finder"
        activate
        open tb
    end tell
end repeat

The order of the opened tabs might be changed (I think the order is current tab followed by tabs sorted by their creation date), and to my knowledge, unlike safari, finder does not have the tab properties accessible to AppleScript (to AppleScript, tabs and windows of finder are indistinguishable). I'm using the snippet with hammerspoon (a lua scripting tool) to manually save/restore using keyboard shortcuts. Hammerspoon also has api to detect window losing focus(deactivate) so this may be potentially automated.

If speed is not a concern, you may try doing the whole thing in hammerspoon (no need for temp file, keeping tab orders but can take a second or two. primitive code, may break in some edge cases):

get_finder_ctab = [[tell application "Finder"
    set tn to (target of the front window as alias)
end tell

get POSIX path of tn
]]

tab_save = {}

function h_save_finderTab()
    fapp = hs.application.get'Finder'
    hs.application.launchOrFocus("Finder")
    num_ftab = fapp:focusedWindow():tabCount()
    tab_save = {}
    for i=1, num_ftab do
        fapp:focusedWindow():focusTab(i)
        runokay,tab_save[i],_ = hs.osascript.applescript(get_finder_ctab)
    end
end

function h_restore_finderTab()
    for _,t in ipairs(tab_save) do
        hs.open(t)
    end
end

Alternative

If it's a one-time thing like clicking the red-cross by accident, you may also try hold down the option key then right click finder icon in dock, then choose "Relaunch", which can trigger Finder to read the saved state (I find sometimes it may be not up to date and may restore an old tab list)