Skip to content

Add a Toolbar

There is a sample project that comes with installations of XCode which will show you how to create a toolbar at the top of windows. This method appears to be the only way of creating a standard window toolbar if you are designing an application for OS X 10.4 or earlier versions. You can find this example code here on your hard drive here:

/Developer/Examples/AppleScript Studio/Simple Toolbar

For Leopard users who don’t mind building applications that require OS X 10.5 or higher, you can now create toolbars directly in Interface Builder with the NSToolbar class.

NSToolbar

Add the class to your window, and you will get a series of default buttons. You can add buttons to the toolbar by adding NSToolbarItem (the item to the right with the “?” image in it) and can add images for the toolbar via a popup menu in the inspector.

The Simple Toolbar sample scripts controlled the toolbar buttons through the “clicked toolbar item” handler of the window containing the toolbar, which confused me when I tried to use NSToolbar without generating the toolbar directly in the applescript. After much frustration, I discovered that the toolbar buttons were scripted through the “clicked” handler as any other button in the window might be. There appears to be no need to necessarily use “clicked toolbar item” at all.

UPDATE: I posted to MacScripter on a problem I had with buttons that are added or re-added when the toolbar is customized: NSToolbar error when customizing the toolbar

UPDATE: Another MacSripter posting by regulus6633 points to problems using the new toolbar. Apparently there are problems when referring to any non-button objects on the toolbar (such as pop-up menu or a search field). My own tinkering confirms this.