为什么我无法在AppleScript中迭代我的记录?

时间:2017-04-25 14:25:42

标签: applescript record

鉴于AppleScript中的以下记录:

set usergroup to {user1:{name:"Darth Vader", role:"leader"}, user2:{name:"Yoda", role:"instructor"}}

显示我可以的特定用户:

set trainer to get user1 of usergroup
display dialog (name of trainer)

但如果我想显示我尝试的所有用户:

repeat with x from 1 to (length of usergroup)
    set member to ("user" & x) as item
    display dialog member
end repeat

我得到user1user2,但如果我尝试:

repeat with x from 1 to (length of usergroup)
    set member to ("user" & x) as item
    display dialog (get name of member) as text
end repeat

我收到错误,所以我尝试了:

repeat with x from 1 to (length of usergroup)
    set member to (get name of ("user" & x)) as item
    display dialog member as text
end repeat

为什么我无法获得name个用户?

1 个答案:

答案 0 :(得分:1)

因为member变量的类是字符串,而不是键。

对于包含简单用户列表的记录:使用someRecord as list

set usergroup to {user1:{name:"Darth Vader", role:"leader"}, user2:{name:"Yoda", role:"instructor"}}
repeat with member in (usergroup as list) -- the member variable contains a record
    display dialog (name of member)
    --display dialog (role of member)
end repeat

或者,您可以使用run script命令评估字符串("用户"& x)作为键

set usergroup to {user1:{name:"Darth Vader", role:"leader"}, user2:{name:"Yoda", role:"instructor"}}
repeat with x from 1 to (length of usergroup)
    set member to ("user" & x)
    set thisMemberName to run script "on run {thisRecord}" & linefeed & "name of " & member & " of thisRecord" & linefeed & "end run" with parameters {usergroup}
    display dialog thisMemberName
end repeat