Limiting operations on selected files in the Finder to specific file types

It can be convenient to select files in the Finder and then apply some process to only those files which are of specified file types.

For example, an open folder might contain various kinds of graphic files (e.g. .jpg, .gif, .png) plus .pdf and .txt files. Instead of selecting each of the graphic files, it is quicker to select all the files (⌘-A) and then run a macro that will apply operations only to the specified file types (.jpg, .gif, .png)—any other files being ignored.

I made some macros for audio file conversion. Here is the start of one of them (to convert uncompressed audio files to AAC), showing how I filter out files of an irrelevant type.

This method can of course be used for any sort of file type. It is assumed that the files’ names all use file extenders (e.g. .wav, .aiff).

First, the variable local files is set to the %FinderSelections%[1] token, so that it now contains a list of the paths of all the selected files.

Then there is a search and replace action which removes any paths that do not contain the relevant file extenders: .aif, .aiff, .flac, .wav. This leaves us with a list in which all irrelevant files have been removed. Subsequent actions in the macro will be applied only to the files that we are interested in.

The regex in this example is:
(?m)^(?!.*\.(aif|aiff|flac|wav)).*\n

The required file types in this example are: aif|aiff|flac|wav (indicating various types of uncompressed audio files). Just change that list of alternatives for your use case (e.g. for graphic files, you might want something like jpg|tiff|gif|png).

The final action in the example checks that the local files list is not empty—i.e. that the Finder selection did contain at least one file that is relevant to the macro. If that is the case, the macro will execute the actions that are enclosed by the if action.

One of those actions will typically be to apply actions to each of the paths (local file) in the file list (local files). We can use the for each action for this, e.g.

Should the list of paths (local files) be empty, you might want to have an action that displays an error message. This would be put in the otherwise section of the if action. Example:


  1. token:FinderSelection [Keyboard Maestro Wiki] ↩︎

1 Like