Display the Name and Bounds of Every Finder Window. Macro

Display the Name and Bounds of Every Finder Window. Macro (v10.2)

Content of this page ( updated 20230127 15:59 UTC):

  • 1_. For ONE Finder window
  • 2a. For ALL Finder windows - (KM with AppleScript)
  • 2b. For ALL Finder (and other apps') windows - (KM without AppleScript)

AppleScript and KM measure window bounds differently.

KM uses Width and Height whereas AS uses Distance from the Left and Distance from the top.

Usage: Anytime you need to position 1 or more Finder windows on your screen, first set the relevant windows where you want them - then run the macro and you'll get the AppleScript bounds.

Should you need the window position for a KM macro where you use f ex "Move and Resize Window" you could use this macro:

1. For ONE Finder window

2a. For ALL Finder windows - (KM with AppleScript)

The Macro - thanks to Chris S @ccstone

Get Bounds of all Finder Windows_AS vs.1 Macro (v10.2)

Get Bounds of all Finder Windows_AS vs.1.kmmacros (10 KB)

2b. For ALL Finder (and other apps') windows - (KM without AppleScript)

use the Window Discovery Tool at Window Information Tools - Macro Library - Keyboard Maestro Discourse

1 Like

Hey there, when posting macros in this category, itโ€™s helpful to provide information about what they do, and how they work. Also, in this case, since the macro was essentially written by Chris Stone, itโ€™s good etiquette to include attribution. :wink:

Feel free to update your post with some more details.

-Chris

1 Like

Thank you for clarifying.

Where do I write what the macro is doing, in a comment field?

include attribution

Right! Before the script starts there is a (not clear) attribution to --Chris.

Maybe you can link to a good example for doing this correctly, this is the 1st time I uploaded a macro and there are a bunch more to comeโ€ฆ :slight_smile:

/

with best regards, Omar KN, Stockholm, Sweden

1 Like

Always glad to help!

While itโ€™s good practice to include it in the macro itself, (I and others use comments at the top of the macro), in this case I was referring to your post. Just add some text at the top (before the macro download link and screenshot) that has this info. :wink:

Here is a great macro that the same Chris Stone (@ccstone) wrote to simplify uploading your macros to the forum. I have been using it for well over a year and it has been very helpful. โ†“

KMF: Automating Sharing Macros or Actions to the Forum - Macro Library - Keyboard Maestro Discourse

-Chris from Ohio

Hey Omar,

See this:

It is a good example of an attribution โ€“ and it will automatically create an attribution for you.

When I'm presenting someone else's work or something heavily influenced by someone else's work I'll do something like this:

ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท
Author	 :  Christopher Stone { Heavy Lifting by Shane Stanley }
Created	 :  2023/01/24 15:07
Modified :  2023/01/24 15:07
Task	 :
URLs	 :  
Tags	 :  @yourForumHandle, @KMForum, @Other-Tags
KmVer	 :  Keyboard Maestro 10.2 on macOS 10.14.6
MacroVr	 :  
ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท

Or this:

ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท
Author	 :  Christopher Stone <scriptmeister@thestoneforge.com>
		 :   building upon work by Shane Stanley
Created	 :  2023/01/24 15:07
Modified :  2023/01/24 15:07
Task	 :
URLs	 :  
Tags	 :  @yourForumHandle, @KMForum, @Other-Tags
KmVer	 :  Keyboard Maestro 10.2 on macOS 10.14.6
MacroVr	 :  
ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท

Comment dialogs support clickable RTF hyperlinks, and I prefer to use them rather than plain text.

-Chris

1 Like

Thanks for linking to this Chris. I wanted to (because I just started using it a couple days ago and love it) but for some reason wasnโ€™t able to find it on the forum.

1 Like

This is great! Now: how can I look up the 'currently-all-crammed-into-the-upper-left-corner-of-my-display' Finder windows by name from the list this generates, and then reverse the process (apply the stored bounds to the window with the name found in the list)? Because that would really help me stop pulling my hair out literally every time I either wake my system from sleep, or use the KVM to switch over to my PC for a few minutes.

Why would your Finder windows be 'crammed-into-the-upper-left-corner-of-my-display' in the first place? Do you use Stage Manager?

And do really need to know the bounds of all Finder windows, if not you could have open only those you need to know the bounds of, and then run the macro.

If I understand correctly?

/
with best regards,
Omar KN, Stockholm, Sweden

IDK what Stage Manager is. Never heard of it.

I'm running macOS 10.12.6 Sierra on an old MacBook Pro, and using a Displayport KVM. Anytime I switch the KVM to my PC, the display settings change on the MBP, and all the Finder windows (and every other app's windows for that matter) are moved & resized based on some much smaller resolution the display output defaults to when this occurs.

As I said, my main interest now is finding a solution to save and restore all open Finder window names and their bounds (or better yet, the paths to each window's currently-open folder). I tested this macro, but the sizes it saved weren't correct for some reason. The window I was testing ended up being in the right position, but was much too wide when I restored it using the captured width and height.

I'm stuck using KM v9 due to my OS version (can't upgrade it, so no KM v10), so maybe that's the problem (or a big part of it).

Additional info: Every so often, I need to reboot and reset the PRAM/NVRAM to fix an issue where the video output just stops working. When this occurs, even when I unplug/reconnect the KVM/Samsung display or open the MBP, neither the MBP retina display or the Samsung will show any video output, and I need to force reboot the MBP. This process will also result in all the open windows being moved/resized as well.

This is a bit complicated because of many variables:

  • maybe create a macro, which makes it easier to revert to the appropriate display settings?
    (or which at least opens System Preferences > Display )

  • which macro for the bounds were you using? # 1. or 2a or 2b?
    2b is straightforward to implement with KM measures.

  • "KM v9 due to my OS version," = no problem.

  • "I need to reboot โ€ฆ the video output." Definitely, the MB Pro reaches its CPU/ memory limit.
    If it helps, for example, get a cheap display for the PC, so when running a video, you don't use the KVM,
    or before starting a video, quit all other apps.

My 5 cents,

/
with best regards,
Omar KN, Stockholm, Sweden

1 Like
  • maybe create a macro, which makes it easier to revert to the appropriate display settings?
    (or which at least opens System Preferences > Display )

The display settings (dimensions, color profile, etc) are restored okay. It's just the window sizes/positions that are not restored properly with the display reconnection. I think it may be due to it being an ultrawide monitor (since they weren't really much of a thing back in macOS Sierra days, so maybe not supported very well by video drivers on my 2017 MBP).

