Applescript Records获得未知财产

时间:2016-02-02 14:19:50

标签: applescript

我希望能够获得朋友的电话号码

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.

2 个答案:

答案 0 :(得分:3)

当记录仅包含用户定义的键时,您可以使用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

答案 1 :(得分:1)

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