Copy a Random Selection of Files to a Different Folder

I have a folder that contains 2000+ PDFs, some of which are tagged with 'listen'. I want to randomly select 20 of these PDFs with the tag 'listen', copy them and paste them into my Downloads folder.

Having rummaged through the forums, I found the attached macro that displays a list of files in a text box, but this is as far as I got.

I know how to use the 'Copy a file' action, but how can I randomly select 20 files tagged with 'listen' out of a folder of 2000+ files?

Get PDFs.kmmacros (3.8 KB)

Hey Ross,

Since you're starting out with the shell anyway using mdfind I'd keep it there and do everything in Bash if you have the skills.

If you need to stick with Keyboard Maestro:

Get the line count of your found files to a variable. (KM filter action.)

Replace 2000 in the Rand equation below with the variable.

Keyboard Maestro Actions.kmactions (1.3 KB)

Put it in a repeat loop that appends the random number to a variable (IF it does not already contain it).

Keep going until the line number equals 20.

Once that's done you can iterate through the random number list and grab the lines corresponding to those numbers from your file path list.

There's no way to get a specific line from a variable with Keyboard Maestro native actions without looping, so I'd use the shell and sed like so:

Sed ⇢ Print a Specific LIne Example v1.00.kmmacros (5.9 KB)