1

The problem, outside of AppleScript:

To witness the problem1, do the following:

  • Double-click on a folder that exists on the Desktop. Doing so will open this folder in Finder.app.

  • Return to the Desktop and double-click on that same folder again.

  • Instead of a second window being created, the previously created window will be brought to the foreground.


A workaround, outside of AppleScript:

So, what do you do if you want two Finder windows of the same folder?

A kluge exists:

Create a new Finder window by opening any other folder. From within this new window, now if you navigate to the desired folder, then you will successfully change this window to the desired folder, and have two windows of this folder.

To navigate to the desired folder within a new Finder window, any method will work:

  • Click on the folder, if it is bookmarked in the sidebar (if enabled, located at the left of the window).

  • Click on the folder, if it happens to exist in the path bar (if enabled, located at the bottom of the window).

  • Use the window's built-in search bar.

  • Or, simply navigate to the folder manually.


The problem, in AppleScript:

The following AppleScript code will open a folder in a new Finder window:

set targetFolder to POSIX file "/Users/Me/Desktop/MyFolder"

tell application "Finder"
    open targetFolder
    activate
end tell

In the exact same way that Finder behaves non-programatically (as defined above), if you run this code for a second time (without closing the previously created Finder window), then the previously created window will be brought to the foreground.


The desired AppleScript solution:

Here is the desired behavior:

  • If targetFolder already exists in any open Finder window (including any minimized Finder window), I would like the AppleScript to create a new window for the same targetFolder (i.e., a duplicate window).

Ideally, I would like the duplicate window to be created, without having to resort to the "trick" that I delineated above (i.e., opening an arbitrary other folder first, before opening the desired folder). However, I will understand if this is not possible (i.e., if the programmatic method must replicate the manual method).


1. I understand that this behavior is in no way a bug or a "real" problem. Apple clearly programmed this action deliberately; they probably determined that most people do not need or want redundant Finder windows.


OS X El Capitan, version 10.11.6.


3

I think this code does what you want.

Code:

set targetFolder to POSIX file "/Users/Me/Desktop/MyFolder"

tell application "Finder"
    set finderFolder to item targetFolder
    set lstTargets to target of every Finder window
    if {finderFolder} is in lstTargets then
        set target of (make new Finder window) to targetFolder
    end if
end tell
  • This solution does exactly what I desire. It is my ideal solution; the script opens the folder without first having to open another folder. It also preserves my preferred Finder "View as" method (which happens to be List view). Finally, I've realized that an if statement is not even required to catch for the case of the folder already being open, because your set target of method functions properly, regardless. Thank you! – rubik's sphere Aug 31 '17 at 8:32
0

This solution gets you where you wanna go, but it does use the workaround. I tried to set the "target" property as I created the window, but it had no effect. I left that part in the script but commented out for your reference.

set targetFolder to ((path to desktop) as text) & "testFolder:"

tell application "Finder"
    set aWin to make new Finder window --with properties {target:targetFolder}
    set aWin's target to targetFolder
end tell

I also tried it by calling on the command line with do shell script, but it had the same effect as you listed in "The Problem" above.

0

Use the reveal command to avoid the behavior of the open command:

set targetFolder to POSIX file "/Users/Me/Desktop/MyFolder"
tell application "Finder"
    reveal targetFolder
    activate
end tell

Caveat: it always open the window in column view

You must log in to answer this question.

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