Finder: testing if there are open windows in a space

I would like to set up a macro for Finder that when you switch to Finder;

  • if there are no finder windows open in the current space...
  • opens a new Finder window in the current space.

This is supposed to be the behaviour of Finder anyway when you have system settings set to not switch to another space when an application is activated, but it isn't reliable, or predictably repeatable.

I can use Finder Activates trigger to open the window to the desired directory easily enough, but the testing to only do it if there are no other windows in the space is the bit I don't know.

I saw this applescript:

is there a modification of this... something along the lines of:

if (exists of window 0) then
      make new Finder window

thanks.

1 Like

I just tried this approach and it works for me (under MacOS Sonoma) but then again so does the default behaviour, so please let me know how you get on…

Here is one way to check if there are any Finder windows open and, if not, open one.

image

In direct terms, this is looking for the Cycle Through Windows menu command that would only be available if a window is already open, even if it is not in the foreground. If the specified menu command is enabled, nothing happens. Otherwise, File > New Finder Window is selected.

Hi, @metaning.

Oddly, automating the Finder is tricky. More challenging is the automation of Mission Control Desktop Spaces. When you put the two together, it gets really ugly!


I've found three non-ideal approaches with AppleScript to count the number of Finder windows in the current Desktop Space:

Option 1

( expand / collapse )
(*
This AppleScript will return the number of Finder windows visible and minimized from the current space with two caveats: 

1. If the Finder is hidden, then windows from other spaces will be counted.

2. If Finder windows from other spaces are minimized, those windows will also be counted.
*)

tell application "System Events"
	-- Since Monterey, one is able to Quit the Finder
	if (name of processes) does not contain "Finder" then
		return 0
	else
		tell application process "Finder"
			return count of windows
		end tell
	end if
end tell

Option 2

( expand / collapse )
(*
This AppleScript will return the number of Finder windows visible and minimized from the current space with two caveats: 

1. If the Finder is hidden, then windows from other spaces will be counted. To avoid these false this script returns 0 if the Finder is hidden.

2. If Finder windows from other spaces are minimized, those windows will also be counted.
*)

tell application "System Events"
	-- Since Monterey, one is able to Quit the Finder
	if (name of processes) does not contain "Finder" then
		return 0
	else if get visible of process "Finder" is false then
		return 0
	else
		tell application process "Finder"
			return count of windows
		end tell
	end if
end tell

Option 3

( expand / collapse )
(*
This AppleScript will return the number of Finder windows visible and minimized from the current space with two caveats: 

1. If the Finder is hidden, then windows from other spaces will be counted. To avoid these false this script returns 0 if the Finder is hidden.

2. If Finder windows from other spaces are minimized, those windows will also be counted. To avoid these false positives, this script unminimizes all Finder windows in all spaces.
*)

tell application "System Events"
	-- Since Monterey, one is able to Quit the Finder
	if (name of processes) does not contain "Finder" then
		return 0
	else if get visible of process "Finder" is false then
		return 0
	else
		tell application "System Events"
			tell process "Finder"
				repeat with w in windows
					if value of attribute "AXMinimized" of w is true then
						set value of attribute "AXMinimized" of w to false
					end if
				end repeat
			end tell
		end tell
		delay 1
		tell application process "Finder"
			return count of windows
		end tell
	end if
end tell


If you want to test for yourself, here's a macro with the three options:

Download: Counting Finder Windows in the Current Space.kmmacros (12 KB)

Macro-Image


Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 15.3.1 (24D70)
  • Keyboard Maestro v11.0.3

Several months ago I searched for a cleaner solution, but came up empty. Maybe someone in the forum can offer up a better approach.


You might find these two related threads interesting:

