Search code examples
applescriptcocoa-scripting

Cocoa Scripting: "whose clause" cannot access certain properties


I am working on making my application scriptable. I struggle with the "whose" filter clause.

I want to make this work, but while name can be used, country can not:

tell application "myapp"
    get every city whose name is "Berlin" -- works
    get every city whose country is "Germany" -- error -1700 (Can’t make country into type specifier)
end tell

The relevant parts of the sdef look like this:

<class name="application" code="capp">
    <cocoa class="NSApplication"/>
    <element type="city">
        <cocoa key="allCities"/>
        <accessor style="index"/>
    </element>
    <class name="city" code="Citi" plural="cities">
        <cocoa class="ScriptableCity"/>
        <property name="name" code="pnam" type="text" access="r">
            <cocoa key="name"/>
        </property>
        <property name="country" code="Ctry" type="text" access="r">
            <cocoa key="country"/>
        </property>
    </class>

What must I do to make country work with "whose" as well? Apparently, the "whose" clause wants a type specifier, not a property name, but I can't make sense of this.

I have implemented indicesOfObjectsByEvaluatingObjectSpecifier:, but that only gets called for name, not for country.


Solution

  • Oh, I had it all wrong. My program code is fine. The issue is caused by the fact that I also have a class named country. So AppleScript, looking at the outmost scope for the identifier first, finds the class country and tries to use that for the comparison. Had the error message included the word "class", this would have been easier to detect, probably.

    There are now two solutions:

    1. Rename the property in the Sdef so that it does not clash with the class name any more, e.g. to country name.

    2. Use of it in order to change the scope for the lookup of the identifier, like this:

      get every city whose country of it is "Germany"
      

    It is also important to make sure that if the same property name is used in several classes, they all use the same 4-char type code. Otherwise this problem can surface as well.