响应 Python 中的 Apple Events 而无需在字典中的键周围使用竖线字符

respond to Apple Events in Python without using pipe characters around keys in dictionary

我在捆绑的 py2app 中使用 appscript 和 aemreceive Python 2.7 应用程序来接受传入的 Apple 事件,return 我的应用程序作为 Apple 事件输出到与我的应用程序通信的外部应用程序。

这很好用,有一个例外:当我 return 一本字典时,键被管道字符包围,而我需要没有这些字符的键。

示例:我想将此响应发送到外部应用程序的位置:

{has description:true, folder:file "travellerHD:Users:andre:Desktop:MyNewFile"}

相反,我的应用发送了这个:

{|has description|:true, |folder|:file "travellerHD:Users:andre:Desktop:MyNewFile"}

已使用此代码安装事件处理程序:

aemreceive.installeventhandler(get_property, "coregetd",
                               ('----', 'request_string',
                                aemreceive.kae.typeAERecord),
                              )

其中 "get_property" 是一个函数的名称,一旦外部应用程序请求某个项目的位置信息,该函数就会被调用。这个函数 return 是一个字典:

return {'has description': asset.has_description,
        'folder': mactypes.File(asset.folder)}

我了解到,如果您想使用像 "folder" 这样的 Apple Events 保留字作为您应用程序的键,或者如果您的键使用 space 或非 ASCII 字符,则必须用管道包围键.因此,当我从 "has description" 中删除 space 或将 "folder" 键重命名为 "myfolder" 时,我的应用程序 return 是一个字典,没有用管道包围键。

然而,与我的应用程序通信的外部应用程序要求我的应用程序使用空 space 键以及 "folder" 键。

有什么想法吗?

非常感谢。

弄清楚它是如何工作的。

在我的 sdef 文件中定义了属性:

<property name="has description" code="ashd" type="boolean" description="Whether the library has an XML description.">
</property>
<property name="folder" code="asfd" type="file" description="Directory in which the library files exist.">
</property>

而不是像这样 return 将键作为字符串:

return {'has description': asset.has_description,
        'folder': mactypes.File(asset.folder)}

我现在简单地 return 四个字符代码,让 aem 完成繁重的工作:

return {aem.AEType('ashd'): asset.has_description,
        aem.AEType('asfd'): mactypes.File(asset.folder)}