脚本桥和使用NSPredicate和FourCharCodes过滤SBElementArrays

时间:2011-01-03 05:35:10

标签: objective-c cocoa itunes scripting-bridge

我第一次尝试使用Scripting Bridge,但是根据包含FourCharCode枚举常量的NSPredicate作为标准,遇到了过滤SBElementArray的问题。

我写了一个简单的程序来识别用户iTunes资料库中的“库”源,方法是使用-filteredArrayUsingPredicate:过滤所有iTunes资源的SBElementArray。我期望得到一个SBElementArray,在评估时,它会生成一个元素的数组,即库源。相反,当我在返回的-get上调用SBElementArray时,我会返回一个空数组。

令人困惑的是,如果更改订单,而是在所有来源的-get上调用SBElementArray以获得具体的NSArray,并在此数组上调用-filteredArrayUsingPredicate:像以前一样谓词,我确实得到了理想的结果。我不相信这应该是必要的,并且我已成功使用其他NSPredicates过滤SBElementArray(例如@"name=='Library'"正常工作)。

下面是代码段。 iTunesESrcLibrary是由Scripting Bridge生成的头文件中定义的FourCharCode常量。 (iTunesESrcLibrary = 'kLib')。我正在跑10.6.5。

iTunesApplication* iTunes = [[SBApplication alloc] initWithBundleIdentifier:@"com.apple.iTunes"];   

NSPredicate* libraryPredicate = [NSPredicate predicateWithFormat:@"kind == %u", iTunesESrcLibrary];

SBElementArray* allSources_Attempt1 = [iTunes sources];
SBElementArray* allLibrarySources_Attempt1 = (SBElementArray*)[allSources_Attempt1 filteredArrayUsingPredicate:libraryPredicate];

NSLog(@"Attempt 1: %@", allLibrarySources_Attempt1);
NSLog(@"Attempt 1 (evaluated): %@", [allLibrarySources_Attempt1 get]);


NSArray* allSources_Attempt2 = [[iTunes sources] get];
NSArray* allLibrarySources_Attempt2 = [allSources_Attempt2 filteredArrayUsingPredicate:libraryPredicate];

NSLog(@"Attempt 2: %@", allLibrarySources_Attempt2);

我得到的输出如下:

Attempt 1: <SBElementArray @0x3091010: ITunesSource whose 'cmpd'{ 'relo':'=   ', 'obj1':'obj '{ 'want':'prop', 'from':'exmn'($$), 'form':'prop', 'seld':'pKnd' }, 'obj2':1800169826 } of application "iTunes" (88827)>
Attempt 1 (evaluated): (
)
Attempt 2: (
"<ITunesSource @0x3091f10: ITunesSource id 65 of application \"iTunes\" (88827)>"
)

1 个答案:

答案 0 :(得分:5)

我想我已经明白了。您似乎不能简单地在NSPredicate中直接使用FourCharCode的整数值来过滤SBElementArray

偶然的机会,我找到了而不是:

[NSPredicate predicateWithFormat:@"kind == %u", iTunesESrcLibrary]

你需要使用:

[NSPredicate predicateWithFormat:@"kind == %@", [NSAppleEventDescriptor descriptorWithTypeCode: iTunesESrcLibrary]]

使用第二种形式,我可以按预期过滤SBElementArray来源列表。但是,这个新谓词不能用于过滤NSArray,即使此数组只是SBElementArray的评估形式!在这里,您必须切换回%u版本。

咆哮:
坦率地说,这很糟糕,而且看起来像Scripting Bridge应该处理的事情,所以我不必这样做;我不应该知道NSAppleEventDescriptor是什么。虽然并非所有与NSArray一起使用的谓词都应该与SBElementArray一起使用是合理的,但反过来情况并非如此,并且它不必要地令人困惑。