< Previous PageNext Page > Hide TOC

Menu Suite

This chapter describes the terminology in AppleScript Studio’s Menu suite.

The Menu suite defines a small number of classes and event handlers for working with menus. It includes the menu and menu item classes and the choose menu item and update menu item event handlers.

Terminology

The classes and events in the Menu suite are described in the following sections:

For enumerated constants, see “Enumerations.”

Classes

The Menu suite contains the following classes:

menu
Plural: menus
Inherits from: item
Cocoa Class: NSMenu

Represents a menu.

For every menu in the menu bar (such as Application, File, Edit, and so on) there is a menu object. For every menu item in a menu (such as New or Open), there is a menu item object.

Figure 8-1 shows the default menus for an AppleScript Studio application (when you create the project by choosing either the AppleScript or the AppleScript Document-based application template in Xcode). The menus are shown in an Interface Builder nib window, with the File menu open.

You can add a menu in Interface Builder by dragging a Submenu item from the Cocoa-Menus pane of the Palette window to the main menu. You can set various attributes for menu in Interface Builder’s Info window.

It is possible to change a menu dynamically by adding and deleting items. You can also dynamically populate the menu for a popup button, as shown in the Examples section for the popup button class.

Starting in AppleScript Studio version 1.3, you can create menus and menu items using make new, and you can assign a script to the items you create. For a sample script, see the Examples section below.


Figure 8-1  The File menu in Interface Builder

The File menu in Interface Builder

For more information on menus, see the document Application Menu and Pop-up List Programming Topics for Cocoa.

Properties of menu objects

A menu object has these properties:

auto enables items
 Access:read/write
 Class:boolean
 Should the menu auto enable its items? default is true; you can set this value in Interface Builder; however, your application has no convenient way of enabling and disabling menu items on standard application menus (such as the File and Edit menus), so setting this property to false for those menus is not recommended; to read more about Cocoa’s underlying support for menu enabling, see NSMenu and NSMenuValidation
super menu
 Access:read/write
 Class:menu
 the menu that contains this menu
title
 Access:read/write
 Class:Unicode text
 the title of the menu; you can set this value in Interface Builder


Elements of menu objects

A menu object can contain the elements listed below. Your script can access most elements with any of the key forms described in “Standard Key Forms.”

menu
 Specify by: “Standard Key Forms”
 

the menu’s submenus

menu item
 Specify by: “Standard Key Forms”
 

the menu’s menu items


Events supported by menu objects

A menu object supports handlers that can respond to the following events:

Nib
awake from nib

Examples

The following script statements, taken from a script running in the Script Editor application, target the menus of a simple AppleScript Studio application. You can use similar statements within an AppleScript Studio application script, though you won’t need the tell application statement.

tell application "TestApp"
    first menu -- result: main menu of application "TestApp"
    title of main menu -- result: "MainMenu"
    menu items of main menu -- result: a list of menu items
    title of menu items of main menu
    -- result: {"", "File", "Edit",  "Window", "Help"}
    menus of main menu -- result: long list
    -- {sub menu of menu item id 1 of main menu of application
         "TestApp", etc. }
    menu items of sub menu of menu item id 1 of main menu
        -- result: a long list
end tell

The following example shows how to create a script object, create a new menu, add a menu item to the menu, and assign the script to the new menu item.

Important: Prior to AppleScript Studio version 1.5, released with Mac OS X version 1.5, the set script statement shown below resulted in a memory leak.

-- Create a script object
    script displayScript
        on choose menu item theItem
            display dialog "Testing"
        end choose menu item
    end script
 
-- Make a new menu
    set testMenu to make new menu at end of menus of main menu with  properties  {title:"Testing"}
 
-- Make a new menu item in the menu
    set displayMenuItem to make new menu item at end of menu items  of testMenu with  properties {title:"Display Test", name:"display"}
 
-- Assign the script to the menu item
    set script of displayMenuItem to displayScript

The Examples section for the menu item class shows how to access additional properties of menu items.

menu item
Plural: menu items
Inherits from: item.
Cocoa Class: NSMenuItem

Represents an item in a menu.

Every menu item is associated with a menu object. Figure 8-2 shows the New menu item in the File menu in an Interface Builder nib window.


Figure 8-2  The New menu item in the File menu in Interface Builder

The New menu item in the File menu in Interface Builder

You can add a menu item to a menu in Interface Builder by dragging an Item from the Cocoa-Menus pane of the Palette window to the menu. You can set various attributes for menu items in Interface Builder’s Info window. Starting in AppleScript Studio version 1.3, you can also use make new to create new menu items in scripts, as noted in the description for the menu class.

For information on working with pop-up menus, including inserting and deleting menu items and getting and setting the current menu item, see the Examples section for the popup button class.

For more information on menus, see the document Application Menu and Pop-up List Programming Topics for Cocoa.

Properties of menu item objects

