Keyboard Maestro: Titling Revisited
26 Jun 2021Last year I published a post about how I was using Keyboard Maestro along with a Python script that accesses titlecaseconverter.com to convert titles into a specified style. Unfortunately, I think there was a change at the site level that started causing issues for me either last year, or earlier this year. As a result I ended up taking the existing Python code, simplifying it for my own specific needs and tweaking it for the issue I was experiencing. In today’s post I’m going to quickly share the update.
Simplification
In investigating issues with code that I did not originally create, I have a general approach of if the answer is not immediately obvious, I start to break down the code to get it to a simple form that allows me to address the issue. In this case, because I only ever use one particular style, I immediately stripped out all of the code around processing variations.
I think the issue was to do with a change in the URL scheme, and after I figured that out, rather than passing out to CURL
to do the processing, I switched the code to use a direct request
call from Python. By returning the result as JSON, this further allowed me to simplify the extraction of data.
This left me with a much smaller set of working code, but also with less flexible code in that it is tailored to my specific requirements. That is fine for me, but may not be for others - though it does open up an opportunity for others to tinker with the code to meet their own particular requirements.
#!/usr/bin/env python3
import os
import urllib.request
import json
import sys
#Build the input string
strURL = "https://titlecaseconverter.com/tcc/?styleC=true&preserveAllCaps=true&title=" + urllib.parse.quote(os.environ["KMVAR_LocalParam"])
#Retrieve and process the JSON
jsonContent = json.loads(urllib.request.urlopen(strURL).read())
strTitle = ""
for jsonItem in jsonContent[0]["title"]:
strTitle = strTitle + (jsonItem["joint"] + jsonItem["word"])
#Write out the title
sys.stdout.write(strTitle)
Updated Macro
If you wish to utilise this change, you can build the macro yourself from the information presented here, update based on the original post, or download the updated version.
Conclusion
The statements from my original post still stand. Nick Janetakis , who created the original Python script, and the generosity of the creator of titlecaseconverter.com deserve the praise on this one. They take a little bit of the pain and tedium out of my workflow and hopefully out of yours too.
I can’t say that my new script/macro is noticeably quicker than the previous one, but it is certainly one that I think will be much simpler for me to maintain going forwards should any issues occur.