Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [258]

By Root 1495 0
of trampoline.scpt, along with appropriate parameter values. We know how to execute a script as a whole in Cocoa using NSAppleScript, but how do we call a particular handler, and how do we pass it parameters? The solution is the very same mechanism by which scriptability of user handlers in an applet is implemented (see "Applet Scriptability," earlier in this chapter)—the 'ascr\psbr' Apple event. We must form this Apple event more or less manually, but it isn't hard to do. Here's how. Observe that for the sake of brevity and clarity I've issued a #define equating "Desc" to the lengthy term "NSAppleEventDescriptor" which would otherwise clutter up the code.

#define Desc NSAppleEventDescriptor

- (Desc*) callSub:(NSString*)handler params:(Desc*)firstParam, ... {

Desc* list = [Desc listDescriptor];

int i=0; va_list ppp; va_start(ppp, firstParam);

Desc* aParam = firstParam;

while(aParam) {

[list insertDescriptor:aParam atIndex:++i];

aParam = va_arg(ppp, Desc*);

}

Desc* h = [Desc descriptorWithString:[handler lowercaseString]];

Desc* ae = [Desc appleEventWithEventClass:'ascr' eventID:'psbr'

targetDescriptor:[Desc nullDescriptor]

returnID:kAutoGenerateReturnID

transactionID:kAnyTransactionID];

[ae setParamDescriptor:h forKeyword:'snam'];

[ae setParamDescriptor:list forKeyword:keyDirectObject];

return ae;

}

- (void) setCell: (int) n toString: (NSString*) s {

Desc* dn = [Desc descriptorWithInt32:n];

Desc* ds = [Desc descriptorWithString:s];

[trampoline executeAppleEvent

:

[self callSub:@"setCell" params:dn, ds, nil] error:nil];

}

- (id) getCell: (int) n{

Desc* dn = [Desc descriptorWithInt32:n];

return [[trampoline executeAppleEvent:

[self callSub:@"getCell" params:dn, nil] error:nil] stringValue];

}

- (NSString*) searchText {

return [self getCell: 1];

}

- (void) setSearchText: (NSString*) t {

[self setCell: 1 toString: t];

}

//...and so on...

The callSub: routine is a general utility for helping to form the 'ascr\psbr' Apple event. It takes as its parameters the name of the AppleScript handler you want to call, followed by a nil-terminated series of AppleScript parameter values. Each AppleScript parameter value must have previously been embedded into an NSAppleEventDescriptor of the proper type, but this is not usually difficult to do; the setCell: and getCell: methods exemplify the technique, and show how to call callSub:.

Part IV. Appendixes


Part IV contains this book's appendixes.

The appendixes are:

Appendix A, The AppleScript Experience

Appendix B, Apple Events Without AppleScript

Appendix C, Tools and Resources

Appendix A. The AppleScript Experience


This appendix illustrates informally the process of developing AppleScript code. The idea is to convey to the beginner some sense of what it's like to work with AppleScript, and to present some typical stages in the development of an AppleScript-based solution. My approach is to demonstrate by example, letting you look over my shoulder as I tackle a genuine problem in my real life. The procedures and thought processes exemplified here are quite typical of my own approach to writing AppleScript code, and probably that of many other experienced users as well; as such, the neophyte may benefit by witnessing them. Besides, if you've never programmed with AppleScript before, you're probably curious about what you're getting yourself into.

Think of this appendix, then, as a nonprogrammer's introduction to the art of AppleScript development. It's the art that's important here. The particular problem I'll solve in this chapter will probably have no relevance whatsoever to your own life. But the way I approach the problem, the things I do and experience as I work on it, contain useful lessons. At the end we'll extract some general principles on how to approach a task with AppleScript.

The Problem


I have just completed, working in Adobe FrameMaker , the manuscript for a book about AppleScript. This manuscript is now to be submitted to my publisher. My publisher can take submissions in FrameMaker, which is what the production

Return Main Page Previous Page Next Page

®Online Book Reader