Hi!
How to check how many Finder's windows are opened at the moment? I want to make a macro which needs at least two Finder windows opened and if it's only one, to create a new window.
Hi!
How to check how many Finder's windows are opened at the moment? I want to make a macro which needs at least two Finder windows opened and if it's only one, to create a new window.
This will give you all windows and tabs counted.
Put either of these in an applescript action
tell application "Finder"
set windowCount to count of Finder windows
display dialog "You have " & windowCount & " Finder window(s) open."
end tell
This will tell you how many individual windows only:
tell application "System Events"
set finderProcess to first process whose name is "Finder"
set windowCount to (count of windows of finderProcess)
end tell
display dialog "You have " & windowCount & " individual Finder window(s) open."
Wow, that's nice timing—I was writing something that also counts Finder windows, but hadn't considered tabs (as I never use them in Finder). My macro was using the first script, and sure enough, it was wrong when I opened a bunch of tabs. The second version works perfectly, so thanks for the unexpected help!
-rob.
Of course, if the Finder's the frontmost application you can just use WINDOWCOUNT() - 1
in your KM macro -- remembering to minus 1 for the Desktop (which I always forget).
Note that all the methods so far include minimised windows...
I should have been clearer: I was getting the paths to the open Finder windows, so counting was part of that:
tell application "Finder"
set windowPaths to ""
set windowCount to count of Finder windows
repeat with i from 1 to windowCount
try
set windowPath to POSIX path of (get target of window i as alias)
set windowPaths to windowPaths & windowPath & linefeed
on error
-- Ignore windows that don't have a valid path
end try
end repeat
return windowPaths
end tell
About 60% me, 40% LLM :).
-rob.
No need to count, just work through the list. This will ignore minimised windows, remove the whose
clause to include them:
set thePaths to ""
tell application "Finder"
repeat with eachWindow in (get every window whose visible is true)
try
set thePaths to thePaths & POSIX path of (target of eachWindow as alias) & linefeed
on error
-- do nothing
end try
end repeat
end tell
return thePaths
Will it also ignore tabs, as that's important?
-rob.
No, the Finder has never been updated to recognise tabs. They're just another window. I think you'll have to hive that off to System Events, which does recognise tab groups
.
How to relate a window in Finder
to a window in System Events
is beyond me, though. So you can count the number of "proper" windows with @troy's script (although, weirdly, that returns an extra window if you minimise one) but will have problems getting their targets. Or you can get all windows and tabs with their targets with your script.
Where are the ObjC people when you need them?
I'm trying to have it not recognize tabs, just the active tab within each window. And I think I have that part working—it's ignoring tabs. But I need it to ignore minimized windows, too. And after talking with Claude (ChatGTP was useless), this modified script does exactly that, and still returns the paths for each actual window:
tell application "Finder"
set windowPaths to ""
set finderProcess to first process whose name is "Finder"
set windowCount to (count of windows of finderProcess)
repeat with i from 1 to windowCount
try
set currentWindow to window i
if not (collapsed of currentWindow) then
set windowPath to POSIX path of (get target of currentWindow as alias)
set windowPaths to windowPaths & windowPath & linefeed
end if
on error
-- Ignore windows that don't have a valid path
end try
end repeat
return windowPaths
end tell
-rob.
Yes, I understand. What I mean is that the Finder's AS dictionary doesn't even have the concept of tabs -- they're just another window. Open a single window in the Finder and tell app "Finder"
sees one window, open a second tab in that window and it sees two.
That bit's interesting. process
is from the Finder's Legacy Suite, and appears act as a sort of redirect, allowing you to use System Events to get process properties without using a tell application "System Events"
block.
Compare the outputs of:
tell application "Finder"
set finderProcess to first process whose name is "Finder"
properties of finderProcess
end tell
...and:
tell application "System Events"
properties of application process "Finder"
end tell
...to see what I mean.
So you're getting the count of Finder windows that System Events sees, then going through them by index in the Finder.
What I don't yet get is how the indexing of Finder tabs works. It looks like it's the z-order of windows and active tabs, and then all the inactive tabs are "behind" those. That would explain why your script works -- windowCount
is the number of windows System Events sees, the same as (and in the same order as) the Finder's windows/active tabs, so isn't large enough that you access inactive tabs.
Very interesting. Thanks, @griffman!
Sidenote: I thought collapsed
was a hold-over from OS9's "window shade" feature -- I hadn't realised you could use it for "minimised". Another one for the scrapbook!