AppleScript:获取对象或类的所有属性的列表 [英] AppleScript: Getting a list of all properties of an object or class

查看:38
本文介绍了AppleScript:获取对象或类的所有属性的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了存储对象的值以供外部(AS 外部)访问,我需要能够获取该对象的每个属性,然后我会尝试将其强制转换为文本并将其存储在某个地方.

In order to store an object's values for external (outside AS) access, I need to be able to get every property of that object, and then I'd try to coerce it to text and store it somehwere.

如何获取对象拥有的属性列表.例如,我可以这样写:

How do I get the list of properties that an object holds. As an example, I can write this:

tell me
  get properties
end tell

这适用于 script 对象.

但对于许多其他对象,我只会收到诸如描述符类型不匹配"之类的错误,如下所示:

But for many other objects, I simply get an error such as "descripter type mismatch", like here:

tell application "iTunes"
  get properties of file track 1
end tell

现在,我知道优秀的Script Debugger 可以做到这一点(它可以显示任何对象的整个属性集),所以它也应该可以在书面 AppleScript 中实现.这其中有什么秘诀?

Now, I know the excellent Script Debugger can do it (it can show any object's entire set of properties), so it should be possible in written AppleScript as well. What's the secret to this?

推荐答案

Script Debugger 的作者 Mark Alldritt 非常友好地向我解释了秘密".

Mark Alldritt, the author of Script Debugger, was so kind to explain the "secret" to me.

Script Debugger 使用一些特殊的 AppleScript API 函数,包括 OSAGetPropertyNames(),来获取这些信息.

Script Debugger uses some special AppleScript API functions, including OSAGetPropertyNames(), to get to this information.

因此,如果我用 C 语言编写包装器,我也可能会做到这一点.

Hence, if I write a wrapper in, for instance, C, I can probably get to this, too.

更新

Cocoa Scripting API 有一个专门的类(NSScriptSuiteRegistryNSScriptClassDescription) - 框架通过读取应用程序的脚本定义 (.sdef) 文件来构建此信息.有了这个,所有可用的类及其属性都可以很容易地学习.

The Cocoa Scripting API has a dedicated classes for this (NSScriptSuiteRegistry and NSScriptClassDescription) - the framework builds this information from reading an app's scripting definition (.sdef) file. With that, all the available class and their properties can be learned quite easily.

这篇关于AppleScript:获取对象或类的所有属性的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