3

In Finder, I can reopen a tab or window I just accidentally closed by going into the Go > Recent Folders menu and pick one of the recent folders from the list.

I'd like to open it with shortcut T instead, but I'm not sure how to approach this.

  • Can I bind a key combo to a menu item with a variable name? If yes, how does that work?
  • If not, are there any options to do this in a shell command or with AppleScript?

Thanks!

EDIT: I've found that BetterTouchTool allows adressing menu items with variable names/labels, but now I need to predict the position of the last closed tab in the A-Z sorted list of recent locations.

Enter the path to the menubar item you want to click, separated by >semicolons. For example: "File;New:Window" (without the quotes!) will trigger the menubar item named "Window"

You can also trigger a menu-item by it's position (e.g. if the title >varies). For example: "File; (2)" will trigger the second item in the >File menu. "(2);(2)" will trigger the second item in the File menu. >Just always put the position in parentheses.

If an item can have multiple names based on it's state (e.g. hide >and show) you can use || to toggle one of them. For example >Edit;Start Dictation|l Cancel Dictation will either start or cancel >dictation :-)

Additionally you can use a wildcard if a menu-item has variable parts

The Go > Recent Folders menu: no way to tell the position of the last closed folder due to this list being sorted by name:

macos recent folders in Finder

1 Answer 1

1

You can get those valves from "Finder defaults", it's on the first position under FXRecentFolders key.

Here's the AppleScript (based on the answer by Oliver Joseph Ash):

on findPathSeparator(theData, theFile)
    set pathSeparator to {0, 0, 0, 1, 1, 0, 0}
    set bytesFound to 0
    set bytesSearched to 0
    
    try
        read theFile from 0 for 0
        
        set numIterations to 0
        
        repeat (get eof theFile) times
            set theId to id of (read theFile from bytesSearched for 1)
            
            if theId is item (bytesFound + 1) of pathSeparator then
                set bytesFound to bytesFound + 1
            else
                set bytesFound to 0
            end if
            
            if bytesFound is (count of pathSeparator) then exit repeat
            
            set bytesSearched to bytesSearched + 1
        end repeat
    on error msg
        msg
    end try
    
    return bytesSearched - (count of pathSeparator)
end findPathSeparator

on getPathFromData(theData)
    set pathSeparator to {0, 0, 0, 1, 1, 0, 0}
    
    set theFile to (open for access POSIX file ("/tmp/get_recent_folders") with write permission)
    
    set eof theFile to 0
    
    write contents of theData to theFile
    
    set startPosition to findPathSeparator(theData, theFile)
    
    try
        read theFile from startPosition for 0
        
        set thePath to ""
        
        repeat
            set idList to id of (read theFile for 8)
            
            if (idList does not end with pathSeparator) then exit repeat
            
            set theLength to item 1 of idList
            
            set thePath to thePath & ("/" & (read theFile for theLength as «class utf8»))
            
            read theFile for (4 - theLength mod 4) mod 4
        end repeat
    on error msg
        msg
    end try
    
    close access theFile
    
    return thePath
end getPathFromData

tell application "System Events"
    tell property list file "~/Library/Preferences/com.apple.finder.plist"
        set dataItems to property list item "FXRecentFolders"'s property list items's property list item "file-bookmark"'s value
        set itemNames to property list item "FXRecentFolders"'s property list items's property list item "name"'s value
    end tell
end tell

set lastPath to getPathFromData(item 1 of dataItems)
do shell script "open " & lastPath

Just assign a global keyboard shortcut to that script.

1
  • Sorry for reviving this conversation after such a long time. I've tried integrating this script in Keyboard Maestro and it's a hit and miss. What could be the reason why it doesn't deliver the desire effect two times out of three? Here's an example: share.cleanshot.com/1iD9nL
    – Alex F.
    Commented May 15, 2022 at 11:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .