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-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: