Re: Handle AppleScript command using a delegate
Re: Handle AppleScript command using a delegate
- Subject: Re: Handle AppleScript command using a delegate
- From: Dustin Voss <email@hidden>
- Date: Mon, 28 Feb 2005 14:57:12 -0800
On 28 Feb, 2005, at 11:07 AM, Matt Budd (Madentec) wrote:
Hello all,
I've been trying to make a sample cocoa application AppleScriptable to see how much work it would be for one of our main applications. The article at http://developer.apple.com/cocoa/applescriptforapps.html is very helpful to see how you can get properties implemented using a delegate for your NSApplication. The problem is that the article doesn't explain how to do commands the same way.
For example, I can right now perform the following AppleScript:
tell application "Scriptease"
set mytestproperty to "test"
end tell
and it works correctly. However, when I try this AppleScript in the script editor
tell application "Scriptease"
mytestcommand
end tell
it complains saying NSCannotCreateScriptCommandError
As Tommy Nordgren points out, this is because you aren't including a required parameter. But you have other problems, too.
Here is a portion of my .sdef for the project:
<suite name="Scriptease" code="mlst"
description="Commands and classes for Scriptease">
<!-- Exposed Classes -->
<classes>
<class name="application" code="capp" description="The application class"
inherits="NSCoreSuite.NSApplication">
If you are altering the
application class, you must replace AppleScript's default class with your own. You must also keep the
application class in the standard suite, and not move it to your app-specific suite. Copy
/Developer/Examples/Scripting Definitions/NSCoreSuite.sdef to your own project and edit the
application class defined in there. Use that file when you generate your .scriptSuite file, .scriptDefinition file, and aete resource.
Also, a class in AppleScript is more like a protocol than a Cocoa class. AppleScript classes don't really inherit; inheritance is mostly just a way to clean up your dictionary. Since you are replacing the default
application class, you aren't really inheriting anything, so remove the
inherits attribute.
<cocoa class="NSApplication"/>
<properties>
<property name="mytestproperty" code="tprp" type="string"
description="My Test Property">
<cocoa method="ASTestProperty"/>
Here, the
method attribute should take a KVC key as a value.
</property>
</properties>
<responds-to-commands>
<responds-to name="mytestcommand">
<cocoa method="ASTestCommand"/>
Here, in your case, the
method attribute should be set to "", because you will have to handle the command via an
NSScriptCommand sub-class instead of via a method on the direct object. See below.
</responds-to>
</responds-to-commands>
</class>
</classes>
<!-- Exposed Commands -->
<commands>
<command name="mytestcommand" code="mlsttcmd" description="My Test Command">
<result type="boolean" description="My Test Retval"/>
<parameter name="mytestparam" code="tprm"
description="My Test Param" type="boolean"/>
</command>
</commands>
This
commands block can rightly go in your own suite, unlike the redefined
application class. But you will need to add a
cocoa tag that has the name of an
NSScriptCommand sub-class.
</suite>
And I've implemented this method in the delegate for my NSApplication (the same class that I did the methods for the properties (as in that article)):
- (void)ASTestCommand: (NSScriptCommand *)poCommand
{
NSLog(@"here"); //I never get here?!?
}
Has anyone got this to work and can see what I'm doing wrong? Most of the stuff that I've seen on implementing AppleScript commands involve subclassing NSScriptCommand, but that seems to be a bit overkill if you end up having a ton of AS-commands, wouldn't it? Any info would be appreciated...
Firstly, the idea of AppleScript is to have just a few generic commands, that act on a ton of properties and elements.
Sketch.app, for example, does not implement any commands at all, but instead only uses the default implementations of
set,
make,
move, etc. That said, you can end up with a lot of commands, and in that case, yes, you may have a lot of
NSScriptCommand subclasses. But each sub-class really only needs to implement one method, that being
performDefaultImplementation.
The two ways Cocoa Scripting can handle a command are 1) in a sub-class of
NSScriptCommand, or 2) in the instance of the direct object (e.g., for a command like
close connection 1, in the first instance of
MyConnection). Since your test command does not have a direct object, you have to use a sub-class of
NSScriptCommand.
What you really need to do is, read through
http://cocoadev.com/?HowToSupportAppleScript, and especially the tech-note at
http://developer.apple.com/technotes/tn2002/tn2106.html, where you can learn how to approach the problem of scriptability.
Hope this helps.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden