WINDOWCOUNT() but just for the current space?

Hi, @alexcr. This can be done with AppleScript. The macro below includes two options, one that counts all Finder windows in the Mission Control Desktop Space and one that counts all except those that are minimized.

Download: Count Finder Windows in Desktop Space.kmmacros (4.7 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 13.4.1 (22F82)
  • Keyboard Maestro v10.2

2 Likes

I would have guesses that is what it does now.

But in any event, whatever it does now is all it can do as Apple does not provide useful APIs for dealing with Spaces.

1 Like

Thanks for the quick responses, @_jims and @peternlewis!

1 Like

Correct me if I'm mistaken, but shouldn't WINDOWCOUNT() return the count of windows for the active application in the current Mission Control Desktop Space?

That's what if seems to do for most applications, but for the Finder it seems to return window_count+1.

The AppleScript I shared above counts as expected and has two other features:

  • The Finder does not need to be activated.
  • Minimized Finder windows can be included or excluded.

That is what I would expect, yes.

I believe for the Finder, the Desktop (background) counts as a window.

1 Like

I can confirm this, it sometimes causes me headaches when I’m working with AppleScript.

1 Like

Hi, @cdthomer. Do you remember the details? With the AppleScript I shared above, this is not an issue.

Hey Jim, I’m not talking about any particular script, just in general, working with AppleScript and Finder (or desktop) sometimes throws me for a loop.

1 Like

I didn't think you were, Chris; I was just wondering if you remembered the AppleScript that did cause the issue. If not, no problem.

1 Like

@_jims and @peternlewis, from what you guys are saying, it sounds like, if you're looking to count Finder windows in a specific space and not other spaces, WINDOWCOUNT()-1 would output the same result as the script that @_jims shared? Or am I wrong? (Noting, of course, that @_jims script provides some additional, useful flexibility.)

That sounds correct to me.

Upon further testing, results are inconsistent. Consider this macro:

Download: Count Finder Windows in Desktop Space.kmmacros (5.4 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 13.4.1 (22F82)
  • Keyboard Maestro v10.2


Then after each of the following steps, run the above macro. I've pasted my observations–see the bold text.

  1. Open a Finder window in Desktop Space 1. [2:1:1]

  2. Move to Desktop Space 2. Open a new Finder window. [2:1:1]

  3. Open a new Finder tab in the same window. After this step, run the macro several times checking with tab 1 selected before some of the runs, tab 2 selected before other of the runs. During my testing, the result was sometimes [3:1:1] and other times [2:1:1]. I could not determine the cause of the inconsistency.

  4. Minimize the Finder window. [3:2:0]


Thus if you want the count of Finder windows in the active space, I suggest you use:

tell application "System Events"
	-- Within "System Events" restricts count to the active Desktop Space
	tell process "Finder"
		return count of (windows whose value of attribute "AXMinimized" is false)
	end tell
end tell
1 Like

Thanks, @_jims! Just curious so that I understand how this works, what does the additional language you added ("whose value of attribute "AXMinimized" is false") do?

1 Like

Finder windows minimized (including those with multiple tabs) will not be included in the count.

Got it. Thanks, @_jims.

One other question related to spaces. How would you recommend creating an action that sets a variable to the current space number?

So, for example, if you're currently in the space "Desktop 3", it would set the variable to "3" (or I suppose "Desktop 3" would be fine, too).

Hi, @alexcr. There is a free application named WhichSpace which displays the current Desktop Space in the Mac menubar. It can also provide that number via AppleScript:

try
	tell application "System Events"
		tell process "WhichSpace"
			set temp to (title of menu bar items of menu bar 1)
		end tell
	end tell
	return item 1 of temp
on error
	return ""
end try

The following related information might be more than you need...

I use WhichSpace in a set of macros that I have shared: Desktop Spaces • Macros to Improve Navigation and Window Management. If you want to dig into the details, I suggest you check out two macros in that set:

  • sub—Get DesktopNo
  • Go to Previous Desktop
1 Like

Thanks, @_jims! Good to know about WhichSpace and the extensive set of macros you created, both of which look very useful.

In the script above, is "temp" the variable name? And what's the meaning and purpose of the final line, "return item 1 of temp"?

temp is a list that contains 1 item.

Here's the AppleScript code in Script Debugger, a highly recommended application that is far superior to Apple's Script Editor.


I should note that I used simplier AppleScript originally, but @seishonagon encountered a situation where WhichSpace inexplicably returned a three-item list. I’ve never been able to reproduce this issue, but the revised script that @seishonagon shared works well in both cases thus I changed the aforementioned macro set to use this revision.

1 Like

Ah, got it. In the updated script you guys arrived at, what's the actual variable name you would then be able to reference within KM? I'm not familiar with working with variable names within lists.

The list is reduced to a single value in the AppleScript. All you need to do is to retrieve that value by following these steps:

  1. Add the Execute an AppleScript action to a macro.

  2. Copy and paste the AppleScript above into that action.

  3. Change ignore results to save value to variable. A Save to variable field will appear.

  4. In the Save to variable field, enter your desired variable name, e.g., local_DesktopSpaceNo.

1 Like