How do I add a toolbar to my application's window?

If you have ever wanted to add toolbars to your application, the only way to do it is through Applescript. Interface Builder doesn’t include the ability to design toolbars (as of Tiger 10.4.10), though that may change in Leopard.

You may have noticed that Interface Builder does include the ability to use the “unified” window look on your windows. But without a toolbar, that checkbox is meaningless. When designing your window and toolbar, think about whether you want the window to have the unified look or not. Try both, and use what looks best to you.

In the Applescript portion of Interface Builder’s Inspector window, make sure you select the awake from nib event for your window. It is during the awake from nib handler that you will initialize your toolbar.

Using the variable theObject, which in an awake from nib handler is a reference to the window in question, you create a toolbar by using make:

-- Make the new toolbar, giving it a unique identifier
set MyWindowToolbar to make new toolbar at end with properties {name:"MyWindow toolbar", identifier:"MyWindow toolbar identifier", allows customization:true, auto sizes cells:true, display mode:default display mode, size mode:default size mode}

	-- Setup the allowed and default identifiers.
	set allowed identifiers of MyWindowToolbar to {"add client identifier", "remove client identifier", "edit client identifier", "print item identifier", "customize toolbar item identifer", "flexible space item identifer", "space item identifier", "separator item identifier"}
	
	set default identifiers of MyWindowToolbar to {"add client identifier", "remove client identifier", "flexible space item identifer","edit client identifier"}

The last 2 commands tell the window what toolbar icons are allowed and what the default set is. Remember, the user can customize the toolbar to suit themselves, so it behooves you to give them the best default set you can think of without being overkill.

Besides the toolbar buttons you create, you also have the freedom to use established buttons like Print, Space, Flexible Space, Separator and Customize Toolbar. Note: The names of two of the standard items are mispelled and must be mispelled to work. The word “identifier” is spelled as “identifer.” The two items are the “flexible space item identifer” and the “customize toolbar item identifer.”

Once the toolbar is created, then you can begin to fill it with buttons, again using make. The standard items already exist, so you don’t need to create them, just the buttons you are adding of your own.

-- Create the toolbar items, adding them to the toolbar.
	make new toolbar item at end of toolbar items of MyWindowToolbar with properties {identifier:"add client identifier", name:"add client", label:"Add Client", palette label:"Add Client", tool tip:"Add a new client.", image name:"addclient32.png"} --note the usage of your own icon file, usually at 32x32
	make new toolbar item at end of toolbar items of MyWindowToolbar with properties {identifier:"remove client identifier", name:"remove client", label:"Remove Client", palette label:"Remove Client", tool tip:"Remove an existing client.", image name:"removeclient32.png"}
	make new toolbar item at end of toolbar items of MyWindowToolbar with properties {identifier:"edit client identifier", name:"edit client", label:"Edit Client", palette label:"Edit Client", tool tip:"Edit a client's information.", image name:"editclient32.png"}

At this point all that is left is to assign the toolbar to the window:

-- Assign our toolbar to the window
	set toolbar of theObject to MyWindowToolbar