Thanks for all the suggestions folks, I'll look through them and see what works. What happens in Ventura is:

  • You have Mission Control set to NOT switch to a space with open windows when activating an application.
  • You click the Finder icon on the dock, Finder activates, but fails to spawn a file browser.
  • You click Finder again on the dock, and you get switched to another space where there is a Finder window open (because Finder doesn't respect the "don't switch spaces" setting).
  • To try to fix the lack of Finder window spawning, you go into system preferences, and toggle the appropriate switch back and forth, and it starts working again, for a while. Hence why I'm trying to build a replacement in KM.

I notice the use of selecting menus in Finder to spawn the new window. I don't appear to have that option in my version of KM (11) - I have "Select a menu by name" or "select or show a menu item".

Is there a reason to do this rather than what I was doing, using the "open" action targeting a directory, with Finder as the chosen application?

@kevinb Your suggestion worked the first time I used it, but afterwards didn't. What is FinderInsertionLocation doing? I tried it with an ANY conditional, for it being both empty and not empty, and then it worked all the time, but then it also triggered when there were other Finder windows open in the space I had active.

@NaOH The Cycle Through Windows menu exists across all spaces, so it's going to be returning true when windows are open in a different space.

@_jims How do I implement your solutions? Taking Option 1 as an example, I'm working within a framework of:

This Application;

  • Finder
  • Activates

Some sort of IF conditional: (what to do here?)

execute the following actions:

  • Open "~"
  • with: Finder

I'm running an older OS. Thanks for that.

For the approach I gave above to work, any menu condition that changes between a space with open windows compared to a space without open windows will work. So maybe it's checking if File > Close Window is enabled, or some other Finder menu item.

OK, so in a space where there are no Finder windows open, File > Close Window is greyed out.

So a solution might be testing if that menu is available, and if not, open a new window?

Is there an easy "test for availability of menu" item in KM?

edit: Actually, that might not work, because it's not available, unless the window itself is selected.

Hmm.. just noticing "select or show a menu" is showing me different options now, and matching the previous examples.

Until I saw @_jims 's reply, I did not pick up on the importance of Spaces in all this (I never use it) so my reply was too simplistic... Also it was broken, as I explain below. Sorry!

The Wiki says: "The %FinderInsertionLocation% token returns insertion location in the Finder, which is usually the path of the front window".

So my thinking was that when there is no front window, the token should be empty. Testing today, I see that that is not correct. I must have been confused by something when I tested this approach before!

Today, a test macro shows that when there is no open window in the Finder, %FinderInsertionLocation% gives the path to the Desktop. Therefore the macro should have tested for that path, not an empty string.

In any case, since I did not even think to address the complexities of Spaces, I doubt that that will work either! I suppose you could try it but I am not optimistic. :wink:

1 Like

Sorry, I was focused on your post title.

I suspect you are asking about the above, right?

Assuming, so...

I suggest you download the macros here: Finder Assistant.

Even if you don't use the main macro, Finder Assistant, check out the subroutine macro named 𝗌.𝗳𝗻𝗱⇾NewTabOrWindowInSpace.

For that sub, create a simple calling macro like the following...


Download: z𝘊𝘢𝘭𝘭 𝗌.𝗳𝗻𝗱⇾NewTabOrWindowInSpace.kmmacros (1.6 KB)

Macro-Image


Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 15.3.1 (24D70)
  • Keyboard Maestro v11.0.3

1 Like

Re-reading the original post, this almost makes it sound as if a specific window is what's wanted when switching to a Space. Is that the case?

No, it's just replicating the behaviour of how it's supposed to work - switching to Finder when there are no Finder windows open in that space is supposed to spawn a window opening to the location Finder defaults towards when you manually spawn a new window.

But it's not when switching to a space, it's when switching to Finder within the space I'm currently working.

The "edit" here is (obviously) correct. But multiple uses of If Then Else appears to work around this.

image

My Mac and OS are older, so testing on a faster machine may show it's necessary to add a short Pause between the first If Then Else and the second one.

Correction to my last post which did not account for if there is already a window open and front most.

image

Sorry, I've been otherwise occupied with things, but OMG thanks - that solved it. I'll really have to keep that in mind in future - testing for disabled menu options as a strategy.

There's a really interesting quirk for the second conditional - it doesn't work if there's:

  • already a Finder window open in the space,
  • that finder window didn't have focus when Finder was last used
  • Instead of using a hardcoded keyboard shortcut, you use the "select a menu by name" feature to select: Window > Cycle Through Windows

Interestingly enough, the final section to spawn the new window works fine with doing that for File > New Finder Window.

Is there any difference in speed or performance in using a keystroke directly, rather than the names of the menus?

(I did have to restate "Minimize" as "Minimise" :sweat_smile:)

Cheers.

1 Like

I appreciate that solution, but it was probably a little outside my confidence level, in terms of loading in someone else's script library. But thanks all the same :slight_smile:

1 Like

I would guess selecting the menu item is faster—it didn't occur to me to use that when I posted that last macro—but I also don't know if the difference is perceptible.

I'd be curious to find out what it is that means choosing Cycle Through Windows doesn't work when chosen by name, but does work on a shortcut.

Does anyone have an insight on why that would be different?

Can you post an example macro? Because this action:

image

...works fine for me.

Demo: Window Cycle demo.kmmacros (3.4 KB)

This version:

Spawns a new copy of the finder window every time you switch to finder if it is already open (but not in focus), stacking them on top of each other.

This version:

Switches focus to the window.

The only difference my newb eyes can see, is choosing the menu by name, vs firing off the keystroke.

In the second step of both macros, when Cycle Through Windows is selected, by following it with the keystroke Command-`, the result is that Cycle Through Windows is being selected twice, once by the menu item and once by the built-in keyboard shortcut. If one window is open, but not selected, then executing that menu command twice will select it then un-select it.