Get the path of the inactive Finder window?

Is it possible to get the path of the inactive Finder window?
Not the all of them, but the second one if I have two Finder windows opened?
I have the macro that copies the file from the one Finder window to another. And in the beginning I am getting the path of the second window:

Copy to another pane.kmmacros (6.1 KB)

which works but I would like to have more elegant solution if possible.
The Chat GPT created the Apple Script which doesn't work.

tell application "System Events"
	tell process "Finder"
		set backgroundWindows to (every window whose background only is true) -- Get all background windows
		if backgroundWindows is not {} then -- Check if any background windows exist
			repeat with theWindow in backgroundWindows -- Loop through background windows
				set thePath to POSIX path of (target of theWindow as alias) -- Get the path
				set the clipboard to thePath -- Copy the path to clipboard
			end repeat
		else
			log "No background Finder windows found."
		end if
	end tell
end tell

Is it possible at all?

If you want to do it with Keyboard Maestro Actions you can make it a lot simpler:

Get Path to WIndow 2 in Finder.kmmacros (3.3 KB)

1 Like

Yes, you are right! But is it possible to avoid switching the windows at all?

I don't think it is possible with native Keyboard Maestro Actions. Keyboard Maestro has a token for the current Finder location but it doesn't have a token for the path to the second Finder Window.

But to make the process almost seamless you can just add a second Bring Window to front Action to restore the first window almost instantly. No need for any pauses or replicating shortcut keys.

1 Like

If you want to know something about the Finder, try asking the Finder:

tell application "Finder"
	try
		return POSIX path of (get target of window 2 as alias)
	on error
		return ""
	end try
end tell

The error is there in case the window is targeting the "machine":

image

....and returning an empty string mimics the behaviour of the %FinderInsertionLocation% token.

2 Likes

Great! :slight_smile: It's perfect now!