Properties Applescript通过属性的字符串名称获取属性的值
在AppleScript中。假设我有一个记录,它有一个名为Title的属性 假设我为文本“Title”设置了一个变量;我可以使用该变量获取属性标题的值吗?基本上,有没有办法做到这一点:Properties Applescript通过属性的字符串名称获取属性的值,properties,applescript,Properties,Applescript,在AppleScript中。假设我有一个记录,它有一个名为Title的属性 假设我为文本“Title”设置了一个变量;我可以使用该变量获取属性标题的值吗?基本上,有没有办法做到这一点: set result to property named "Title" of myRecord 而不是: set result to Title of myRecord 你可以试试。。。关于错误方法: set aRecord to {title:"hello world", author:"who's wh
set result to property named "Title" of myRecord
而不是:
set result to Title of myRecord
你可以试试。。。关于错误方法:
set aRecord to {title:"hello world", author:"who's who"}
try
aRecord as Unicode text
on error error_message
set err to error_message
end try
err
-- "Can’t make {title:\"hello world\", author:\"who's who\"} into type Unicode text."
然后解析err(现在是文本)。它取决于记录的复杂性,如果它是嵌套列表的记录,或记录;解析将非常复杂。玩得开心:)我找到了答案。我也意识到我问的问题不对。我试图获取的值来自属性列表项 以下是我学到的,以及如何做到这一点:
use framework "Foundation"
set _plist to ...
set _objcPlist to GetAppleScriptObjectAsObjcObject(_plist)
set _value to GetObjcPropertyValueByName("MyProperty", item 1 of _objcPlist)
on GetAppleScriptObjectAsObjcObject(asObject)
set a to current application
set cClass to class of asObject
if (cClass is record) then
return a's NSDictionary's dictionaryWithDictionary:asObject
else if (cClass is list) then
return a's NSArray's arrayWithArray:asObject
else
error "Unexpected Class Type"
end if
end GetAppleScriptObjectAsObjcObject
on GetObjcPropertyValueByName(propertyName, objcItem)
return (objcItem's valueForKey:propertyName) as text
end GetObjcPropertyValueByName
你试过了吗?你为什么要这样做?你的任务是什么?记录是一组属性(又名“struct”),而不是任意键值集合(又名dictionary/hash/map)。有很多方法可以乱搞唱片的内部,但它们都是邪恶的黑客,有着各种各样令人讨厌的陷阱。使用正确的数据结构做这项工作要好得多。@CraigSmith-是的,我当然试过了。@foo-当我发布问题时,我不知道如何使用正确的术语。更多信息请参见我的答案。