A menu item object has these properties:

action method
 Access:read/write
 Class:Unicode text
 the action method for the menu item—a string representation of a Cocoa method selector
associated object
 Access:read/write
 Class:item
 the object associated with the menu item
enabled
 Access:read/write
 Class:boolean
 Is the menu item enabled? you can connect a update menu item handler to a menu item to gain control over whether it should be enabled or disabled
has sub menu
 Access:read only
 Class:boolean
 Does the menu item have a sub menu?
image
 Access:read/write
 Class:image
 the image for the menu item; by default, no image is assigned to a menu
key equivalent
 Access:read/write
 Class:Unicode text
 the key equivalent to select the menu item; default is no key equivalent for menu items you add in Interface Builder, but you can specify a key equivalent in the Attributes pane of the Info window; default menu items have default key equivalents (such as Command-N for the New menu item in the File menu)
key equivalent modifier
 Access:read/write
 Class:number
 not supported (through AppleScript Studio version 1.4); the modifier key for the key equivalent; default is no key equivalent modifier for menu items you add in Interface Builder, but you can set a modifier in the Attributes pane of the Info window; default menu items have default key equivalents modifies (such as Shift-Command-P for the Page Setup… menu item in the File menu)
menu
 Access:read/write
 Class:menu
 the menu that contains the menu item; when you create and modify menus in Interface Builder, this property is set automatically
separator item
 Access:read only
 Class:boolean
 Is the menu item a separator item? you can add separator items in Interface Builder by dragging the empty menu from the Cocoa-Menus pane in the Palette window (you can use tooltips to find this item)
state
 Access:read/write
 Class:enumerated constant from Cell State Value
 the state of the menu item
sub menu
 Access:read/write
 Class:menu
 the sub menu of the menu item (if any)
tag
 Access:read/write
 Class:integer
 the tag of the menu item (an arbitrary item associated with the menu item); default is 0; you can set this property in the Info window in Interface Builder; you might use a tag, for example, as a way to identify a particular menu item
target
 Access:read/write
 Class:anything
 the target of the action for the menu item
title
 Access:read/write
 Class:Unicode text
 the title of the menu item; you can set this property in the Info window in Interface Builder


Commands supported by menu item objects

Your script can send the following commands to a menu item object:

perform action

Events supported by menu item objects

A menu item object supports handlers that can respond to the following events:

Menu
choose menu item
update menu item

Nib
awake from nib

Examples

The following script statements, taken from a script running in the Script Editor application, show how to access properties of the “New” menu item in the “File” menu of a simple AppleScript Studio application. You can use similar statements within an AppleScript Studio application script, though you won’t need the tell application statement.

tell application "TestApp"
    set menuItem to second menu item of main menu
    title of menuItem -- result: "File"
    set item1 to first menu item of sub menu of menuItem
    title of item1 -- result: "New"
    key equivalent of item1 -- result: "n"
end tell

For an example that shows how to add a script to a menu item, see the Examples section for the menu class.

Version Notes

The action method and target properties were added in AppleScript Studio version 1.4. As a result, you can dynamically change the target of an action in your application or change the Cocoa method that is executed when the action is triggered. For an example of how to do this, see the toolbar item class.

The key equivalent modifier property in this class is not supported, through AppleScript Studio version 1.4.

Events

Objects based on classes in the Menu suite support handlers for the following events (an event is an action, typically generated through interaction with an application’s user interface, that causes a handler for the appropriate object to be executed). To determine which classes support which events, see the individual class descriptions.

choose menu item

Called when a menu item is chosen.

Syntax
choose menu item reference required
Parameters
reference

a reference to the menu item object whose choose menu item handler was called

Examples

When you connect a choose menu item handler to a menu item object in Interface Builder, AppleScript Studio supplies an empty handler template. This handler is where your application deals with the user’s menu choice.

on choose menu item theObject
    (* Add script statements here to handle the chosen menu item.  *)
end choose menu item

For an example that shows how to use this event handler, see the Task List sample application, available at <Xcode>/Examples/AppleScript Studio (starting with AppleScript Studio version 1.2).

update menu item

Called periodically when the state of a menu item may need to be updated.

The handler should return true to enable the menu item or false to disable it.

Syntax
update menu item reference required
Parameters
reference

a reference to the menu item object whose update menu item handler was called

Examples

When you connect an update menu item handler to a menu item object in Interface Builder, AppleScript Studio supplies an empty handler template. The theObject parameter refers to the chosen menu item and you can use the parameter to access properties or elements of the item. If, for example, your browser application has a menu item to display the modified date for a selected file, you might use an update menu item handler to disable the menu item when no file is selected.

on update menu item theObject
    (* if the menu item should be enabled… *)
    return true
    (* other statements *)
    (* if the menu item should be disabled *)
    return false
end update menu item


< Previous PageNext Page > Hide TOC


© 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-10-31)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.