In the beginning, all my macros used variables, text, and big ugly regex to process out what I needed to get to. Over time, I realized that this was the hard way of doing things: While functional, debugging was hard, and any little change in the macro could require rewriting a lot of regular expressions.
Then, as I was working on an update to Quick Web Search, I had a thought to see what might be possible with sqlite3
, a free database app included with every Mac. At the time, I knew a reasonable amount of simple SQL, but nothing about integrating sqlite3
with Keyboard Maestro. To say I struggled would be an understatement.
But once I got my head around things, the end result was a remarkable improvement. What had been literally dozens of regular expressions to add, remove, and modify shortcuts in the list became a few very simple SQL calls to the database, with just a bit of regex to format the data in the right format. And things that were slow to do with certain huge variables would happen instantly, or nearly so, when queried from the database.
Databases are also a great way to store lots of user settings in a permanent manner. Instead of creating multiple globals, or an array-like global that needs regex processing to use and update, a simple table can keep your user's preferences and provide simple updating. When the macro launches, read the settings and split them into instance variables. If they get changed, write them out to the database, etc.
I'm also using sqlite3
in MacroBackerUpper, and coming updates to both Macro Usage Counter and The Decrufter will add database functionality, to improve speed and offer additional features.
I have a tutorial series in mind that would walk through the process of setting up a new macro that involved the use of a multi-table sqlite3
database, but before I spend any time on it, I'm curious if there's enough interest to make it worthwhile.
If you'd like to see such a tutorial series, let me know.
-rob.