Avoiding a very large MDFIND shell command

I am using a KM macro to run the Shell script (MDFIND command) as shown. It sends a list of found folder names (based on the 2 criterion) to the variable aa_Folder_List. This works at the moment.

There are an unknown number of folders to be found (requiring usually about 10-30 criterion).

Rather than putting lots more OR statements into the MDFIND shell command (with $KMVAR_aa-fN variables - where N typically will = a number from 10-30 or more) is there an easier way to do this than predefining & initialising 30 vars in KM and having a very large MDFIND shell command?

(I am reading in the criterion from a text file in a FOR EACH loop in KM - so how would I assign each string to different variable names [aa-fN] in this loop if there is no other way?)

Just build the whole command in a single Keyboard Maestro variable.
Assuming it is only the kMDItemDisplayName’s, you do something like this:

  • Set variable Query to “”
  • For Each Display Name
    • if Query is not empty
      • Set Variable Names to "%Variable%Query% || "
    • Set Variable Names to “%Variable%Query%(kMDItemDisplayName == '*%Variable%Display Name%*'c)”
  • Set variable Query to “( (kMDItemUserTags == 'Gray*'cdw) && (%Variable%Query%))”
  • Execute Shell Script: mdfind -onlyin “/Users/username/Documents/AAA/” “$KMVAR_Query”

How exactly you do the “For Each Display Name” I’ll leave up to you - probably a while forever loop that you break out of or some such.

1 Like

Thanks, this works very well.