For some reason this only works with “Red” in the field for argumentArray and the variable doesn’t work. Can someone help me understand why?
Works:
set theArray to current application's NSArray's arrayWithArray:apiRes
set thePred to current application's NSPredicate's predicateWithFormat:"colorName = %@" argumentArray:{"Red"}
set resultAPI2 to (theArray's filteredArrayUsingPredicate:thePred)'s firstObject() as record
Doesn’t work:
Set colorSearch to "Red"
set theArray to current application's NSArray's arrayWithArray:apiRes
set thePred to current application's NSPredicate's predicateWithFormat:"colorName = %@" argumentArray:{colorSearch}
set resultAPI2 to (theArray's filteredArrayUsingPredicate:thePred)'s firstObject() as record
Both versions seem to work for me. Version one:
use framework "Foundation"
use scripting additions
set apiRes to {{colorName:"Red", colorID:"aa"}, {colorName:"Blue", colorID:"bb"}, {colorName:"Red", colorID:"cc"}}
set theArray to current application's NSArray's arrayWithArray:apiRes
set thePred to current application's NSPredicate's predicateWithFormat:"colorName = %@" argumentArray:{"Red"}
set resultAPI2 to (theArray's filteredArrayUsingPredicate:thePred)'s firstObject() as record --> {colorName:"Red", colorID:"aa"}
Version two:
use framework "Foundation"
use scripting additions
set apiRes to {{colorName:"Red", colorID:"aa"}, {colorName:"Blue", colorID:"bb"}, {colorName:"Red", colorID:"cc"}}
set colorSearch to "Red"
set theArray to current application's NSArray's arrayWithArray:apiRes
set thePred to current application's NSPredicate's predicateWithFormat:"colorName = %@" argumentArray:{colorSearch}
set resultAPI2 to (theArray's filteredArrayUsingPredicate:thePred)'s firstObject() as record --> {colorName:"Red", colorID:"aa"}
Did I miss something?
No you did not. Odd I can run yours and can’t in my code. Going deeper…thank you
Make sure your importing Foundation
Try changing the predicate from:
set thePred to current application’s NSPredicate’s predicateWithFormat:“colorName = %@” argumentArray:{colorSearch}
To:
set thePred to current application’s NSPredicate’s predicateWithFormat_(“colorName = %@”, colorSearch)
You don’t need the argumentArray