1. What is appscript?
Objc-appscript is a high-level Apple event bridge that allows you to control scriptable Mac OS X applications from Objective-C programs.
For example, to get the value of the first paragraph of the topmost document in TextEdit:
TEApplication *textedit = [[TEApplication alloc]
initWithName: @"TextEdit.app"];
NSString *result = [[[[[[textedit documents] at: 1]
paragraphs] at: 1] get] send];
This is equivalent to the AppleScript statement:
tell application "TextEdit"
get paragraph 1 of document 1
end tell
Appscript builds upon the Apple Event Manager and Foundation APIs to provide:
- a high-level RPC mechanism for sending commands to applications via Apple events
- a mechanism for converting data between common Foundation and Apple event types
- a simple programmatic query language for identifying one or more objects in an application's object model
- a glue code generator for representing these object model "references" in human-readable form based on application-defined terminology
- a clean, object oriented-like syntax for ease of use.
Appscript requires Mac OS X 10.3 or later.
"Hello World!"
The following program uses appscript to create a new "Hello World!" document in TextEdit:
#import <Foundation/Foundation.h>
#import "TEGlue.h"
int main(int argc, char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
TEApplication *textedit;
TEMakeCommand *makeCmd;
textedit = [[TEApplication alloc]
initWithName: @"TextEdit.app"];
makeCmd = [[[textedit make] new_: [TEConstant document]]
withProperties: [NSDictionary dictionaryWithObjectsAndKeys:
@"Hello World!\n", [TEConstant text], nil]];
[makeCmd send];
[textedit release];
[pool release];
return 0;
}