Unit 5 · Lesson 12 Intermediate ⏱ ~45 minutes

Native Toolbars

HyperXTalk lets you add a native toolbar to any stack window — the same kind you see in Finder, Safari, and Mail on macOS, or matching the native style on Windows and Linux. In this lesson you'll learn how to create a toolbar, populate it with SF Symbols icons, and respond to toolbar button clicks in your scripts.

🎯 Learning Objectives
  • Understand what a native toolbar is and when to use one
  • Create a toolbar and attach it to a stack window
  • Add items with labels and SF Symbols icons
  • Handle toolbarItemClicked messages in a script
  • Show and hide the toolbar

🛠️ What is a Native Toolbar?

A native toolbar is the row of icon buttons that sits at the top of a window, just below the title bar. Unlike buttons you place on a card yourself, a native toolbar is rendered by the operating system — which means it automatically adopts the system appearance, responds to dark mode, supports customisation by the user, and looks exactly like the toolbars in the OS's own apps.

HyperXTalk's toolbar support is provided by the org.openxtalk.nstoolbar extension. You interact with it entirely through plain HyperXTalk script — no Objective-C required.

A stack window showing a native toolbar with New, Open and Save buttons
Fig. 1 — The finished toolbar
🟢 Native toolbars work on macOS, Windows, and Linux. The toolbar automatically adopts the look and feel of each platform, so your users always get a toolbar that feels at home on their system.

🔨 Creating a Toolbar

You create a toolbar with the create toolbar command, giving it an identifier name and telling it which stack window to attach to:

create toolbar "mainBar" in this stack

The first argument is the toolbar's identifier — a string you choose. You'll use this same identifier in every subsequent command that refers to this toolbar. The identifier must be unique within your stack.

To make the toolbar visible, set its toolbarVisible property to true:

set the toolbarVisible of toolbar "mainBar" to true
💡 You can create the toolbar and set all its properties before making it visible. This is good practice — it avoids the user seeing the toolbar appear item by item.

🧩 Toolbar Items

Each button in a toolbar is called an item. Items are identified by a short name you choose — like "fileNew" or "fileSave". For each item you want to add, you set its label and icon, then add it to the toolbar's item list.

Setting Item Properties

stack script
-- Set the label shown beneath each toolbar button set the itemLabel["fileNew"] of toolbar "mainBar" to "New" set the itemLabel["fileOpen"] of toolbar "mainBar" to "Open" set the itemLabel["fileSave"] of toolbar "mainBar" to "Save" -- Set the SF Symbols icon for each item set the itemIcon["fileNew"] of toolbar "mainBar" to "doc.badge.plus" set the itemIcon["fileOpen"] of toolbar "mainBar" to "folder" set the itemIcon["fileSave"] of toolbar "mainBar" to "square.and.arrow.down"

Adding Items to the Toolbar

Setting labels and icons registers the items, but doesn't add them to the visible toolbar yet. You control which items appear — and in what order — by setting the itemNames property to a return-separated list of item identifiers:

set the itemNames of toolbar "mainBar" to \ "fileNew" & return & "fileOpen" & return & "fileSave"

The order of the names determines the order of the buttons from left to right. You can reorder or change the items at any time by setting itemNames again.

Toolbar Item Properties at a Glance

Property What it does
itemLabel["id"] The text label shown beneath the button
itemIcon["id"] An SF Symbols name (e.g. "folder") or the filename of an image imported into your stack (e.g. "MyLogo.png")
itemNames Return-separated list of item IDs that appear in the toolbar, in order
toolbarVisible Whether the toolbar is shown (true) or hidden (false)

🔣 SF Symbols Icons

Item icons are specified using SF Symbols names — Apple's built-in icon library. There are over 6,000 symbols covering almost every common UI concept.

Some commonly useful symbols for toolbar items:

Symbol nameLooks like
doc.badge.plusNew document
folderOpen folder
square.and.arrow.downSave / download
square.and.arrow.upShare / export
arrow.uturn.backwardUndo
arrow.uturn.forwardRedo
trashDelete
magnifyingglassSearch
gearSettings
info.circleInfo / about
printerPrint
pencilEdit
🟢 To browse all available symbols, download the free SF Symbols app from Apple at developer.apple.com/sf-symbols. You can search, preview, and copy the exact symbol name to paste into your script.
The SF Symbols app with folder searched, showing many folder icon variants with their names
Fig. 2 — SF Symbols app

