Hi all. I am new to keyboard maestro and before I spend loads of time working this out I wondered if someone could give me a view on whether the following is possible and how to go about doing it? I want to be able to point keyboard maestro at a directory, copy the content of all files (and all files in all the subfolders) to the clipboard so that I can paste the contents of all the files, in one go.
That’s a rather generalised sort of request.
Broadly speaking, if you have a wide variety of files not all of them are necessarily going to have copy-paste-able content. Different applications can accept pasting in different ways, etc. What would you want to do when dealing with multiple content types? Does everything have to be pasted at once, or is sequential pasting acceptable - you mention pasting in one go, but does that matter if it is automated? There’s also nothing defined about the ordering - that could get interesting if you have specific ordering needs.
There are a lot of unknowns in your brief outline, so it is difficult to give you precise guidance.
That aside, to give you at least some starting point, let’s take what is arguably the simplest case of every file containing just plain text, and capturing those in a default order to be pasted all at once. That’s actually something we could have a shell script do for us (no Keyboard Maestro required), and then we could wrap Keyboard Maestro around the script to give us some options like specifying which directory branch to search, what to file match against, and even additional things like whether to also include the file name. i.e. using Keyboard Maestro to give us a bit of a UI for the script.
The macro could start by prompting for a folder selection, then prompting for a file pattern match (defaulting to *.*
matches files with any file extension - you could change this to just *
for those without extensions too, or to *.log
for just log files), and a checkbox on whether to include the file name and relative path in the output (so you know which file produced which set of output).
With those details captured, we can build a shell script that finds all matching files, appends the content of each file to a variable (adding a new line for good measure so the last line of a file does not run into the first line of the next file), and optionally prefixing the relative file path.
We could take the resulting variable content and pass it back to Keyboard Maestro to put on the clipboard, but it can also be done easily in the script with pbcopy
.
Lastly we could optionally add something to Indicate the macro has completed, after all it could take a few seconds to run on a large branch. I quite like subtle audio feedback for that sort of thing, and so you might end up with a macro something like this.
Hopefully, that’s enough to give you some ideas of how you might want to tackle this, and the sorts of things you might want to think about in terms of filtering, ordering, and combining files.