How can I move a window with an unpredictable title?

The Mail app on my desktop Mac has six Viewer Windows on three monitors:

Main (center) monitor:  "All Inboxes"
Left monitor:  "Red" & "All Sent"
Right monitor:  "All Junk" & "All Drafts" & unpredictable name (often "Searching")

Sometimes when my Mac awakens from sleeping, all Viewer Windows have moved to the Main monitor.

I've created a Keyboard Maestro macro that moves the Viewer Windows with predictable names to their proper position. The window I use for random searching presents a problem because its name is unpredictable when I've canceled my last search.

I'm seeking a way to identify the Viewer Window with an unpredictable name. Keyboard Maestro's "Move Window(s)" steps offer several choices other than window titles. "Window index" seems promising if I could determine the index of my "Searching" window.

How can I move a window with an unpredictable title?

1 Like

Perhaps try to access the windows using their index number instead of their name. I'm not certain if that will work, but it's worth a try.

1 Like

Airy, in my opening post, I mentioned using the Window Index. Other than trial and error, how do I find the Index of the desired Mail window?

I have a Keyboard Maestro macro that displays window attributes (e.g., title, size, position), but there appears to be no Token for Window Index.

Sorry I missed that point earlier. In any case, yes, I was trying to imply that you check using trial and error. What's wrong with that approach? Do the numbers change over time? That's what I was hoping you would test.

By asking about a token, are you trying to convert a window name to a number? If that were possible, we could just use your original solution and use the name. My idea is to use a number instead of a name. Just find out the number for each window using trial and error.

A few days ago I posted a similar solution for someone else using AppleScript to get the names of all the windows for a specific app. You may also find that approach to be very helpful to solve your problem... (my solution was for the Finder app, so you will have to edit this solution to make it work for the Mail app)...

That was the AppleScript, but then I had to use a little magic to extract the window names from the list of Window Menu items, which I did as follows, but you will have to edit that as well to work for the Mail app...

This approach is probably better for you, as it doesn't rely on index numbers. If you can't figure out how to adapt my code to work with Mail, I can probably help you with that later.

1 Like

One way to do it:

You've got 6 windows.
5 window names are known.
1 window name isn't.

If you activate and move the 5 known windows first, the 1 that's left is your unknown window and will be at the back (index -1).

Note: For me, at least, Mail's window names have message counts appended, with a (space, en-dash, space) separator, so you may need to account for that in your string matching.

This cascades the windows -- change the numbers in the first action to what you want (they're the x,y of the top-left corner of the window) and the last action for the "unknown" window:

Move unknown by index.kmmacros (6.2 KB)

Image

Another method would be to check each window's name and move it accordingly -- the "Switch/Case" action has an "otherwise" that lets you match "everything that doesn't match the previous entries". This example uses both window indexes and names to target windows:

Move unknown by Switch-Case.kmmacros (11.1 KB)

Image

That's just two, hastily thrown together, examples which can certainly improve on -- and I'm sure there's a bunch of other approaches to this.

When you list all an app's windows they are in index order:

%WindowName%All%

...so you just need to work out which line your "unknown" window is on -- the easy way is work through the lines in turn, stopping when a line doesn't match one of your known names:

Find unknown window index.kmmacros (4.8 KB)

Image

There's, deliberately, a whole bunch of different techniques in the above macros to show that TMTOWTDI. Hopefully they'll give you some ideas.

1 Like

Another option might be this: I encountered a similar problem when I restart my computer. I always use several desktops, each with a safari window open, with standard pages open on each desktop. The problem was that when you restart, Safari does not open on all desktops, but opens just the first. All the other safari windows can be opened, but that gives the same problem you have with mail, what name is stored for each window may vary. My solution was this: I made a KM macro that sets the windows to a set name before shutting down (in my case I just used a number 1-4, created in pages and named as such and stored locally). Then after startup I can bring each named window to the front and move it, again using a KM macro. I am not at home to try it on my mac, but a similar approach might work in mail. There has to be a naming convention to the windows, I would try to use that to set all windows to something I can always call up.

1 Like

Your suggestions are always excellent, Nige. I am your Padawan in training.