Re: Adding simple AppleScript commands to a Cocoa application
Re: Adding simple AppleScript commands to a Cocoa application
- Subject: Re: Adding simple AppleScript commands to a Cocoa application
- From: Matt Neuburg <email@hidden>
- Date: Tue, 18 Apr 2006 16:40:59 -0700
- Thread-topic: Adding simple AppleScript commands to a Cocoa application
On Sun, 16 Apr 2006 13:46:15 +0000, "Robert Nolet"
<email@hidden> said:
>Hi,
>
>I've been trying to write a very simple cocoa application which implements
>several simple AppleScript commands with no arguments/parameters. Basically,
>i want to:
>
>tell app "MyApp"
> do something
>end tell
>
>such that a
>
>-(void) doSomething;
>
>message is sent to MyController (subclass of NSObject, instantiated with
>Interface Builder). So far I have been able to realize this by setting
>MyController as the application delegate in Interface Builder, and
>subclassing NSScriptCommand as following:
>
>@implementation MyCommand
>
>- (id) performDefaultImplementation {
> [(MyController *)[[NSApplication sharedApplication] delegate] doSomething];
> return nil;
>}
>
>@end
>
>My MyApp.sdef file contains the following:
>
><?xml version="1.0" encoding="UTF-8"?>
><!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">
><dictionary title="MyApp Terminology">
> <suite name="MyApp Suite" code="MyAp" description="MyApp scripting
>commands.">
> <command name="do something" code="MyApDoSo"
> description="Do something.">
> <cocoa class="MyCommand"/>
> </command>
> </suite>
></dictionary>
>
>This all works exactly as I want it to, however I think that creating a
>subclass of NSScriptCommand and then accessing my MyController object by
>going through the application delegate is an incredibly sloppy and
>round-about way of achieving this. Also, if I were to add more commands, I
>would have to create a new class for each command I want to implement. So I
>am looking for a more elegant way of linking methods in my MyController
>class to AppleScript commands.
>
>I'm sure it should be possible, but all the code examples I have found so
>far work by calling methods in data objects which are passed as parameters
>to the AppleScript command, and not for no-parameter commands.
The last chapter of the second edition of my AppleScript book contains an
example of implementing a "no-parameter command" (verb-first dispatch). It
sounds to me, though, as if you're doing it right. The implementation of
Cocoa Scripting and its curious variety of key-value coding "slams" (as I
put in my book) "into your existing application like a side wind, ignoring
your architecture and surprising your code". In other words Cocoa Scripting
tries to militate against your nice object architecture. Basically the
problem you've cited is only the tip of the iceberg. Your solution sounds
quite elegant considering everything. m.
--
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide - Second Edition!
<http://www.amazon.com/gp/product/0596102119>
_______________________________________________
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