Get Colored Text Output From a Python Script

I am trying to get the clipboard output from a Python script to display the outputted text in color. I use the "Execute shell script" action and then set the output to a clipboard but there is no text formatting on the output.

I have tried python's colorama and I have also tried the python library "Rich". Both of these will print the text with color in terminal but not when run through Keyboard Maestro.

First create an ENV_PATH variable, more info:

Hey Matt,

I'm not going to take time to look at those libraries, so I'm not up to speed on all of their features. Nevertheless:

  1. As @hello mentions – you need to make sure Keyboard Maestro is properly aware of your Python environment. If you've installed a version of Python other that what comes with macOS, it's highly likely you'll need to do this.

  2. Colored text printed to a Terminal application is NOT rich-text – it uses ANSI escape sequence sent to the terminal app to produce the colors – so it's very unlikely the libs you're using could produce rendered rich-text outside of an actual terminal app.

-Chris

Hey Chris,

Good to know about the ANSI escape sequence, The "Rich" library says for "Rich text and formatting" but they probably just mean it looks like rich text and not that it actually is.

Is there any way to get python to send colored text to KM? I may just have to make the rest of the app in python, but I was trying to avoid that since I don't really know how to write python. The output from the script is just kinda hard to read without color or underlines or something like that.

You can always compose HTML and then send that through the Unix command line app textutil and pipe to the clipboard.

See this for an example:

-Chris

Once you have RTF on the clipboard you can display it in Keyboard Maestro using a Display Text action.

-Chris

1 Like

I will have to try that out. Thanks!

1 Like