How to (un)minimize a specific window?

I'm trying to replicate with KM something I did with BTT.

I would like to (un)minimize a specific window. So, a shortcut for:

  • If window "name" is minimized, then show it.
  • If window "name" is not minimized, then minimize it.

I have tried several options with "if then" and various "windows conditions". Unfortunately without success. Can someone please help me?

With BTT it looks like this. Should be more or less self-explanatory, even for non BTT users :slightly_smiling_face:

The problem I'm facing is that in macOS the front window can still be a minimized window. I'm trying to find a workaround.

Okay this works for me:

The only issue is that the animation takes about a second to complete. If you spam the trigger key too quickly, it may miss a trigger.

You said you wanted it to work with a specific app. I made it work with Keyboard Maestro Editor. You will have to try your specific app, which I don't have. So I couldn't test it for your app.

Basically, my idea was simply to try to activate the app using a menu item. Keyboard Maestro Editor has such a menu item. I'm not sure whether Scrivener has a menu item that brings it to full screen. You will have to look for one.

There might be other approaches to solve this issue. I will continue to ponder it.

1 Like

Thank you @Airy :slightly_smiling_face:. And sorry, but this is too much for me. Is that the whole macro? Probably not. Would you be so kind as to post the macro?

No, I didn't actually say that. The macro should always work and (un)minimize this particular window. :slightly_smiling_face:

Yes, that is the whole macro. I know, it's so simple it looks like it couldn't work, but it does! It's amazing!

Did you try it? It works with the KM Editor.

Your exact words were:

I would like to (un)minimize a specific window.

That's what I did. I made it work with a specific window. It's impossible for me to make it work with your specific window because I don't have your app. Therefore, you will have to take a minute to find a menu item in your app that brings your window to the front.

Yes, a specific window. But the macro should be able to be triggered when any app is the front app. I'll try that out.

Oh well, if you are talking about apps with multiple sub-windows, AND you want this macro to work with ANY application, not a specific application, that's a different question. I'm not sure if macOS keeps track of "which window in each app was the last one displayed." BTT may have its own internal trackers for that, but KM is limited to public APIs in macOS.

I'm still pondering it. I have a few more tricks up my sleeve.

Remember, your code only works with a specific application, Scrivener. Your code does not work with "Any Application". I'm not sure that BTT would be able to do that.

1 Like

Maybe that's the case.

Ok, mmm, BTT just seems to search for the window with this name, no matter which app is in front. And then it does what it is supposed to do with this window: (un)minimize.

Right. That means your BTT macro will work with a specific window with a specific name. That's exactly what my macro (above) does - it works with a specific program with a specific name - "Keyboard Maestro".

If you want to, you could make my macro work with Scrivener. All you have to do is find a menu item in Scrivener that activates Scrivener, and then modify my macro to activate that menu item. Of course I can't do that because I don't have Scrivener.

I'm still not sure if I understand you correctly (must be my bad English).

Your macro always works? Assuming Safari is the front app, I press the shortcut and that specific window of Scrivener is (un)minimized?

AHA! Now I understand.

You see, the problem is that I don't have BTT. Therefore I misunderstood what your BTT program does. You said "Should be more or less self-explanatory" but in fact it was not self explanatory. I thought your BTT program did something different from what it does. I won't explain what I thought it did, because that won't help you.

Okay, now we are getting somewhere. Now that I understand what you want, I will reconsider.

What you really want is this:

I want a KM macro that searches for a window containing the string "X" in the frontmost app and that activates that window within that app.

I'm looking for a way to do this now. Remember, BTT is not bound to public APIs in macOS, while KM is. If BTT is using a private macOS API to achieve this, then KM won't be able to achieve it.

Also, you didn't say what you want the macro to do if there are two windows open with the same name. It's entirely possible that this could happen. (If you had mentioned this from the start, I probably would have understood the issue.)

1 Like

Haha, sorry, what's self-explanatory for you with KM is probably self-explanatory for me with BTT. :slightly_smiling_face:

1 Like

This case does not exist. This specific window has a unique name.

Maybe in your specific case every window will have a unique name, but any solution I write will have to handle the case where two windows of the same name exist. Here's an example. This app has two windows open with the same name, "Test1".

Screenshot 2023-12-11 at 16.44.53

My macro has to make a decision when two windows have the same name. I presume you don't care what my macro does, which means I can pick any of them at random.

By the way, I'm working on a solution, but it requires that your app be AppleScript-compatible. I think you said your app is Safari, which I suspect is AppleScript compatible. Are there any other apps besides Safari that you want it to work with? You should list the names of any apps you want this macro to work with, because if any of them are not AppleScript compatible, my solution will not work.

Also, my solution will not work if you are using multiple desktops, or spaces. I hope you aren't doing that.

Just for your information, the approach that I'm working on uses this action:

What this macro does is save the name of every open window in a specific app (I may make it work with more apps in the future), into a KM global variable called Windows. Then it has all the information needed to activate the window.

The problem that I'm trying to resolve right now is how to convert an AppleScript list into a multi-line KM variable. I'm not very good with AppleScript, so this is taking a while.

Haha, thank you very much @Airy You invest too much time for me and especially in problems that that do not exist. My window has this name.

No other window will ever be called like that.

Safari was just an example. The point is to be able to trigger the macro always, from any app, no matter how many desktops are open. You're telling me that KM probably can't do that. Then that's the way it is. That's one of the reasons why I use KM and BTT. Thank you very much for your efforts. :slightly_smiling_face:

I'm willing to stop, but I'm halfway there. I'll try to finish my solution anyway. Although you seem to be saying that you want the solution to work with multiple desktops. That's a new requirement that I probably can't fix, because macOS doesn't provide public APIs for "spaces."

I see your window name contains emoji's. I have no idea if my macro will work with window names that contain emojis.

Okay, I'm so happy I got an initial version working. Here it is...

OpenScrivener Macro (v11.0.1)

OpenScrivener.kmmacros (4.8 KB)

In this example, when you use the PgUp key, my macro will minimize Safari if it is open, and if it is not, reopen the exact window in Safari that contains the word "Scrivener."

The tricks I had to use were (1) An AppleScript action to fetch the names of all open windows in the Safari app, and (2) isolate the exact name of the Safari window using a shell script.

After I saved the correct name, I restored the correct window using the saved name.

It can probably be modified to work with any app besides Safari, but Safari was the first app you mentioned, so that's the one I started with.

If you don't want to give it a try, that's okay, but I enjoyed the exercise.