Applescript 记录变得未知 属性

Applescript Records get unknown property

我希望能够获取好友的phone个数

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}

我得到的错误是 Can’t set number to {person of details}. Access not allowed.

当记录仅包含用户定义的键时,您可以使用 run script 动态创建脚本并 运行 它。我在变量 keyString 周围添加了管道,因此它被迫使用用户定义的键而不是枚举键(删除它们以使用枚举键)。

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

AppleScript 记录的键就像变量一样,它们是在编译时创建的,您不能更改它们,并且它们不能与字符串进行比较。

您可以获取密钥并将其与二级评估进行比较,但这相当昂贵。

解决方法是创建记录列表

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