Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [243]

By Root 1378 0
proceeds.

Start up Xcode. Choose File → New Project and select AppleScript Automator Action; name the project LAME Encode. When the project window appears, choose File → New File to create an AppleScript Text File called ui.applescript to be added to the project. The reason is that we wish to write some code that will interact with the action's interface, and this code must not go into main.applescript, the only script supplied by default.

Now let's create our action's interface. In the project window, double-click main.nib to start up Interface Builder. The View window displays the pane in which the Action's interface is to appear. Start by removing the placeholder text ("UI elements go here"). Apple's interface guidelines for Automator actions specify that interface elements should be Small size rather than Regular, and that the NSView should have 10-pixel margins. They also suggest using space economically, and in particular they ask that a popup menu should be preferred to radio buttons.

Figure 27-6 shows our action's interface. The top row contains the interface elements the user can set. The popup menu contains the names of the most important presets: its items are "insane," "extreme," "standard," "medium," a menu item separator, then "studio," "cd," "hifi," "tape," and "mw-us." The second row contains a text field that we'll use to show the user the results of the current settings in the top row. We won't implement VBR bitrates, so the bitrate text field will be disabled unless the "cbr" checkbox is checked. The height of the NSView has been reduced as much as possible; vertical space is at a premium when the user is constructing a workflow in Automator, so we don't want to waste any.

Figure 27-6. The action's interface

Just as in the AppleScript Studio tutorial, earlier in this chapter, we now give our interface elements AppleScript names and arrange for notification when the user changes one of them. The names are namePopup, fastSwitch, cbrSwitch, cbrText, and example. The notifications must all be sent to ui.applescript; they are the popup menu's action, the clicked of the two checkboxes, and the bitrate text field's action and changed, along with the NSView's update parameters and awake from nib.

We can use Cocoa bindings to tie our interface items to parameter values that our main.applescript code will receive. That's why the NSObjectController called "Parameters" is present in the main window. The use of bindings isn't compulsory, but in this case we can save ourselves a bit of coding by binding the "cbr" checkbox's value to a key "cbrSwitch" and the bitrate text field's value to a key "cbrText." We can now immediately bind the bitrate text field's enabled to the "cbr" checkbox; thus, the text field will be enabled or disabled automatically as the user checks or unchecks the check box. Also, later on we'll be able to supply a default value for the bitrate text field without writing any code. (Figure 27-7 shows the bindings for the bitrate text field as displayed in the inspector after creating them.)

This completes the design of the interface, so save and quit Interface Builder. Back in Xcode, now comes the touchiest part of the process—editing the Info.plist file. Starting with Xcode 2.1 this task is has been made somewhat easier, because it can be

Figure 27-7. Binding an interface element

done mostly in the target's info window. Choose Project → Edit Active Project to bring up the info window, and switch to the Properties pane; in the lower half is the Collection popup menu, and the idea is to choose each of its items in turn and edit, as necessary, the members of that collection. Here are the settings I've elected to use:

Collection

Setting

Value

General

Action name

LAME Encode

General

Application

iTunes

General

Category

iTunes

General

Icon name

iTunes

Parameters

cbrSwitch

Type boolean; value 0

Parameters

cbrText

Type string; value 128

Parameters

exampleText

Type string; value "dummy"

Description

Summary

Encodes files to MP3.

Also, in the General

Return Main Page Previous Page Next Page

®Online Book Reader