Like I said, it's a deep dive if you're new to the language(s). If you post your working prototype (or even a much more detailed explanation of what you're shooting for) I'd be glad to see if I could point you in the right direction. Here's some further reading on fitting text using a combination of tech.
MAGICNUMBER - is just an approximator
FITTEXT - is just an approximator, and requires additional software called jquery to work
TEXTFIT - is just an approximator
FITTY - might work, can't be sure, no video demos to see, I may need to download it
TEXTFILL - might work, can't be sure, no video demos to see, but requires additional software
FLOWTYPE - does not scale the text to fit the box, it just scales the text as a function of box size
SVG - no URL was provided, so I can't tell how it works, but it claims to require magic numbers
There must be a system API that they use to obtain the width of the outputted text. How else could they possibly work if not through an API? Hopefully they aren't reading the pixels directly off the screen to see how the text was written. Do you know which API they would use or what the name of the API routine is?
There's no point in showing you my cheating code because it's not what I want anyway. I want something that isn't an approximator.
If Keynote can do it, it's doable. And I'm fairly sure it's done with the assistance of a macOS API. All I need to do is find out what that API is and then use a language that can access that API.
The nice video (GIF) you provided shows that that is definitely not what I want. The video shows that the scaling is very inaccurate resulting in a window that's sometimes 75% blank. I want an algorithm that shrinks to fit. And when I say "fit" I mean 100% fit, not 50% fit.
I am sorry if I sound frustrated. I know you're all trying to get me closer to a partial solution. And I appreciate that. But I've already got a half-baked solution. What I want is an actual real solution. That's why I suggested %CompositeTextWidth%. If there was such a token, I could write a solution that always came up with the correct answer. (Well, %CompositeTextHeight% would also be needed, but I didn't want to push my luck in my original post.)
@Sleepy oh I see, now I understood what you were talking about.
Hey there @Sleepy, I just happened upon this jQuery plugin and thought of you.
Might fit the bill (if you've not already found a solution).
Rather than have Keyboard Maestro automagically interpret RTF as attributed strings, I have added explicit filters to convert Attributed Text (in clipboards) to/from RTF text for the next version.