Configuring NSPredicateEditor(RowTemplate) for Spotlight metadata queriesI'

China☆狼群 提交于 2019-12-07 22:28:46

问题


I'm trying to configure an NSPredicateEditor (in Interface Builder) to edit the predicate for an NSMetadataQuery.

As a first step, I'm trying to configure an NSPredicateEditorRowTemplate to accept key path(s) for the left-side expression, trying a single keyPath (kMDItemTextContent) to get started.

I can't figure out how to get all the pieces into IB. I've selected the row template, and set "Left Exprs" to "Key Paths" in the IB Attributes Inspector. But, using Apple's PhotoSearch example as a model, it appears that I should enter a user-readable attribute name (say, "Content") here; I can't figure out how to bind it to "kMDItemTextContent".

I've dissected the (correctly-configured) NIB in PhotoSearch(*), and inside it there is an NSKeyPathExpression specifying a metadata attribute attached to an NSPopUpButton/NSPopUpButtonCell.

I can't figure out where to navigate in IB to find the NSPopUpButton, and I'm not sure what I'd do to bind it to an NSExpression.

Any help appreciated.

(*) In case you're wondering, I got inside the NIB by converting it to a XIB, confirming that it still builds correctly, then examining it with BBEdit.


回答1:


I've found that working with NSPredicateEditor and friends in Interface Builder is an exceedingly tedious task. For that reason, I do all of my row template configuration in code.

For your situation, it doesn't sound like you need a custom row template subclass, so you could probably just do:

#define NSPERT NSPredicateEditorRowTemplate
NSPERT * template = [[NSPERT alloc] initWithLeftExpressions:[NSArray arrayWithObject:[NSExpression expressionForKeyPath:kMDItemTextContent]] 
                               rightExpressionAttributeType:NSStringAttributeType
                                                   modifier:NSDirectPredicateModifier
                                                  operators:[NSArray arrayWithObject:
                                                             [NSNumber numberWithUnsignedInteger:NSContainsPredicateOperatorType]]
                                                    options:(NSCaseInsensitivePredicateOption|NSDiacriticInsensitivePredicateOption)];

Once you've got the template, simply add it to the predicateEditor:

NSMutableArray * templates = [[myPredicateEditor rowTemplates] mutableCopy];
[templates addObject:template];
[template release];
[myPredicateEditor setRowTemplates:templates];
[templates release];

As for translating the "kMDItemTextContent", if it doesn't happen automatically (and I think it might), you could use the NSPredicateEditor localization options to display a different name.



来源:https://stackoverflow.com/questions/4708256/configuring-nspredicateeditorrowtemplate-for-spotlight-metadata-queriesi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!