Basic, elementary help for using ASOBJC to extract a value from a structured list?

I am trying to use ASOBJC to extract a string value from a AS list.

The list is structured thus:
property t_DataNamestoFMPnames : {{dataName:“flights_climbed”, fmpName:“AH_FlightsClimbed”}, ¬
{dataName:“apple_exercise_time”, fmpName:“AH_ExerciseTime”}, ¬
{dataName:“active_energy”, fmpName:“AH_Active Energy”}, ¬

My desire is to use the “dataName” as an argument and the FMPName as the result.

My struggle is two-fold: I do not fully understand KVC; I cannot figure out how to tell valueForKey what to look up.

The starting point is using Shane’s book example (5-4) on ValueForKey. The key argument I am attempting is a AS string object (metricName).

(its valueForKey:metricName inList:t_DataNamestoFMPnames)

I expect (hope for) the returned list to be just the entry with the matching argument.

So, any help you can offer is greatly appreciated. I suppose I can revert to a sequential lookup but a faster way would be better.

Thanks.
Karl

First, you need an NSArray. Then something like this:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

property t_DataNamestoFMPnames : {{dataName:"flights_climbed", fmpName:"AH_FlightsClimbed"}, ¬
	{dataName:"apple_exercise_time", fmpName:"AH_ExerciseTime"}, ¬
	{dataName:"active_energy", fmpName:"AH_Active Energy"}}

set theData to current application's NSArray's arrayWithArray:t_DataNamestoFMPnames
set theResult to (theData's valueForKey:"dataName") as list

For filtering or matching, you need to use an NSPredicate:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

property t_DataNamestoFMPnames : {{dataName:"flights_climbed", fmpName:"AH_FlightsClimbed"}, ¬
	{dataName:"apple_exercise_time", fmpName:"AH_ExerciseTime"}, ¬
	{dataName:"active_energy", fmpName:"AH_Active Energy"}}

set theData to current application's NSArray's arrayWithArray:t_DataNamestoFMPnames
set thePred to current application's NSPredicate's predicateWithFormat:"dataName = %@" argumentArray:{"apple_exercise_time"}
set newList to (theData's filteredArrayUsingPredicate:thePred) as list

Absolutely perfect, Shane. Thank you ever so much! The rest of this is TMI but designed to express my gratitude for your measured approach to teaching these techniques.

I should have, but did not, connect that I needed an NSArray for the table. I have been developing a script to parse JSON data and of course I learned how and why to to use the NSArray. Just got so overwhelmed trying to understand the valueForKey that I missed the larger picture.

Due to your coaching in your book, I have also learned to seek answers in the Xcode developer documentation. Sadly, it never occurred to me to read the sections on Files and Sorting for a table lookup.

I have been scripting for about 10 years - nothing especially tough. I got interested in the idea of using ASObjc, bought your book and have been attempting to learn the new language. I must say that this is a difficult but rewarding adventure. I have now been able to parse JSON data, get text from a pdf (blazingly fast compared to my former process of using pdfPen), and now my current project - updating a Filemaker database from JSON data - replacing doing the same thing with csv files and Numbers.

Best to you,
Karl

1 Like

Bought AppleScriptObjC Explored? Everyday AppleScriptObjC? Where? I’d kill for a copy, no matter how outdated. After all, AppleScript itself is arguably outdated, but we’re stuck with it. :kissing_cat:

Finding a new home for the book has proved more complex than I hoped, but if anyone is really keen, they can contact me via email.