Send Terminal commands via Keyboard Maestro

I am using this Terminal Bash script. It allows controls of the speed of the built-in MacOS Text-to-speech function, by typing the command TTS in the terminal. E.g. Typing TTS 100 will set the speed to 100.

I would like to incorporate the TTS part into a Keyboard Maestro macro.

I assume "Execute Shell Script" would be the best way of doing this.

This is my macro:

But when I run it, I get an error that says "Macro Cancelled" appears in the Notification area.

Unfortunately, the error doesn't fit in the notification, so I can't see it properly. Any ideas?

You can check the KM Engine log by going to Help > Open Logs Folder and double clicking the log file for the engine, where you should be able to see the full error message.

Incidentally, did you know KM has a Speak Text action that can be configured to speak text at different speeds and with different voices? It could be a good alternative to setting the macOS TTS function to different speeds.

41%20PM

When I look at the log, it says

"tts: command not found"

Any idea how I can fix this? Is it because TTS comes from a terminal bash script?

Incidentally, did you know KM has a Speak Text action that can be configured to speak text at different speeds and with different voices? It could be a good alternative to setting the macOS TTS function to different speeds.

I am actually trying to get the macro to work with Dictater which works with the built in TTS service to display the words that are being read (Like a teleprompter on a TV show). It makes it very handy for proof reading!

Short Version:

Try this instead:

/usr/local/bin/tts 1

Long Version

tts is most likely a 3rd party tool installed somewhere like /usr/local/bin/tts. There are other places it could be, which are collectively referred to as your PATH or $PATH

Unfortunately, Keyboard Maestro does not inherit the $PATH from your shell, and there's no good way to tell Mac apps which $PATH to use anymore (there used to be, but Apple removed it and didn't supply a replacement). I believe it uses a default $PATH but that does not include /usr/local/bin/ as one of the folders.

There are basically three ways to deal with this:

  1. Put your shell commands into a text file (aka a "shell script"). I generally use this for anything more complicated than 1 or 2 lines. The main benefit here is that you can edit the script even when Keyboard Maestro's editor is not open.

  2. Make sure your commands use the "full path" if they are not in the default $PATH. This is usually the best way to go if you are just using a few lines of shell commands.

  3. You can also declare a PATH at the top of a Keyboard Maestro "Execute Shell Script". The benefit here is that you can tell it to look in all of the places you want, in the order that you want. The downside is that if you add a new folder to your PATH, you have to go into each shell script and change it.

In your case, I would use option #2.

In Terminal.app, type which tts and it should give you a path such as /usr/local/bin/tts

Then use /usr/local/bin/tts 1 in Keyboard Maestro's window.

1 Like

Thanks, tjluoma.

here are some notes for anyone else trying this.

  1. I had to go to the Finder and press cmd + Shift + G to open the "Go to Folder prompt".
  2. In that, I entered /usr/local/bin/.
  3. I then found the file name of the TTS script. It is tts5000.sh (Most the other scripts in that folder use the same name as the command, so I am not sure why the TTS author did it differently. Perhaps TTS is too common a name...)

So the finished command I put in Keyboard Maestro was:
/usr/local/bin/tts5000.sh 1

(And I can swap 1 with what every value I want).

That worked perfectly! Thanks!

This may be true for most Mac apps, but KM does let you specify a $PATH for use in all Execute a Shell Script actions. All you have to do is make a variable called ENV_PATH and add the paths you want to it: action:Execute a Shell Script [Keyboard Maestro Wiki]

Note that if you select Help from the gear menu in the Execute a Shell Script action, it would take you to the wiki help which talks all about PATH and how to set it or deal with issues related to it.