I'd like to create an If Then Else action whose condition is based on the file extension(s) of the file(s) selected in Finder. For example...
If the file or files selected all have the .txt extension, then _____.
If the file or files selected all have the .mp4 extension, then _____.
Otherwise, cancel macro.
And so on. I'm justing using TXT and MP4 as examples here, but it can be other extensions. I hope this makes sense. Any help would be greatly appreciated!
Here's one possible solution that works as a demo of a full-bore solution. I'm pretty sure it's not the best or shortest answer, but it is pretty quick and functional :).
Macros are always disabled when imported into the Keyboard Maestro Editor.
The user must ensure the macro is enabled.
The user must also ensure the macro's parent macro-group is enabled.
System information
macOS 14.4.1
Keyboard Maestro v11.0.2
The macro counts the number of files/folders in the selection, and saves the selection to a variable. A loop then tries all the extensions you specify. For each one, it uses a shell command (grep-c) to count the number of entries whose name ends in that loop's extension.
If the total of the grep matches the total of lines in the selection, then only that file type is selected. A second case statement then lets you process each extension differently, as it sounds like that's what you needed to be able to do. You could work with the variable saved earlier, or the `%FinderSelections% token again, to do what you needed to the files.
As noted, this is more of a demo than a complete solution, but hopefully it's enough to help get you started.
As part of the "friendly competition" to solve this as concisely as possible, I propose the following macro. I'm using some shell ideas here. I made at least one assumption that may not be true - I assumed that none of the files had spaces in their names. If this assumption is not true, I think I could modify this macro to account for that. Think of this as a proof-of-concept rather than a fully tested idea.
Creative use of uniq, that's awesome! But I think you can greatly simplify the shell command—as well as protect spaces and extra dots—by using awk in a different manner. First, save the %FinderSelections% to a variable, call it local_theFiles or whatever.
That will handle spaces in filenames, as well as multiple dots in a filename. The rest of the macro would then be unchanged.
How it works: awk -F. says that the text should be split into fields on the '.' character, and then the command to run on each line of input, {print $NF}, prints the last field (i.e. the extension) on each line. Then it's run through uniq, and you're done.
Thanks for the compliment. (Both of them!) I know about the -F argument in awk. I just decided I was too tired to refine my macro at this hour to make it work better. And I really didn't need to finish my macro since your macro does the job well.
Just want to say thanks to everyone who has chimed in here so far! I'm traveling today and tomorrow, but I look forward to digging into these solutions once I'm back home. You all are the best.
Nice! Riffing off your clever use of uniq, can I suggest a tweak? Try changing the shell script action to
grep -E -o '\..*?$' | uniq
...and prefixing each of your "Switch/Case" tests with a period to match. Since you're then only looking for extensions you can ditch your "no space" assumption as well. It will, of course, have problems if you've got exentionless files (actually, paths with no periods in them) in the Finder selection -- but that's rare these days.