Creating Programs for Mac OS X. Part 3: Apple Script

    In this part, I’ll talk about another interesting application development tool for Mac OS X - the scripting language Apple Script.

    Apple Script was designed to be used by end users, not programmers, and to allow them to control the applications and documents with which they work. For example, using Apple Script, you can open a photo in an image editor, crop to the desired size, write a link to the photo in a text file, etc.

    Unlike how the user interacts with the application through the GUI, for example, imprints information in the text fields of the application for working with databases, AppleScript works in a completely different way, the script uses the internal object model of the application, thereby entering values ​​into the database itself. This means that while the script is running, the application does not even have to be shown. Naturally, this model of work requires that your application be written with support for Apple Script.

    Each application that Apple Script understands publishes supported commands in the Apple Event dictionary, which is used to determine valid commands.

    A language very similar to natural

    In the Radio-T podcast, the host bobuk said that a script written in the Apple Script language is read like plain English text. In principle, he is right, because this is one of the fundamental features of Apple Script.

    The Apple Script engine combines verbs and nouns to complete actions. For example, to print a document, a page from a document or a specific fragment, instead of calling the functions printPage, printDocument, printRange, we take the verb print and add the necessary noun:
    print page 1
    print document 2
    print pages 1 thru 5 of document 2

    To work with a specific application, the word “tell” is used.
    tell application "iTunes"
    end tell

    The previous expression can be written in one line
    tell application "Microsoft Word" to quit

    For events from the Core Suite (activate, open, re-open, close, print, exit), you can use the following instructions:
    quit application "iPhoto"

    Constructs can be nested:
    tell application “QuarkXPress”
    tell document 1
    tell page 2
    tell text box 1
    set word 5 to “Apple”
    end tell
    end tell
    end tell
    end tell tell

    You can use hierarchy as follows:
    pixel 7 of row 3 of TIFF image "my bitmap"

    But Apple's capabilities are not limited to managing other applications. With it, you can create a normal Cocoa application with a graphical interface, etc. For example, the following code:
    set pix to 72
    set answer to text returned of (display dialog "Enter in the number of inches" default answer "1")
    display dialog answer & "in =" & (answer * pix) & "px"

    when executed, it will show a dialog box asking you to enter the number of inches, then this value is converted to pixels and the result is shown in the next window.

    You can also save the script so that you can use it later as a full-fledged application. The trigger event handler must be inside the following structure:
    on run
    - handler code
    end run

    In principle, you don’t need to write it, then when you start the script processing will start from the first line in the file.

    If you drop a couple of other files on the file with the script, the following handler will be used at startup:
    on open theItems
    - do something with these same theItems
    end open

    The scripting tool is located in / Applications / AppleScript. is used as an editor / interpreter.

    To open the Apple Event dictionary, click File => Open Dictionary in the Script Editor menu, a window with a list of applications will open,
    select the one you need (for example iTunes) and click OK . A window opens with a description of the commands for the application we need.

    And now we ’ll write a simple Cocoa application with which you can control iTunes.
    Open Xcode, File => New Project, Cocoa Application => in iTunes, write iTunes_Controller. Add a new Objective-C class “controller”. In the controller.h file we write
    #import <Cocoa / Cocoa.h>

    interface controller: NSObject {
    - (IBAction) nextClick: (id) sender;
    - (IBAction) prevClick: (id) sender;
    - (IBAction) pauseClick: (id) sender;
    - (IBAction) playClick: (id) sender;
    - (void) executeAppleScript: (NSString *) sctript;

    In principle, there is nothing secret-military in this code. Just write a class with which we will control iTunes. * Click functions are handlers for clicking on the corresponding buttons on the form. And executeAppleScript: (NSString *) sctript is a function that will execute the script in the script parameter.

    Now click on MainMenu.nib and get into InterfaceBuilder. Add a new NSObject to the MainMenu.nib window and assign the controller class to it:

    Then we will place four buttons on the form with the inscriptions "next", "play", "pause", "previous" and connect them to the corresponding. handlers from the controller class:

    Save everything and return to Xcode. In the controller.m file, write the following:
    - (void) executeAppleScript: (NSString *) script {
    try {
    NSAppleScript * ascript = [[NSAppleScript alloc] initWithSource: script];
    [ascript executeAndReturnError: nil];
    [ascript release];
    catch (NSException * e) {
    NSLog (@ "exception:% @", e);
    - (void) nextClick: (id) sender {
    [self executeAppleScript: @ "tell application \" iTunes \ "\ n next track \ n end tell"];
    - (void) prevClick: (id) sender {
    [self executeAppleScript: @ "tell application \" iTunes \ "\ n previous track \ n end tell"];
    - (void) playClick: (id) sender {
    [self executeAppleScript: @ "tell application \" iTunes \ "\ n play \ n end tell"];
    - (void) pauseClick: (id) sender {
    [self executeAppleScript: @ "tell application \" iTunes \ "\ n pause \ n end tell"];

    In principle, there is nothing complicated in this code either. The only interesting thing is the NSAppleScript class, with the help of which we execute the script. Read more about NSAppleScript here .

    Basically, I think that this is enough to get a general idea of ​​Apple Script and to write something on it.

    PS: what else would be interesting to read in terms of creating applications?
    PPS: noticed that my articles copy-paste on other resources (thank God at least leave a link to the hub) I certainly don’t mind, but if you copy and paste, please be so kind as to report it to me (by e-mail, mail, or write in kamenty)

    Also popular now: