Keyboard Maestro to launch Automator workflow, or Automater to trigger KM Macro?

I am curious to hear thoughts on pros and cons.

As documented in my other postings about workflow for file handling, I have created some scripts that I am now using to format filenames and folder names. As I have been using them to clean up my files and folders, they have proven extremely useful at creating uniformity in my naming scheme.

Right now, I have implemented them as Python scripts that do the major part of the work, basically by taking in a file or folder name and figuring out the “correct” file or folder name to emit based on my underlying scheme. I have them created a series of KM macros, each of which looks at the current file(s) selected in Finder and applies the scripts to each item, with some additional KM action blocks that support the running of the script. There are right now 5 macros, and it set up so I can trigger a KM palette from which I select the appropriate macro to be run. This has proven very efficient.

The additional parts of KM basically related to a) looping through the Finder selection, b) checking depending on which macro it is to ensure the selected item is a folder vs file (eg don’t process a folder in the macro that renames files), and c) using a bash script that takes the supplied file or folder name, feeds it into the Python script that does the formatting, and then doing the necessary rename. Fairly simply bash scripts.

However, I often find that I have selected a bunch of files or folder and it is natural when working at my desktop with my Logitech mouse to just right click rather than jumping to the keyboard to trigger the KM palette and select the proper item, so it occurs to me that it would also be a good idea to encapsulate these operations into Finder services (or whatever they are called now under Mojave) as well.

It would be easy enough to create de-novo services in Automator and have them run the same embedded bash script. However, I don’t like that sort of duplication. The whole point of creating Python scripts as standalone scripts rather than embedding the logic in the KM macros is to isolate that logic, so it can be used in multiple places without code duplication, thus avoiding the issue of keeping the same script in two locations (Automater and KM, for example) in sync. By having a separate script file, I can make changes to that and the results are automatically available to any wrapper that uses it.

Therefore, it also makes sense to implement the full functionality in one place and then wrapper it for use elsewhere. So I can either create an Automator action that just calls the KM macro via AppleScript, OR I can embed the bash script in an Automator action (which itself calls the external Python script) and have KM execute that Automator action. Again, only one code base to be changed when something needs fixing or modification.

I am curious to know what others think is the more efficient solution, or a the better solution for whatever reason.

Right now, for this purpose, I am strongly leaning towards keeping the macros in KM as is (since they are already finished and working anyway), and have the Automator scripts call the KM macros. My major motivation for that approach is that KM macros are synced between all of my computers through Dropbox, while services do not sync between different computers, and so keeping functionality in KM when that is feasible and recreating the Automator actions/services on each machine seems less difficult and error prone compared to having to recreate a more complex Automator action on each machine.

Thoughts?

1 Like

Quick Actions, I believe, which is no more intuitive than “Services”.

I’m a little confused here. Are you running a bash script just to execute your Python script? That shouldn’t be necessary, just put #!/usr/bin/python at the beginning of your Python script.

Personally I have chosen KM hotkeys. I found Quick Actions not, er, quick enough.

Yes, I do use bash scripts to execute python scripts.

I have written the python scripts to do the filename or foldername formatting. For example, I have starting naming files in the convention of _filename.pdf, where the date can be in a format of yyyy.mmdd, yyyy.mm, or yyyy depending on need (most common is yyyy.mmdd). The filename can be multiple words, but all separators are underscores. While this is the most convenient way (for me) for a file to wind up named, it is by far not the most convenient for typing, so I often type what should wind up as, for instance, 2019.0617_this_is_a_file.pdf as “this is a file 2019.0617.pdf”.

The python script can take that filename as typed and “fix” it. I can also take files without a date in the name and add the file’s creation date or content creation date, force all characters to lowercase, check that the file exists, etc. It has a lot of checks internally to do the “best” job of renaming a file.

To make the script as useful as possible, it takes in a fully qualified filepath and emits just the corrected filename part. This way, the script becomes essentially a module that can be used by KM, Hazel, or any other utility to perform the filename reformatting.

In KM, this script is wrapped in bash script that takes the filename, pipes it through the formatted, takes the result and IF it has chanced, then renames the file. The same script can be used wrapped in AppleScript to return a changed filename for Hazel rules. I also have a wrapper python program that can be executed at the command line to rename files, and has options to recourse down an entire folder tree. It pipes the filenames to this script and gets back the corrected filename.

My intent was to create a script that basically works as a filter for filenames (the foldername corrector the same implementation), and that is why it is called by bash. No different that a bash pipeline… eg mdfind | grep | awk | sort, for example.

I agree that KM hotkeys are convenient, and I have used the “META” key trick of using Karabiner elements to make META-R pop up a palette of all of my renaming tools that I have created. It’s really efficient on my laptop where my hands are near the keyboard even when working on the trackpad. However, on my desktop I have a Logitech dual button mouse, to after selecting a bunch of files, right clicking is faster than going to the keyboard. However, then you need to click the quick actions menu for the pop up…but still reasonably fast. I put together an Automator action last night that runs the KM macro, although it might just type the bash script in anyway despite what I said above since it’s only a few lines of code anyway, as the heavy lifting is in the python filter.

What would be very cool would be to be able to either a) have Quick Actions designed to appear directly in the right-click pop up menu instead of the submenu, or b) have KM able to have it’s menu appear in the pop-up menu. I don’t know if the latter requires a Finder extension or if there is some simpler way to make that happen, however.

FYI there’s a new overview of Palettes just posted today at iMore:

This sounds exactly like what I have been struggling to do. I have a bunch of files I want to rename based on creation date.

I tried in hazel KM Automator. I feel like the missing piece is a script. But I don’t know how to write scripts. I was hoping to find a starting point somewhere. All I really want is to prepend the creation date to the file name. Eg. yyyy-mm-dd - file name.extension.

Many of those files are currently in Devon think after being exported from Evernote.

And resources to do the same in either of those would work too.