Kamin Pallaghy
Kamin Pallaghy

Reputation: 158

Applescript Records get unknown property

I want to be able to get the phone number of a friends

set people to {"Kristi", "Mark", "John"}
set details to {kristi:"1247 532 523", Mark:"0411 123 979", John:"0225 552 446"}

set person to choose from list people
set number to {person of details}

The error i am getting is Can’t set number to {person of details}. Access not allowed.

Upvotes: 0

Views: 218

Answers (2)

vadian
vadian

Reputation: 285260

The keys of AppleScript's records are like variables, they are created at compile time, you cannot change them and they are not comparable with strings.

You could get and compare the keys with second level evaluation but this is quite expensive.

A workaround is to create a list of records

set people to {"Kristi", "Mark", "John"}
set details to {{name:"Kristi", phone:"1247 532 523"}, {name:"Mark", phone:"0411 123 979"}, {name:"John", phone:"0225 552 446"}}

set person to choose from list people
if person is false then return
set person to item 1 of person

set phone to missing value
repeat with anItem in details
    if name of anItem is person then
        set phone to anItem's phone
        exit repeat
    end if
end repeat

Upvotes: 1

dj bazzie wazzie
dj bazzie wazzie

Reputation: 3542

When a record contains only user defined keys you can use run script to create a script on the fly and run it. I have added pipes around the variable keyString so it is forced to use a user defined key instead of an enumerated key (remove them to use enumerated keys).

set people to {"Kristi", "Mark", "John"}
set details to {kristi:"1247 532 523", Mark:"0411 123 979", John:"0225 552 446"}

itemFromRecordByString(details, "Kristi")

on itemFromRecordByString(theRecord, keyString)
    set plainScript to "on run argv
return |" & keyString & "|  of (item 1 of  argv)
end run"

    run script plainScript with parameters {theRecord}
end itemFromRecordByString

Upvotes: 3

Related Questions