Applescript按字符串名称属性

时间:2016-06-05 03:33:52

标签: properties applescript

在AppleScript中。让我们说我有一个名为Title的属性的记录。

我们说我在文本中设置了一个变量" Title&#34 ;;我可以使用该变量来获取属性Title的值吗?基本上,有没有办法做这样的事情:

set result to property named "Title" of myRecord

而不是:

set result to Title of myRecord

2 个答案:

答案 0 :(得分:1)

我找到了答案。我也意识到我没有问正确的问题。我试图获取的值来自属性列表项。

这是我学到的知识,以及如何实现这一目标:

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

答案 1 :(得分:0)

您可以尝试尝试...错误方法:

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."

然后解析错误(现在是它的文本)。它取决于aRecord的复杂性,如果它是嵌套列表或记录的记录;解析会非常复杂。玩得开心:)