Path to selection in second finder window

Hi,

Is it possible to get the path to the selection in the second finder window? The first script below only returns the path of the second finder window. And the second script returns the path of the selection in the first finder window. Tried to combine both as one, but so far it only returned errors.

Is it possible to combine these two scripts?

Path of second finder window

try
	return POSIX path of (target of second Finder window as alias)
	on error
	return POSIX path of (desktop as alias)
end try

Path of selection in first finder window

return expand("") 

on expand(separator)
	
tell application "Finder"
	set theSelection to get selection as list
	set theResult to ""
	if length of separator = 0 then set separator to linefeed
	repeat with theItem in theSelection
		if length of theResult > 0 then set theResult to theResult & separator
		set theResult to theResult & POSIX path of (theItem as alias)
	end repeat
	return theResult
end tell

end expand

Thanks in advance!

The selection is a property of the application, not of the window and it is a singular thing.

The finder dictionary describes it this way:

the selection in the frontmost Finder window

If you try to specify a selection in a window, you will get a -1728 error, which typically represents an entity that doesn’t exist.

If you run this command, tell application "Finder" to properties of window 2, you will see that selection is not among them.

However, if you run tell application "Finder" to properties then you will see it there.

If you only need the selection of window 2 but wish to work in window 1, a workaround would be to switch to window 2, assign the selection to a variable and then switch back to window 1.

Here is a clunky script that breaks down all the steps and throws up dialogues to show what’s going on. To minimize distractions, I only provide the appropriate window with each result. Select one file in a window, then switch to another window and select one file there; then run the script.

tell application "Finder"
	
	set w1 to window 1
	--> Finder window id 2438 of application "Finder"
	set n1 to name of w1
	--> "dy01"
	display dialog "w1: " & n1
	
	set w2 to window 2
	--> Finder window id 2431 of application "Finder"
	set n2 to name of w2
	--> "mpv-play2"
	display dialog "w2: " & n2
	
	
	set index of window n1 to 1
	display dialog "target 1: " & target of window n1 --> dy01
	set sel to selection as alias
	set salt to sel as text
	display dialog "window 1 selection: " & salt & " of window " & n1 --> dy01
	
	set index of window n2 to 1
	display dialog "target 2: " & target of window n2 --> mpv-play2
	set sel to selection as alias
	set salt to sel as text
	display dialog "window 2 selection: " & salt & " of window " & n2 --> mpv-play2
	
	set index of window n1 to 1
	display dialog "target 1: " & target of window n1 --> dy01
	
	display dialog "previous selection from window 2: " & salt --> mpv-play2
	open file salt -- open the previous selected file from window 2
end tell

Hi Mockman,

Thanks for your reply! The script returns the error below and the dialog gives the name of the frontmost window.

error “Finder got an error: Canceled by user.” number -128

I’m not quite sure if the purpose why i use this is clear. I use a text expansion which will use the path generated by this script “Path of selection in first finder window”. I also need a script which will return the path of the selection in the second finder window. But not sure if this is possible?

So i use an text expansion with 2 scripts which will return the path of the selection of the frontmost window and would like to let the second script return the path of the selection in the second finder window. (it can be done if i put the window as the selected item, but preferably i should be the selection in that window just as the first script)

Hope this explains the use a bit more?!

You have to switch windows, as in bring window 2 to the front.

The script above is really about demonstrating the issue.

Try this:

tell application "Finder"
	activate
	set n1 to name of window 1
	set n2 to name of window 2
	set index of window n2 to 1 -- bring window 2 to front
	delay 0.4 -- wait for windows to complete re-arranging; can decrease but should test;
	set sel to selection -- should be in window 2
	set salt to sel as text
	
	set xPath to (container of file salt as text) -- path of original window 2
	set index of window n1 to 1 -- bring original window 1 back to front
end tell
xPath

Mockman. I tested your script and it worked as expected but only if the target of Finder windows 1 and 2 are not the same. A different approach that appears to avoid this issue is as follows. Error correction should be added in case Finder window 2 does not exist.

tell application "Finder"
	activate
	set selectionOne to selection as text
	set index of Finder window 2 to 1
	delay 0.5 -- not required on my computer
	activate -- to test script in Script Debugger
	set selectionTwo to selection as text
	delay 0.5 -- not required on my computer
	set index of Finder window 2 to 1
end tell

tell me to activate -- to test script in Script Debugger
display dialog "Window 1 Selection: " & selectionOne & linefeed & "Window 2 Selection: " & selectionTwo

BTW, as far as I know, the direct answer to vanwoods’ question is no–it’s not possible to get the path to the selection in the second Finder window.

Ok, thanks for the reply!

I’ll then stick to the path of the name of the second finder window. Which is in my first post.

Your solution is cleaner than mine. I should note though that I think the OP is interested more in the path of the container than of the selected item.

Actually, you could probably reduce it to something like this:

tell application "Finder"
	set t1 to target of window 1 as text
	set t2 to target of window 2 as text
	{t1, t2} -- result of both as list
end tell

Mockman. Thanks for the additional perspective. I had a different understanding of what the OP wanted, although a second reading of the OP’s post left me somewhat confused as to the desired result. Anyways, the OP has a solution which is great.

Only the first of the two questions raised by the OP has been addressed, and I’ve included below a suggestion for the second question. Error correction–which is appropriate to the larger script–should be added for the circumstance where two Finder windows are not found.

set {windowOnePaths, windowTwoPath} to getPaths(linefeed)

on getPaths(separator)
	tell application "Finder"
		set windowOneSelection to selection as alias list
		set windowTwoTarget to (target of second Finder window as alias)
	end tell
	repeat with anItem in windowOneSelection
		set contents of anItem to (POSIX path of anItem)
	end repeat
	set {TID, text item delimiters} to {text item delimiters, separator}
	set windowOneSelection to windowOneSelection as text
	set text item delimiters to TID
	return {windowOneSelection, (POSIX path of windowTwoTarget)}
end getPaths

Thanks peavine!

This works regarding my second question, but unfortunately still only with the second window name as path.

Anyway, hope it can be done somewhere in the future…?!