[KM Macro] Open a File Outside the Zettelkasten Identified by a UID
This Keyboard Maestro macro will open the file in a specified directory with a filename containing the currently selected text. The intended use case is a UID scheme for documents outside the Zettelkasten; as such, the macro expects that only one file will match the search string.
As an example, a daily/diary note might contain a line like:
- [[Project Name]]: The table of sources has grown too large and unwieldy for Markdown. Moved it into a spreadsheet (202007071949).
Double-clicking the spreadsheet's UID and triggering the macro will open the spreadsheet in the default application for its filetype.
Links:
- GitHub repo, Readme section specific to this macro
- Direct link to KM macro file (you may have to right click and 'save link/target')
Howdy, Stranger!

Comments
I saw that you are using the
findcommand. How fast is it for you? I've got basically the same action for my Zettelkasten Alfred-Workflow and use the spotlight-commandmdfind, which is very fast in finding files with names containing a UID anywhere on my hard disk.(where $query is the UID or any other part of a filename).
Thanks, @rhubarb. I'm new to shell scripting and wasn't aware of
mdfind.I used
findbecause I wanted to keep to standard Unix commands wherever possible, and to search the files themselves rather than a database which may be out of date (aslocateandmdfinddo). I'm guessing that Spotlight is pretty good at keeping its database up to date though, so maybe this isn't a practical problem formdfind.To test speed, I
cded into a directory of about 600 plain text files (inside a VeraCrypt volume):Testing
mdfind:Testing
find:Running the search on the whole volume, which contains about 39,000 files:
mdfind:0.01s user 0.01s system 9% cpu 0.169 totalfind:0.03s user 0.17s system 98% cpu 0.196 totalRunning the search from
/(that is, the whole of Macintosh HD):mdfind:0.01s user 0.01s system 37% cpu 0.043 totalfind:6.15s user 88.16s system 53% cpu 2:54.83 totalfinddidn't have permission to search many directories and caused a number of permission request dialogues to pop up.So I infer that
findmay be faster for highly targeted searches, but as the number of files to be searched increasesmdfindquickly catches up, and at some point between 39,000 files and the whole contents of my laptop,findbecomes unusably slow.This StackExchange answer is useful in understanding the pros and cons.
Please ignore the test results above.
I don't think I kept the number of matches to each search consistent at one, and the search string in the case of both
findandmdfindshould be sandwiched between asterisks (e.g.*202007221713*) for a fair comparison and to find a file or folder with the UID anywhere in its name.In summary:
The macro now uses
mdfindbecause:mdfindis much faster thanfindexcept in the case of highly targeted searches.findcan easily become uselessly slow.mdfindto miss a file. That Spotlight's database might be out of date doesn't seem to be a practical concern. Furthermore, this Stack Overflow answer asserts that any metadata attribute withFSin it is not stored in Spotlight's database at all. I haven't seen this corroborated, but searches onkMDItemDisplayNamedo seem to run faster than onkMDItemFSName.Other file systems (at least exFAT) and VeraCrypt volumes (even if formatted "Mac OS Extended") don't retain the metadata attribute
kMDItemDisplayName. For this reason, the macro searches onkMDItemFSName, despite being slower.The macro now opens folders as well as files and has been renamed to reflect this. This is an updated direct link to the macro (you may have to right click and 'save link/target').
timetest results:In each case, a single file matches the search.
Run from the root of a directory in a VeraCrypt volume containing about 620 text files:
Run from the root of a copy of that directory, outside of the VeraCrypt volume:
mdfind, searching onkMDItemFSName:0.00s user 0.01s system 22% cpu 0.058 totalmdfind, searching onkMDItemDisplayName:0.01s user 0.01s system 27% cpu 0.058 totalfind:0.00s user 0.00s system 87% cpu 0.005 totalRun from the root of a VeraCrypt volume containing about 33,000 files:
mdfind, searching onkMDItemFSName:0.00s user 0.01s system 9% cpu 0.136 totalfind:0.07s user 0.14s system 98% cpu 0.207 total(permission errors)Universal search, including mounted volumes (a partition, external HDD, other VeraCrypt volumes):
find:12.60s user 92.72s system 67% cpu 2:36.92 total(numerous permission errors)Universal search, excluding mounted volumes: