Minimize and Maximize a chrome tab with a name that changes?

I have a YouTube tab that I like to watch whenever something is rendering, etc. Is there a macro that I can program that would apply just to this tab in such a way that it could be minimized and maximized? Keeping in mind that the name of the tab constantly changes, whenever there is a different video, etc. (meaning that I wouldn't be able to utilize the macro that involves opening a tab with a specific name)

If there is indeed some well-defined sense in which it is the same tab, with some identifiably unchanging property - its positional index for example - then you should be able to get a usable reference to it in JavaScript.

Any sense of what that unchanging feature is ?

Is it perhaps just the first tab with a URL which begins with the YouTube site address ?

Here is one way of obtaining the name of the first tab in Chrome which has a url starting with https://www.youtube.com

Name of first YouTube tab in Chrome.kmmacros (4.8 KB)

Any sense of what that unchanging feature is - yes, it will always be a youtube video. However, each time the video changes it does not appear as though the word "YouTube" exists in the tab anymore.

For example, if I am watching this video, here is what is displayed in the tab
Screenshot 2024-03-20 at 12.01.48 PM

Then, if I go to watch an entirely different video, there is an entirely different name in the tab:
Screenshot 2024-03-20 at 12.01.55 PM

Granted, the WEBSITE is always the same (Youtube) but the name of the tab is always going to change.

Understood. The Macro above should always be able to tell you what the current name of your YouTube-viewing tab is.

Thanks for this! I might need a little more help though, I'm afraid. I have imported it and assigned a hot key to it successfully, but it doesn't seem to work…

Specifically, the goal is to be able to minimize and maximize the YouTube tab.

It doesn't, on your system, display the name of the first tab containing a YouTube url ?

Are you, perhaps, using Keyboard Maestro 10 (or earlier) rather than Keyboard Maestro 11 ?

That is the only part of the problem which the illustrative macro (above) aims to solve – getting the name. The rest I have left as an exercise for the reader.

"minimize and maximize" In Chrome UI terms this means close and open ? Or do you have something more specific in mind, like a particular menu path or widget ?

Here (choose your hotkey) is a first guess at the kind of thing that you might have in mind:

First matching Chrome tab opened or minimized.kmmacros (4.7 KB)


Expand disclosure triangle to view JS source
return (() => {
    "use strict";

    const main = () => {
        const
            chrome = Application("Google Chrome"),
            window = chrome.windows.at(0);

        return window.exists()
            ? (() => {
                const
                    matchingTab = window.tabs.where({
                        url: {
                            _beginsWith: kmvar.local_urlStart || ""
                        }
                    }).at(0);

                return matchingTab.exists()
                    ? (() => {
                        const
                            matchID = matchingTab.id(),
                            matchIndex = window.tabs()
                            .findIndex(
                                t => matchID === t.id()
                            ),
                            displayed = !window.minimized() && (
                                window.activeTabIndex() === (1 + matchIndex)
                            );

                        return displayed
                            ? (
                                window.minimized = true,
                                `Minimized: ${matchingTab.name()}`
                            )
                            : (
                                window.activeTabIndex = (1 + matchIndex),
                                window.minimized = false,
                                `Maximized: ${matchingTab.name()}`
                            );
                    })()
                    : openURL(kmvar.local_urlStart);
            })()
            : openURL(kmvar.local_urlStart);
    };

    // openURLInChrome :: String -> IO String
    const openURL = url => (
        Object.assign(
            Application.currentApplication(),
            {includeStandardAdditions: true}
        )
        .doShellScript(
            `open -a "Google Chrome" "${url}"`
        ),
        url
    );

    return main();
})();

It doesn't, on your system, display the name of the first tab containing a YouTube url?

  • In the tab, no. For example, I'm watching a video titled "What's on these GENESIS floppy discs?" So that is also the text in the tab. But in the WEBSITE, yes; it DOES list youtube.com as the first thing. See previous screenshots as well as this one, taken just now:
    Screenshot 2024-03-21 at 9.20.58 PM

Are you, perhaps, using Keyboard Maestro 10 (or earlier) rather than Keyboard Maestro 11 ?

  • Nope. I'm on Keyboard Maestro 11.

"minimize and maximize" In Chrome UI terms this means close and open ? Or do you have something more specific in mind, like a particular menu path or widget ?

  • Correct, minimize and maximize : )

