Setting a Finder window's directory history

I’m trying to make an AppleScript that reopens the last closed Finder window, including history. Here’s my script so far:

tell application "Finder"
	set newWindow to make new Finder window
	activate
	# https://old.reddit.com/r/applescript/comments/13pjv7l/reading_path_name_with_spaces/jl9xm3l/
        # I couldn't figure out how to set a Finder window's target in JXA (window.target = Path("...")
        # wouldn't work) so I'm just using AppleScript and running the JXA part from it
	set recentFolders to run script "const finderPrefs = $.NSMutableDictionary.dictionaryWithContentsOfFile(
	`${$.NSHomeDirectory().js}/Library/Preferences/com.apple.finder.plist`
);

const paths = finderPrefs.valueForKey('FXRecentFolders').js.map((item) => {
	const bookmark = item.valueForKey('file-bookmark');
	const nsurl =
		$.NSURL.URLByResolvingBookmarkDataOptionsRelativeToURLBookmarkDataIsStaleError(
			bookmark, $.NSURLBookmarkResolutionWithoutUI, $(), $(), $(),
		);
	return nsurl.path.js;
});
// reverse the paths so that they are oldest to newest
paths.reverse();" in "JavaScript"
	# Go to each recent directory so that the directories are in the window's history
	repeat with recentFolder in recentFolders
		set target of newWindow to (recentFolder as POSIX file)
	end repeat
end tell

This accomplishes what I want, but it’s slow enough where the window shows the directories changing. Instead of traversing the directories, is there any better way to set a Finder window’s history, such as a property? Or does anyone have a better way to do this overall?

I believe you can only manipulate a window when it exists, that is, it must be visible.

Hi nibble. Welcome to MacScripter.

You can speed up the method you’re using a tiny amount by creating the window with the first folder (instead of creating it and then changing the target) and by making the window as small as possible while its contents are being redrawn. Not quite what you want, but it may lessen the annoyance. :slightly_smiling_face:

use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use scripting additions

# https://old.reddit.com/r/applescript/comments/13pjv7l/reading_path_name_with_spaces/jl9xm3l/
# I couldn't figure out how to set a Finder window's target in JXA (window.target = Path("...")
# wouldn't work) so I'm just using AppleScript and running the JXA part from it

set finderPrefs to current application's class "NSUserDefaults"'s alloc()'s initWithSuiteName:("com.apple.Finder")
set recentFolders to ((finderPrefs's objectForKey:("FXRecentFolders")) as list)'s reverse

repeat with i from 1 to (count recentFolders)
	set this to recentFolders's item i
	set thisFolder to (current application's class "NSURL"'s URLByResolvingBookmarkData:(this's |file-bookmark|) options:(current application's NSURLBookmarkResolutionWithoutUI) relativeToURL:(missing value) bookmarkDataIsStale:false |error|:(missing value)) as «class furl»
	tell application "Finder"
		if (i = 1) then
			activate
			set newWindow to (make new Finder window to thisFolder)
			set {x, y, w, h} to newWindow's bounds
			set newWindow's bounds to {x, y, 1791, 305} -- Smallest window size on my iMac.
		else
			set newWindow's target to thisFolder
		end if
	end tell
end repeat
tell application "Finder" to set newWindow's bounds to {x, y, w, h}
1 Like