Polle Polle - 1 month ago 15
AppleScript Question

Choose from list of records

How do I from this list of records, get a 'choose from list' showing only 'Fornavn2' and 'Efternavn2', but setting var to corresponding 'ElevID2'???

I know this is not the correct syntax, but it goes to show what I want:

set var to ElevID2 of varEleveriklasse to choose from list (Fornavn2 of varEleveriklasse & " " & Efternavn2 of varEleveriklasse)


List:

set varEleveriklasse to {{Fornavn2:"Kenneth", Efternavn2:"Oddersen", ElevID2:"23442"}, {Fornavn2:"Thomas", Efternavn2:"Johansen", ElevID2:"23452"}, {Fornavn2:"Johan", Efternavn2:"Thomasen", ElevID2:"76590"}, {Fornavn2:"Charlotte", Efternavn2:"Frandsen", ElevID2:"78569"}, {Fornavn2:"Mathilde", Efternavn2:"Charlottesen", ElevID2:"64569"}}

Answer

It's impossible to populate the list without a repeat loop.

A smart solution to get the index of the chosen item is to include it in the list.

set varEleveriklasse to {{Fornavn2:"Kenneth", Efternavn2:"Oddersen", ElevID2:"23442"}, ¬
    {Fornavn2:"Thomas", Efternavn2:"Johansen", ElevID2:"23452"}, ¬
    {Fornavn2:"Johan", Efternavn2:"Thomasen", ElevID2:"76590"}, ¬
    {Fornavn2:"Charlotte", Efternavn2:"Frandsen", ElevID2:"78569"}, ¬
    {Fornavn2:"Mathilde", Efternavn2:"Charlottesen", ElevID2:"64569"}}

set fullNameList to {}
set indexCounter to 1
repeat with anItem in varEleveriklasse
    set end of fullNameList to (indexCounter as text) & space & anItem's Fornavn2 & space & anItem's Efternavn2
    set indexCounter to indexCounter + 1
end repeat

set chosenItem to choose from list fullNameList
if chosenItem is false then return
set chosenItem to item 1 of chosenItem // chosenItem is a list or boolean false

set listIndex to word 1 of chosenItem as integer
set ElevID to ElevID2 of item listIndex of varEleveriklasse
display dialog ElevID