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.
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.
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.
@_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.)
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.
Open a Finder window in Desktop Space 1. [2:1:1]
Move to Desktop Space 2. Open a new Finder window. [2:1:1]
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.
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
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?
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 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.
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.