Skip to content

Instantly share code, notes, and snippets.

@dagronf
Created February 10, 2019 00:53
Show Gist options
  • Save dagronf/3d03094a7ee79c1f91607f4c365fba91 to your computer and use it in GitHub Desktop.
Save dagronf/3d03094a7ee79c1f91607f4c365fba91 to your computer and use it in GitHub Desktop.
How to use Spotlight searches in Swift (basic example)
let query = MDQueryCreate(kCFAllocatorDefault, "kMDItemContentType = com.apple.application-bundle" as CFString, nil, nil)
MDQueryExecute(query, CFOptionFlags(kMDQuerySynchronous.rawValue))
let count = MDQueryGetResultCount(query);
for i in 0 ..< count {
let rawPtr = MDQueryGetResultAtIndex(query, i)
let item = Unmanaged<MDItem>.fromOpaque(rawPtr!).takeUnretainedValue()
if let path = MDItemCopyAttribute(item, kMDItemPath) as? String {
print(path)
}
}
@dagronf
Copy link
Author

dagronf commented Feb 10, 2019

// let query = MDQueryCreate(kCFAllocatorDefault, "kMDItemFSLabel = 6" as CFString, nil, nil)
// let query = MDQueryCreate(kCFAllocatorDefault, "kMDItemContentType = com.apple.application-bundle" as CFString, nil, nil)
// let query = MDQueryCreate(kCFAllocatorDefault, "kMDItemUserTags == 'cat' && kMDItemUserTags == 'dog'" as CFString, nil, nil)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment