Search code examples
macoscocoaapplescriptcocoa-scripting

How to pass an arbitrary AppleScript Record to Cocoa in a scriptable app?


I have a Cocoa application with an AppleScript dictionary described in a .sdef XML file. All of the AppleScript classes, commands, etc. defined in the sdef are working property.

Except for my "submit form" command. The "submit form" command is my only command attempting to pass a parameter that is an arbitrary hashtable of information from AppleScript to Cocoa. I assume this should be done by passing an AppleScript record which will be automatically converted to an NSDictionary on the Cocoa side.

tell application "Fluidium"
    tell selected tab of browser window 1
        submit form with name "foo" with values {bar:"baz"}
    end tell
end tell

The "with values" parameter is the record -> NSDictionary parameter i am having trouble with. Note that the keys of the record/dictionary cannot be known/defined in advance. They are arbitrary.

Here is the definition of this command in my sdef XML:

<command name="submit form" code="FuSSSbmt" description="...">
    <direct-parameter type="specifier" optional="yes" description="..."/>
    <parameter type="text" name="with name" code="Name" optional="yes" description="...">
        <cocoa key="name"/>
    </parameter>
    <parameter type="record" name="with values" code="Vals" optional="yes" description="...">
        <cocoa key="values"/>
    </parameter>
</command>

And I have a "tab" object which responds to this command in the sdef:

<class name="tab" code="fTab" description="A browser tab.">
    ...
    <responds-to command="submit form">
        <cocoa method="handleSubmitFormCommand:"/>
    </responds-to>

and Cocoa:

- (id)handleSubmitFormCommand:(NSScriptCommand *)cmd {
    ...
}

The "tab" object correctly responds to all the other AppleScript commands I have defined. The "tab" object also responds to the "submit form" command if I don't send the optional "with values" param. So I know I have the basics setup correctly. The only problem seems to be the arbitrary record->NSDictionary param.

When I execute the AppleScript above in AppleScript Editor.app, I get this error on the Cocoa side:

+[NSDictionary scriptingRecordWithDescriptor:]: unrecognized selector sent to class 0x7fff707c6048

and this one on the AppleScript side:

error "Fluidium got an error: selected tab of browser window 1 doesn’t understand the submit form message." number -1708 from selected tab of browser window 1

Can anyone tell me what I'm missing? For reference the entire application is open source on GitHub:

http://github.com/itod/fluidium


Solution

  • Right -- NSDictionaries and AppleScript records seem like they would mix, but they don't actually (NSDictionaries use object keys -- say strings) where AppleScript records use four letter character codes (thanks to their AppleEvent/Classic Mac OS heritage).

    See this thread on Apple's AppleScript Implementer's mailing list

    So, what you actually need to do, in your case, is to unpack the AppleScript record you have and translate it into your NSDictionary. You could write the code all by yourself, but it's complicated and dives deep into the AE manager.

    However, this work has actually been done for you in some underlaying code for appscript/appscript-objc (appscript is an library for Python and Ruby and Objective-C that lets you communicate with AppleScriptable applications without actually having to use AppleScript. appscript-objc could be used where you would use Cocoa Scripting, but has less of the sucky limitations of that technology.)

    The code is available on sourceforge. I submitted a patch a few weeks ago to the author so you could build JUST the underlaying foundation for appscript-objc, which is all you need in this case: all you need to do is pack and unpack Applescript/AppleEvent records.

    For other googlers, there's another way to do this, that's not using appscript: ToxicAppleEvents. There's a method in there that translates dictionaries into Apple Event Records.