spotlight/metadata searches are confusing me

In order to set up an asynchronous query looking for a specific application, I have a predicate of:

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K LIKE[cd] %@ AND %K = %@",
                                                         @"kMDItemDisplayName", name,
                                                         @"kMDItemContentType", UTTypeApplicationBundle.identifier];

I tested it with standalone code, and this did what I wanted -- finding applications with the given name. But recently, it seems to have stopped working.

That query should be the equivalent of

mdfind 'kMDItemDisplayName LIKE[cd] "Safari" AND kMDItemContentType == "com.apple.application-bundle"'

but that gives me

Failed to create query for 'kMDItemDisplayName LIKE[cd] "Safari" AND kMDItemContentType == "com.apple.application-bundle"'.

If I drop the compound, and just do

% mdfind 'kMDItemDisplayName LIKE[cd] "Safari"'

then I get no output:

% mdfind 'kMDItemDisplayName LIKE[cd] "Safari"'
% 

And yet clearly I do have Safari installed on my system.

What am I doing wrong, or missing? Anyone?

My #1 Spotlight query tip is to use mdls to display the attributes of the item before running your test queries. For example:

% mdls /Applications/Safari.app 
…
kMDItemDisplayName                 = "Safari"
…

So let’s try searching for that:

% mdfind 'kMDItemDisplayName == "Safari"'
/Applications/Safari.app
…

Cool.

The reason why your searches are failing is that you’re using LIKE. That’s not supported in the Spotlight query syntax. Rather, you use == with wildcards. For example:

% mdfind 'kMDItemDisplayName == "Safar*"'
/Applications/Safari.app
/System/Library/CoreServices/UAUPlugins/SafariUserAccountUpdater.bundle
…

I don’t know if this is documented anyway in the current docs, but my go-to reference for this is the File Metadata Query Expression Syntax sections of the File Metadata Search Programming Guide in the documentation archive.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

The reason I was using LIKE is because I couldn't do a case-insensitive query without. I still can't, despite the documenation. For example, it uses kMDItemAuthors ==[c] "Steve" and just below that explains that the [c] means it is case-insensitive. And yet:

sef% mdfind 'kMDItemDisplayName == "Safari"'
/Applications/Safari.app
/Users/Shared/Previously Relocated Items 1/Security/System/Library/AssetsV2/com_apple_MobileAsset_MacSoftwareUpdate/f7b05c91052116c046919f72de2c03a86cabcf3e.asset/AssetData/payloadv2/ecc_data/System/Library/Templates/Data/Applications/Safari.app
/Library/Application Support/Apple/Safari
/Library/Apple/System/Library/Assistant/Plugins/Safari.assistantBundle/Contents/MacOS/Safari
/Users/Shared/Previously Relocated Items/Security/Developer/SDKs/MacOSX10.6.sdk/System/Library/PrivateFrameworks/Safari.framework/Versions/A/Safari
/Users/Shared/Previously Relocated Items/Security/Developer/SDKs/MacOSX10.7.sdk/System/Library/PrivateFrameworks/Safari.framework/Versions/A/Safari
/Users/sef/Library/Application Support/SyncService/LastSync Data/Safari
sef% mdfind 'kMDItemDisplayName ==[c] "Safari"'
sef% 

(Also, I thought that LIKE used the SQL wildcards.)

No ideas from anyone? I filed FB11963917 as a result. :(

spotlight/metadata searches are confusing me
 
 
Q