Link to Keyboard Maestro macro (uploaded in disabled state): TAG OPEN DOCUMENT(S) - v1.3.kmmacros
So, it turns out that even if a document is open, it can be tagged in MacOS. This makes me happy, and it can make our lives a little bit easier.
Sometimes when I am working with an open document or a batch of open documents - usually a PDF or Word or TextEdit document or documents - I want to quickly tag it/them for later quick retrieval via Spotlight. But to do this, I would typically leave the work I am doing in an open document, locate the document in Finder, and tag the item there, then go back to the open document. That’s a pain. For multiple open documents, often from different file locations, it becomes maddeningly tedious, when all I want to do is quickly tag my open document or documents right when it occurs to me to do so, and then get right back to work.
This macro utilizes/adapts scripts by Nige_S (see this Macro and discussion of scripting solutions for tagging multiple open documents), and Chris Stone (see discussion here) to gather the filepaths of the open documents. Then it prompts the user for comma-separated tags and sets them (by adding to or replacing existing tags) to the open document or all open documents in a given app, accordingly, without having to interact with Finder. I have tested this with Preview, Word, Excel, Powerpoint, and TextEdit. It works even with unsaved, in-progress Office documents (although the documents must have been saved at least once so that they have a file location). It should work with most any item that has a file location and can be tagged in MacOS.
If you choose to tag all your open documents (in a given app), and you’re working with file types other than Preview, TextEdit, Word, Powerpoint, and Excel, and probably others, the macro must bring each open document to the front briefly in order to get the filepath using Chris Stone’s script. I personally can live with this, because the overall time save is worth it.
This macro prompts the user to ADD or REPLACE tags. The macro will throw up a self-dismissing dialog briefly showing you the tags set to the file. Obviously, that element can be omitted if you don’t care about that.
The two Applescripts which do the heavy lifting are depicted below:
The above script will gather the filepaths of all open documents in the specific apps listed instantly. Chris Stone’s script, below, can get the path of the frontmost document in just about any app; it’s the fallback for when I’m not working with the apps featured in the script above. However, it only works for the frontmost document, not for multiple windows. I rely on Keyboard Maestro to iterate through the open windows, bringing each to the front momentarily, in order to get the filepath via Stone’s script.
I can’t get a properly sized image of the macro into this post. You can see it better in the cross-post at the Keyboard Maestro forum here.