Is There a Way to Search Macros for a File Path?

Howdy folks, lately I've been doing a little bit of Dropbox housecleaning. I keep all of my AppleScript, JavaScript and HTML files in there that way KM can reference them from any of my computers.

However, I am seeing that I have a lot that I believe have fallen into disuse for one reason or another. But before deleting them, I would like to verify that I don't have them embedded in a macro somewhere. But searching in the KM Editor for the file path, or even the file name does not show me any results unless the search scope is part of the action's title.

So is there a way to search so that the results will include file path fields and there the file name I'm trying to find?

-Chris

Aren't all the macros stored in a single file? (I can't remember its path/name.) Did you consider opening that file in a TextEditor and searching that file for the names you want to find?

1 Like

There is, it's in Keyboard Maestro's Application Support directory, and I did consider it... but I'd rather avoid that if possible in case I were to screw up the file.

I know I can simply duplicate the file and open/search through the duplicate, and I'm not opposed to doing that (since I already have one other macro that does something similar), but I figured I would reach out and see if there was a simpler solution first.

Well, it's binary encoded by default, I think, so that could be tricky, but you can make a plain text XML copy with plutil at the command line.

Expand disclosure triangle to view JS Source
(() => {
    "use strict";

    const main = () => {
        const fpOut = "~/Desktop/km.xml";

        return (
            Object.assign(
                Application.currentApplication(), {
                    includeStandardAdditions: true
                }
            ).doShellScript(
                `plutil -convert xml1 -o ${fpOut} "${kmPlistPath()}"`
            ),
            `XML text copy at ${fpOut}`
        );
    };


    // --------------------- GENERIC ---------------------

    // applicationSupportPath :: () -> String
    const applicationSupportPath = () => {
        const uw = ObjC.unwrap;

        return uw(
            uw($.NSFileManager.defaultManager
                .URLsForDirectoryInDomains(
                    $.NSApplicationSupportDirectory,
                    $.NSUserDomainMask
                )
            )[0].path
        );
    };

    // kmPlistPath :: () -> IO FilePath
    const kmPlistPath = () => {
        const
            kmMacros = [
                "/Keyboard Maestro/",
                "Keyboard Maestro Macros.plist"
            ].join("");

        return `${applicationSupportPath()}${kmMacros}`;
    };

    return main();
})();

The XML copy should be easy enough to search through if you've got a text file editor that can handle large files.

(If that turns out to be unwieldy, you might be able to sketch an XQuery for searching through particular types of value in the plist.)

@ComplexPoint thanks for the tips... I'm pretty ignorant on JavaScript... but I built a macro (I got bored haha) that simply duplicates the plist file and then opens it in BBEdit allowing me to search it for the file name... (more on that below), but how would your route be better/more efficient? Ultimately I would love to be able to have the search run in the background that way it requires no input from me (other than triggering the macro perhaps).

This is what I have right now. The TL;DR version is it duplicates the KM plist file, opens it in BBEdit, and searches it for the file name supplied at the beginning (either from the Finder's selection or the prompt for file action). It then asks what you want to do with the file, and then tags it or trashes it accordingly.

It works great for me, I just went through a large folder of AppleScript files in about a minute and found every one that is and is not in my KM plist file and tagged them accordingly. Let me know how you think this could be improved because if it proves useful enough (and safe enough) I would be willing to share it in the Macro Library category.

14)Keyboard Maestro- Search Plist File for Embedded File.kmmacros (48 KB)

Macro screenshot (click to expand/collapse)

Hey Chris,

Huh?

I get results with either file name or full or partial path...

Here's one example:

-ccs

Thanks Chris, that's so weird... in the past (even the other day when I was searching for some specific embedded files) there were some instances where a search wasn't showing them even though I know they were embedded in some macros.

This morning I searched for others and it did show me the ones I searched for this morning. So maybe I was simply messing something up with my search. :person_shrugging:t2:

Either way... I think I'll stick with the macro I built (I already rebuilt it twice and am happy with the final result), but this is good to know too.

-Chris

EDIT: Hey @Sleepy I went ahead and built a macro to search the plist file (well, a duplicate of it anyway haha), and here's a screen recording of it in action. I may post it to the forum later on.

Screen recording of the macro
Search KM plist file for embedded files and tag

1 Like

Whenever I'm able to help a wizard, even just a little bit, my day is starting off really well.

1 Like

I don't consider myself a wizard by any means but I appreciate your thoughtfulness and being able to help get your day off to a good start! :grin:

Just an update… I ended up building a macro to search the KM plist file for any files you select in Finder and tag them if they’re found.

Chris (@ccstone) recommended me linking to my posted macro here so here’s the link:

Find Which Files Are Used in Your Macros and Tag Them Accordingly