👆 Handling Toolbar Clicks

When the user clicks a toolbar button, HyperXTalk sends a toolbarItemClicked message to the stack, passing the item's identifier as a parameter. You handle this in your stack script or card script using a switch statement:

stack script
on toolbarItemClicked pItemName switch pItemName case "fileNew" doMenu "New Stack" break case "fileOpen" doMenu "Open Stack..." break case "fileSave" save this stack break end switch end toolbarItemClicked

The switch statement checks pItemName against each case in turn, running the matching block and then stopping at break. This is the cleanest way to handle multiple toolbar items.

💡 The toolbarItemClicked handler can live in your stack script or a card script, but not in an object script (such as a button or field). Place it wherever makes the most sense for your application's structure.

Putting It All Together

Here's a complete, self-contained example. Put this entire script in your stack script and it will build the toolbar when the stack opens:

stack script
on openStack -- Build a toolbar from scratch create toolbar "mainBar" in this stack -- Define item labels set the itemLabel["fileNew"] of toolbar "mainBar" to "New" set the itemLabel["fileOpen"] of toolbar "mainBar" to "Open" set the itemLabel["fileSave"] of toolbar "mainBar" to "Save" -- Define item icons (SF Symbols names) set the itemIcon["fileNew"] of toolbar "mainBar" to "doc.badge.plus" set the itemIcon["fileOpen"] of toolbar "mainBar" to "folder" set the itemIcon["fileSave"] of toolbar "mainBar" to "square.and.arrow.down" -- Set which items appear and in what order set the itemNames of toolbar "mainBar" to \ "fileNew" & return & "fileOpen" & return & "fileSave" -- Show the toolbar set the toolbarVisible of toolbar "mainBar" to true end openStack on toolbarItemClicked pItemName switch pItemName case "fileNew" doMenu "New Stack" break case "fileOpen" doMenu "Open Stack..." break case "fileSave" save this stack break end switch end toolbarItemClicked

⚙️ Show & Hide

Toggle the toolbar's visibility at any time — for example, to let the user show or hide it from a menu:

set the toolbarVisible of toolbar "mainBar" to false
🛠️ Exercise — Build a Text Editor Toolbar

Create a simple text editor stack with a toolbar containing formatting controls.

  1. Create a new stack called TextEditor.hyperxtalk and add a scrolling field named Editor that fills most of the card.
  2. Open the stack script and add an openStack handler that creates a toolbar called "formatBar".
  3. Add four items: "textBold" (Bold, icon: bold), "textItalic" (Italic, icon: italic), "textUnderline" (Underline, icon: underline), and "textClear" (Clear, icon: trash).
  4. Add a toolbarItemClicked handler. For Bold, Italic, and Underline, toggle the relevant text style of the selected text in the Editor field. For Clear, set the text of the Editor field to empty.
  5. Test by typing some text in the field, selecting it, and clicking your toolbar buttons.

Bonus challenge: Add a fifth item called "fileSave" (Save, icon: square.and.arrow.down) that saves the stack when clicked.

📝 Review Questions
Question 1
What command creates a native toolbar?
create toolbar "identifier" in this stack creates a new toolbar and attaches it to the stack window.
Question 2
How do you control which items appear in a toolbar and in what order?
By setting the itemNames property to a return-separated list of item identifiers in the desired order.
Question 3
What message is sent when a toolbar button is clicked, and where should you handle it?
toolbarItemClicked is sent with the item identifier as a parameter. It can be handled in the stack script or a card script, but not in an object script such as a button or field.
Question 4
What are the two ways to specify a toolbar icon?
You can use SF Symbols names (e.g. "folder") — Apple's built-in icon library browsable via the free SF Symbols app — or the filename of an image imported into your stack (e.g. "MyLogo.png").
Question 5
What happens when the user clicks a toolbar item, and what parameter does the handler receive?
HyperXTalk sends a toolbarItemClicked message with the clicked item's identifier as a parameter (e.g. pItemName). You use a switch statement in your stack or card script to check the identifier and run the appropriate code.