pythonappleeventssourceforge-appscript

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


I use appscript and aemreceive in a py2app bundled Python 2.7 application to accept incoming Apple Events and return my app's output as Apple Events to the external application which communicates with my app.

This works quite well, with one exception: When I return a dictionary, the keys are surrounded by pipe characters where I need keys without these characters.

Example: Where I would like to send this response to the external app:

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

instead my app sends this:

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

The event handler has been installed using this code:

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

where "get_property" is the name of a function which gets invoked once the external app requests location info for an item. This function returns a dictionary:

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

I have learned that surrounding keys with pipes is necessary if you want to use Apple Events reserved words like "folder" as your app's keys or if your key uses spaces or non-ASCII characters. So when I remove the space from "has description" or rename the "folder" key to "myfolder", my app returns a dictionary without surrounding the keys by pipes.

Yet, the external app which communicates with my app requires my app to use keys with empty spaces as well as the "folder" key.

Any ideas?

Thanks a lot in advance.


Solution

  • Figured out how it works.

    In my sdef file the properties are defined:

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

    Instead of returning the keys as strings like this:

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

    I now simply return the four character codes and let aem do the heavy lifting:

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