Search code examples
cocoapropertiesapplescriptosx-snow-leopard

Cannot access cocoa property via applescript


I have two string properties setup in my cocoa application's app delegate which are KVC compliant. My .sdef exposes them to applescript as text read-write properties.

When I use applescript to do the following, my properties are returned correctly:

tell application "iKeepFit"
activate
properties
end tell

What gets weird is that when I try to access the property by its name, applescript editor will popup a dialog saying my app had an error. Yet xcode debug output shows no messages. Previously when my .sdef was wrong then xcode would show errors if I ran the script calling my app when it is attached to the debugger.

E.g.
propertyname <- error "Can’t get propertyname." number -1728 from «class dact»
set propertyname "value" <- error "Can’t set propertyname." number -10006 from «class dact»

Is there something that would typically cause this behavior?

I've gone through two cocoa books, an applescript book and lots of sample code and apple documentation, but they seem to all implement applescript support differently. What I've got so far is the closest to working code from trying them all out. Here's my .sdef file:

<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">

<!-- use XInclude to include the standard suite 
<xi:include href="file:///System/Library/ScriptingDefinitions/CocoaStandard.sdef" xpointer="xpointer(/dictionary/suite)"/>
-->
<suite name="iKeepFit Scripting" code="iKft"
    description="Commands and classes for iKeepFit Scripting">

    <class name="application" code="capp" description="The application class.">
        <!-- the following names the Objective-C class where Cocoa will look for
         the property accessor methods for the properties we define for
         this AppleScript class. -->
        <cocoa class="MyApplication"/>

        <!-- the 'done' property. -->
        <property name="completedactivity" code="dAct" description="An activity that was completed" type="text" access="rw">
            <cocoa key="doneActivity"/>
        </property>

        <!-- the 'cancel' property. -->
        <property name="canceledactivity" code="cAct" description="An activity that was canceled" type="text" access="rw">
            <cocoa key="cancelActivity"/>
        </property>

        <responds-to name="completed">
            <cocoa method="handleCompletedActivityScriptCommand:"/>
        </responds-to>
    </class>      
</suite>

I commented out the standard suite stuff because surprisingly it's broken and produces the following error: .sdef warning for argument 'FileType' of command 'save' in suite 'Standard Suite': 'saveable file format' is not a valid type name.

Please help. Thanks.


Solution

  • I was able to find a solution. In Snow Leopard, Applescript apparently doesn't know what properties are defined in the application unless it's surrounded by a using terms from block.

    using terms from application "iKeepFit"  
        set doneActivity to "test"         
    end using terms from
    

    And for some reason the <class> element needed an id="blah" attribute in it - the only reason I've found that is by looking at the output of an .sdef editing program. The real frustrating part is that this isn't in any of the examples or documents I've read.