Still not having any luck with this macro that you so graciously programmed and sent over to me, however. I've assigned it to a hot key and everything. All it does is give me a program beep sound as though nothing is happening, and....nothing is happening. Obviously, I'm missing a step or something here, I'm just not sure what.

First or second macro above ?

(the first is just an illustration of obtaining the name of the first tab with a given prefix)

You have enabled the second macro ?

A second (simpler) approach, and third macro for you to try:

  1. download
  2. enable
  3. experiment with.

This time, using the Maximize ⇄ Minimize named window action, after checking whether Chrome is displaying a Youtube page.

Maximize ⇄ Minimize Chrome window if active tab shows YouTube.kmmacros (6.8 KB)

Hmm......still not working. Also, this screen popped up:
Screenshot 2024-03-22 at 5.49.37 PM

For clarification, I do have a YouTube tab open.

Which of the three macros are you testing ?

(The 3rd only applies to the case where the frontmost is the youtube tab)

but it begins to look as if I can't help you : -)

(Perhaps our systems or assumptions diverge too much ?)

I have tried all three, unfortunately. I'm certainly more than happy to hang with you through this though , if anything just because I am like a dog with a bone with these little problems! But I certainly understand completely if I'm wearing you out. On my end, I would love to figure it out, however…

Indeed, I have tested all three options.

If you would be at all interested, I would be more than happy to meet you online through Google meet or whatever so that you could see my screen and go from there?

Hey friend, just wanted to circle back with you one more time on this. Would you be up for a quick zoom / Gmeet so that I could share my screen with you and see if we can get this figured out? It's got to be a simple glitch that I'm sure could quickly get solved on.

Probably not an option from behind my firewall – may be better for you to find someone who knows Chrome better (I don't really use it much myself).

Start from first principles. Is Chrome responding as expected (or even at all!) if you ask it for the URL? A simple action will test that:

image

You don't need to run a full macro, just select "View" from the action's cog-wheel when a YouTube tab is frontmost in Chrome.

Will the YouTube page always be frontmost in Chrome when you want to min/max it?

AppleScript is gobsmackingly simple sometimes...

You've got a bunch of windows open in Chrome. Each of those windows has a bunch of tabs. Many of those tabs are open on a YouTube page, and more than one is the active tab of its window. You're playing a video in one -- but the only thing you know is that, out of all the windows that have an active YouTube tab, its window is the highest in the stacking order.

So you want to minimise the first window you come to, working front-to-back, whose active tab has an URL starting with "https://www.youtube.com":

tell application "Google Chrome"
	set minimized of first window whose active tab's URL starts with "https://www.youtube" to true
end tell

And if you've got many minimised windows but only one whose active tab is on YouTube:

tell application "Google Chrome"
	set minimized of first window whose minimized is true and active tab's URL starts with "https://www.youtube" to false
end tell

This won't cover all cases -- for example, you're playing a video in the active tab in a window but you have another Chrome window open to YouTube above it in the stacking order, but it's a start until the problem can be more clearly defined. If you want to go further, like only minimise the first window actually playing a video, you'll need to use JavaScript.

Thanks for chiming in! I'm really hoping to figure this out.

"Is Chrome responding as expected (or even at all!) if you ask it for the URL?"
To be honest, I'm not sure.
I've created the following macro (mirroring what you suggested):
Screenshot 2024-04-02 at 8.45.00 PM

But when I execute it, this Window pops up:
Screenshot 2024-04-02 at 8.44.08 PM

Sounds like you may need to enable:

Chrome > View > Developer > Allow Javascript from Apple Events


See also:

https://wiki.keyboardmaestro.com/actions/Browser_Actions#Chrome_Allow_JavaScript