Script bridge and SBElementArrays filtering using NSPredicate and FourCharCodes

I am experimenting with Scripting Bridge for the first time, but have SBElementArray into SBElementArray filtering problem according to NSPredicate, which contains the enumeration constant FourCharCode as a criterion.

I wrote a trivial program to identify the source of the library in the iTunes user library using -filteredArrayUsingPredicate: to filter SBElementArray all iTunes sources. I expected to return SBElementArray , which, when computed, will create an array of one element, namely the source of the library. Instead, when I call -get on the returned SBElementArray , I return an empty array.

Surprisingly, if you change the order and instead call -get in SBElementArray all sources to get a specific NSArray , and call -filteredArrayUsingPredicate: in this array with the same predicate as before, I get the desired result. I don't think this is supposed to be necessary, however, and I had a successful SBElementArray filtering using other NSPredicates (e.g. @"name=='Library'" works fine).

The following is a snippet of code. iTunesESrcLibrary is a iTunesESrcLibrary constant defined in the header file created by Scripting Bridge. ( iTunesESrcLibrary = 'kLib' ). I am running 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); 

The output I get is the following:

 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)>" ) 
+4
source share
1 answer

I think I figured it out. It seems you can't just use the FourCharCode integer value directly in the NSPredicate , which you are going to use to filter SBElementArray .

By chance, I found that instead of:

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

you need to use:

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

Using this second form, I can filter out the SBElementArray source list as expected. However, this new predicate cannot be used to filter NSArray , although this array is only an evaluated form of SBElementArray ! Here you should return to the %u version.

Welt:
Honestly, this sucks, and it looks like something like the Scripting Bridge should deal, so I don't need to; I did not need to know what NSAppleEventDescriptor . And although it is reasonable that not all predicates that work with NSArray should work with SBElementArray , the reverse should not be unnecessarily confusing.

+5
source

Source: https://habr.com/ru/post/1334031/


All Articles