which macro for the bounds were you using? # 1. or 2a or 2b?
2b is straightforward to implement with KM measures.

I tried vs.1. But, as I mentioned, when I tested it with a single window, it gave incorrect window size values. Didn't bother testing the others beyond that since I figured, "if it doesn't work accurately with a single window..."

Instead, I'm using this simple script to collect window names and bounds since it returns accurate sizes for all open Finder windows.

tell application "System Events"
	set _W to a reference to windows of application "Finder"
	return [_W's name, _W's size, _W's position]
end tell

This returns something like the following (when run in script editor):

{{"windowName1", "windowName2", "windowName3", "windowName4", "windowName5", "windowName6", "windowName7", "windowName8", "windowName9"}, {}, {{1944, 144}, {2605, 66}, {1130, 559}, {0, 559}, {1130, 22}, {0, 22}, {0, 1042}, {2583, 44}, {2561, 22}}}

Definitely, the MB Pro reaches its CPU/ memory limit. If it helps, for example, get a cheap display for the PC, so when running a video, you don't use the KVM, or before starting a video, quit all other apps.

The issue isn't with playing a video. It's sometimes when waking the MBP from sleep, I can't get any display output. Might be the video card is starting to fail.

Also, I need to work on both a Mac and PC for my job (game audio), so I need to use the KVM because of latency issues when using VNC or other remote viewer (from either machine to the other).

Really just hoping I can find a way to do what seems like it should be a relatively simple task here: collecting an array of window names and their bounds, and using that data to restore windows to the bounds saved for each matching window name.

If I were trying to script this in lua for REAPER windows, I'd have no problem. But trying to have this interaction with Finder via applescript... :face_with_spiral_eyes::face_with_symbols_over_mouth::exploding_head:

I've also posted about this on stackoverflow with additional info: AppleScript: Save/Restore the Bounds of Every Open Window by Path/Name

Obvious question first:

Why not? If that's a software requirement, have you looked at running that thing within a VM?

It sounds like a "re-size/re-position to match resolution" issue, which we used to see a lot with laptops using projectors -- the OS will change windows to fit the lower resolution, but won't scale them up or reposition when the big screen is reconnected.

Can you share how you did the restoration, either the AppleScript or the KM macro? It seems strange that the positioning was correct but the size wrong, but you sometimes have to re-position with one action then re-size with a second (although the usual error would be a smaller window, not larger).

Although I do wonder -- AS bounds are {leftEdge, topEdge, rightEdge, bottomEdge} while a KM rectangle is leftEdge, topEdge, width, height, which has caught me out a few times! (Also note that in AS a window does not have size, as you can see from the empty list {} in your Script Editor results.)

While you could do it with AS, you should be able to do it in KM as well. For the "Window Related Tokens", the All parameter returns results in window index order, so while you can't get "name and frame" as a unit you can create a list of names and a list of frames which are in the same order, then iterate through using a form of (pseudocode):

set frame of window named window[i] to frame[i]

How are you planning on collecting the data, then restoring it? Manual triggers? Does the "Display Layout Changed" trigger work with your KVM (I'd assume so) because that could work for the "restore" part -- I think it might be too late for "collection" though, as the windows may have already been re-sized...

1 Like

Please try
Window Information Tools - Macro Library - Keyboard Maestro Discourse

/

1 Like

Numerous reasons:

  1. because there isn't enough time to deal with upgrading the OS of my MBP because I need to use it daily for work, and can't afford the downtime.
  2. any other time I've upgraded one of my Macs to a new OS, a lot of the stuff that I use tends to start having weird new bugs/glitches, and some stuff just won't work anymore (mostly audio plugins and other audio tools/software).
  3. I spend far too much time as is trying to troubleshoot and fix issues with my systems. I don't want to add a bunch of new problems to solve to the list.
  4. new macOS means a larger disk footprint, and more resource hungry, meaning my MBP will end up running worse/slower than it does now.
  5. working on audio is very latency-sensitive. Adding another layer of a VM running just so I could still use various stuff that wouldn't work on a newer macOS version would not only be far more taxing to my already-strained system, but would also be much slower and have more latency issues.

The only real solution is to get a new Mac with newer macOS, and gradually transition over to using the new machine, keeping my current one set up as backup for any point where I run into a problem on the new system. and unfortunately, I'm not quite ready to drop the money for a new MBP or Mac Studio at this point.

Yes, exactly. Only instead of using a projector, I'm just disconnecting,reconnecting a 5k Samsung display.

Yes, this is likely what caused the problem. I probably just needed to convert between the different coordinate formats.

And I didn't notice before, but you're right. I only get the list of names and positions with that script, not all 4 window bounds coordinates.

Both using a hotkey, and automatically when switching my KVM to the PC.

Yes, I have something set up to trigger from display change current'y, but it's really simple. And I can simply periodically update the saved window data so that, when the display settings do change, I'll already have a fairly recent backup of all the window bounds.

This will also be helpful. Thanks!

And sorry, I now realize that the other script I posted doesn't actually collect a list of window sizes - just names and corner positions - and (@okn) your v1 script probably works fine as long as the coordinates returned for the bottom right corner of a window aren't entered into a KM action as window size values (which is exactly what I did when testing the macro out :person_facepalming:).

And now, back to waiting for the critical info I still can't seem to figure out (the reason I started posting in here to begin with): help with the syntax and structure of my RestoreFW1.applescript to get it working to iterate through a list of current finder window names/paths, search the saved FWdata for each current window name/path, return the saved bounds for any matches, and restore the current windows to their previously saved bounds.

While I've a lot sympathy for not updating a business-critical system which you know is stable, you're running an unsupported OS which hasn't received security fixes for many years. You've also missed out on general bug fixes, performance improvements, productivity enhancements...

A computer is a tool and, like any other tool, should be maintained if you want to get the best from it. When it's a business tool you should do "due diligence" (and maybe remain one OS behind current) rather than leap straight in with bleeding edge updates, but consider updating unless there's a definite reason not to.

And it isn't "downtime", it's worktime that helps make the rest of your worktime more productive -- like a taxi driver getting their car serviced or a gardener cleaning and sharpening their tools.

FWIW, I did an update from 10.12 to 10.15 (latest compatible) on Monday for someone who had refused to upgrade for "reasons" -- it took 4 hours, including a full Time Machine before starting and all app updates, and they've been in this morning saying how much better the machine is running and how they wished they'd done it sooner. So not every upgrade is a time consuming disaster!

Offering another -- get a refurbished machine, clone to that and test your upgrade path while you carry on working with the original.

Anyways, that's enough of that! Back to KM...

The main benefit of using AppleScript over KM for this is that AS can get Finder ids , which will solve the problem of having multiple windows of the same name when you come to re-position them.

tell application "Finder"
	set {winIDs, winBounds} to {id, bounds} of every window
end tell

While fast, that gets us a pair of AppleScript lists which we'll then have to somehow munge to text for KM, so probably easier to create the text as we go:

set outText to ""
tell application "Finder"
	set AppleScript's text item delimiters to ","
	repeat with eachItem in (get every window)
		set outText to outText & id of eachItem & "," & (bounds of eachItem as text) & linefeed
	end repeat
end tell
return outText

...so that outText is one line per window, each line in the form

winID,leftEdge,topEdge,rightEdge,bottomEdge

Getting that back into useful AS values from a KM variable takes a little more work but, once a line is separated out into id and bounds you can position the window directly -- do that in a try block so that if the window has been closed since the last "gathering" you don't get an error. You can set your own inText numbers using the output from the previous script:

-- theWindows will be fed in from a KM variable, this is for testing
set theWindows to "184,1357,325,2446,761
354,978,563,1748,999"

tell application "Finder"
	set theWindows to every paragraph of theWindows
	set AppleScript's text item delimiters to ","
	repeat with eachItem in theWindows
		set winID to (text item 1 of eachItem) as integer
		set winBounds to the rest of (every text item of eachItem)
		repeat with eachItem in winBounds
			set contents of eachItem to eachItem as integer
		end repeat
		try
			set position of window id winID to winBounds
		on error
			--do nothing, window not present
		end try
	end repeat
end tell

(And I'm sure the better AppleScripters here can improve on this idea.)

So, putting that altogether into a KM macro that

  1. Saves Finder window positions with โŒƒW
  2. Restores positions to the last-saved with โ‡งโŒƒW

Save or Restore Finder Window Positions.kmmacros (6.6 KB)

Image

1 Like

@Nige_S First off, THANK YOU! :star_struck: I can't wait to try out the macros you attached! This applescript is a lot deeper than anything I would've been able to figure out on my own. Really hoping this works and allows me to finally put an end to this window moving/resizing issue every time I switch systems or one/both of my displays go to sleep.

Well, that makes me feel a bit more hopeful about it. :slightly_smiling_face: I just know that, working with some of the finicky audio plugins/software that I use (especially those which haven't been maintained or updated in several years), other times when I've upgraded to a new Mac or OS version, I've run into problems where various things either wouldn't work anymore or became really buggy with the new setup. Trying to troubleshoot and fix those issues ended up costing me a lot more time than the OS upgrade itself, and spending my time dealing with those problems ultimately cost me my job at one point (due to the negative impact on my productivity). :frowning_face:

This is basically what I had in mind/meant by "get a new Mac with newer macOS". I just need to save up for a little while longer to get a newer Mac than the old 2017 MBP I have now (which is starting to warp and no longer closes properly due to a very swollen battery :frowning_face:), plus whatever new cables/adapters/hubs/etc that I'll need in order to connect it to the other pieces of hardware I use in my studio setup.

Thank you again for all the help with this issue (and my other posts in this forum)! :smiley:

@Nige_S I found just one tiny problem with the restore script that makes a major impact:

		try
			set position of window id winID to winBounds

should be:

		try
			set bounds of window id winID to winBounds

Aside from that, it seems to work perfectly, even with Finder windows with multiple tabs when a different tab is focused than before saving window ids/bounds!