Getting started with Automator

illustrations illustrations illustrations illustrations illustrations illustrations illustrations
post-thumb

Published on 14 March 2024 by Andrew Owen (4 minutes)

Although I pretty much have this website how I want it now, I’m still tweaking the CSS and adding features. Lately I’ve added some more social networks to the landing page and some more options to the social share buttons on the articles. But one thing I really wanted to add is the option to listen to articles. I considered services which offer a free tier, such as Trinity Audio and Speechify, but I had too much existing content. Incidentally, Speechify’s AI version of my own voice was spooky. I could tell it wasn’t a real human, but only just. So in looking for a zero-cost solution, I ended up using macOS’s Automator app. I also decided not to convert articles with code samples, because those are best left to dedicated screen readers.

The Macintosh has had text-to-speech support since 1984. But the quality has improved a lot in recent years. I still don’t like listening to computer voices, but I find Australian female Siri tolerable. However, as these are my words they really ought to be in a male voice, at least for the French version. To add voices to your system, from Settings, go to Spoken Content and in the System Voice list, click Manage Voices. Ideally, you’ll want to choose a premium voice. If one isn’t available, then enhanced is the next best option. I ended up going with Evan (Enhanced) for US English and Nicolas (Enhanced) for Canadian French. My own accent is somewhere between North London and New England. And my French content is partially machine translated, so it sounds weird to a native of France to begin with. Having settled on the voices, I now needed to find the most efficient way of converting the content to an MP3 file for inclusion in the article. And that’s where Automator comes in.

Apple provides a user guide, but it’s very basic. You can create workflows that run in the app, standalone apps, quick actions ( including services) and more.

The simplest way to create a workflow is using the block code approach. Select an input and then drag various actions from the list and populate them with varaibles.

The main hurdle I had to overcome was figuring out how to take multiple inputs. There are no parallel steps, so the solution is to store inputs as variables and then ensure the next step ignores the output of the previous step.

Here’s how I created a Text to MP3 (French) service:

  1. In Automator, from the File menu click New and select Quick Action.
  2. Set Workflow receives current to text in any application.
  3. From the Actions tab, search for Set Value of Variable and drag it to the workflow. Click New Variable and enter TextToSpeak in the Name box, then click Done.
  4. From the Actions tab, add a Get Contents of Clipboard action to the workflow under the last action. Click the Options tab and select Ignore this action’s input.
  5. Add a new Set Value of Variable action and set the Variable to a new variable called filename.
  6. Add a Get Value of Variable action and in its Options, select Ignore this action’s input.
  7. Add a Text to Audio File action and select your preferred System Voice From the View menu, ensure Variables is visiable and drag the filename variable to the Save As box. After the variable, enter .fr. Select a save destination form the Where list.
  8. Add an Encode to MPEG Audio action and under Destination, select Same as source file and Delete source files after processing.
  9. Add a Rename Finder Items action and select Replace Text from the list. In the Find box enter m4a, select extension only from thin the list and select Ignore Case. In the Replace box enter mp3.
  10. Save the workflow and give it a name. Exampel: Text to MP3 (French).

Now to convert text to audio:

  1. Open the text in an application. Example: a page in a web browser.
  2. Copy the filename you want to use to the clipboard. Example: the slug at the end of the URL.
  3. Highlight the text you want to translate.
  4. Right click to open the context menu and from Services, select your workflow. The file is saved to the locaiton you selected in the workflow.

In Hugo, I added this code to my single.html:

{{ if .Params.audio }}
    <audio controls preload="auto">
        <source src="/audio/{{ .File.BaseFileName }}.mp3">
    </audio>
{{ end }} 

If audio is set to true in the article metadata, it converts the slug (including the language suffix) to the name of an mp3 file in the audio path. The advantage of this approach is that after I’ve translated an Englsih text on my site, I can click Français in the menu and the slug is still on the clipboard. It’s also possible to use shell scripts with Automator, so it may be possible to further automate the process. But that’s a task for another day.