mdfind "AND" / "NOT"

I am writing a little script that take advantage of the mdfind command.
I would like to search for a text “AAAA” into a given field such as:

mdfind “kMDItemAuthors == ‘Luciano’”

but i don’t want that the same text is into kMDItemTextContent

Is there any direct way to have something like:
mdfind (“kMDItemAuthors == ‘Luciano’” NOT “kMDItemTextContent == ‘* Luciano*’”)

I guess with AppleScript I can get list of the 2 separate searches and from there identify the unique files …

Thanks !


Something some this, I am not sure with quotes syntax:

mdfind ‘kMDItemAuthors == ‘Luciano’ && ! kMDItemTextContent == ‘* Luciano*’"’

Is not working. I tried with all possible combinations of quotes.
This works:
mdfind ‘kMDItemAuthors == *Luciano’ → 2
This is also works:
mdfind ‘kMDItemTextContent == *Luciano’ → 99
But the two together, such as:
mdfind (‘kMDItemAuthors == *Luciano’ &&! ‘kMDItemTextContent == *Luciano’)

doesn’t work … with this error.
-bash: syntax error near unexpected token `‘kMDItemAuthors == *Luciano’ ’

copy this as is, and try again:

mdfind “kMDItemAuthors == ‘Luciano’ && ! kMDItemTextContent == ‘Luciano’”

mdfind “kMDItemAuthors == ‘Luciano’ && kMDItemTextContent != ‘Luciano’”

Excellent !!!
Thanks a lot !

You should include [c] if you want the search case-insensitive. Here it is using Metadata Lib:

use script "Metadata Lib" version "2.0.0"

set theFolder to choose folder
set theMetadata to perform search in folders {theFolder} predicate string "kMDItemAuthors ==[c] %@ AND kMDItemTextContent !=[c] %@" search arguments {"*Luciano*", "*Luciano*"}

Using the lib will be a tad quicker, too.

You are right. I forgot the option to use Metadata Lib.
Thanks !


is there any quick way to exclude one attributes (kMDItemTextContent) from the search?
Let’s say I want to search for the word “the” in my files and find those files where it is present either the name, or author or in the comments etc… but not in the kMDItemTextContent, otherwise all files will be positive.

This is the script I am using so far. It works, but I would like to include in the search all fields (=attributes) but not kMDItemTextContent.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use script "Metadata Lib"

# the clipboard contains the string I want to find
set MyDoi to the clipboard
set theFolder to alias "iMac_HD:Users:ldicroce:Desktop:for_ERC:"

set {a1, a2, b1, b2, c2} to {"kMDItemDescription", MyDoi, "kMDItemFinderComment", MyDoi, ".pdf"}

set theMetadata to perform search in folders {theFolder} predicate string "%K CONTAINS[c] %@ OR %K ==[c] %@ AND kMDItemFSName ENDSWITH[c] %@" search arguments {a1, a2, b1, b2, c2}

You have to specify each attribute you want to search separately.

Once you start mixing ANDs and ORs, you really should start including parentheses, or you may not get what you expect.

You are right. I have now included the parentheses.