beetlebug

macOS features

This page shows the parts of macOS that a ztk script reaches: the Dock icon, an icon in the menu bar, the dialogs of macOS, drags with Finder, window tabs, and the full screen. Each part has a script that you can run with ztk.

The commands of this page exist on each platform, and they give an error where they have no effect. Test the platform first with ztk::has:

Test 1 on macOS when
ztk::has taskbar ztk::taskbar reaches the Dock icon.
ztk::has tray ztk::tray can show an icon in the menu bar.
ztk::has mac The commands ztk::mac::* work.
ztk::has native-menus The menus are menus of macOS.

The Dock icon

ztk::taskbar changes the Dock icon of the app. It shows a count, a progress bar over the icon, or a bounce that asks the user to look at the app:

Command The Dock icon
ztk::taskbar badge 3 A red badge with 3.
ztk::taskbar badge 0 No badge.
ztk::taskbar progress 0.4 A bar along the bottom of the icon.
ztk::taskbar progress indeterminate A bar with no value.
ztk::taskbar progress none No bar.
ztk::taskbar attention One bounce.
ztk::taskbar attention -critical Bounces until the app is active.
The Dock icon of a ztk app with a red badge of 3 and a progress bar along its bottom
The Dock icon after ztk::taskbar badge 3 and ztk::taskbar progress 0.4.

macOS bounces the icon only while the app is not active. The Dock has no colors for a paused or a failed job, so -state paused and -state error show the bar with its value.

A job with a progress bar

This script moves a progressbar in the window and the bar of the Dock icon together:

ttk::progressbar .p -maximum 10 -length 240
pack .p -padx 20 -pady 20
proc step {i} {
    .p configure -value $i
    ztk::taskbar progress [expr {$i / 10.0}]
    if {$i < 10} {
        after 300 [list step [incr i]]
    } else {
        ztk::taskbar progress none
        ztk::taskbar attention
    }
}
step 0

The menu of the Dock icon

ztk::mac::dockmenu adds the entries of a menu to the menu of the Dock icon, above the items of macOS:

menu .dock -tearoff 0
.dock add command -label "New Window" -command newWindow
.dock add command -label "Pause Sync" -command {puts pause}
if {[ztk::has mac]} {
    ztk::mac::dockmenu .dock
}
proc newWindow {} {
    toplevel .w[clock milliseconds]
}

ztk::mac::dockmenu with no menu removes the entries.

The menu of the Dock icon of a ztk app, with New Window and Pause Sync above the items of macOS
The menu of the Dock icon with the entries of .dock.

An icon in the menu bar

ztk::tray puts an icon in the menu bar, on the right, as the menu bar extras of macOS do. A click on the icon opens its menu:

image create photo dot -width 16 -height 16
dot put black -to 3 3 13 13
menu .traymenu -tearoff 0
.traymenu add command -label "Open Notes" -command {wm deiconify .}
.traymenu add separator
.traymenu add command -label Quit -command exit
if {[ztk::has tray]} {
    ztk::tray create -image dot -template 1 -tooltip "Notes" -menu .traymenu
}
Option On macOS
-template 1 macOS draws the image in the colors of the menu bar: for a light bar, for a dark bar, and while the menu is open. Use a black image with an alpha channel.
-image The icon, 18 points high. With no image, the icon of the app.
-menu A click opens the menu.
-tooltip The text that shows when the pointer stays on the icon.
The menu bar of macOS with the icon of a ztk app and its open menu: Open Notes and Quit
A menu bar icon with its menu.

An icon with a command

An icon with no -menu runs its -command. %a is the action: click, or menu for a right click or a click with Control. This script uses the image dot above:

if {[ztk::has tray]} {
    ztk::tray create -image dot -template 1 -command {
        if {"%a" eq "click"} {wm deiconify .; raise .}
    }
}

An app with no Dock icon

An app that runs only in its menu bar icon has no Dock icon and no menu bar. Make it with --menu-bar-only:

ztk bundle -o Sync.app --menu-bar-only sync.tcl

ztk::tray has each option.

The dialogs of macOS

The dialogs of Tk are the dialogs of macOS:

Command Dialog
tk_getOpenFile The open panel of macOS.
tk_getSaveFile The save panel of macOS.
tk_chooseDirectory The open panel, for a folder.
tk_messageBox An alert of macOS, with the buttons of -type.
tk_chooseColor The color panel of macOS.

With -parent, a file dialog and a message box open as a sheet of the parent window. The command waits for the answer, as in Tk:

ttk::button .open -text "Open..." -command {
    set file [tk_getOpenFile -parent . -title "Open a note" \
        -filetypes {{"Text files" {.txt .md}} {"All files" *}}]
    if {$file ne ""} {puts "open $file"}
}
ttk::button .ask -text "Delete..." -command {
    set answer [tk_messageBox -parent . -type yesno -icon warning \
        -message "Delete the note?" -detail "You cannot undo this action."]
    puts $answer
}
pack .open .ask -side left -padx 10 -pady 20
A ztk window with the open panel of macOS as a sheet below its title bar
tk_getOpenFile -parent . opens the open panel as a sheet of the window.
An alert of macOS from tk_messageBox with a warning icon and the buttons No and Yes
tk_messageBox -type yesno -icon warning.
  • The filters of -filetypes become the file types that the panel accepts.
  • The default button of an alert is on the right, and Return chooses it.
  • The color panel has no Cancel button. tk_chooseColor gives the color of the panel when the user closes it.

Drags with Finder

A widget gets files from Finder and text from other apps with tkdnd::drop_target. <<Drop>> gets the list of the paths in %D:

ttk::label .drop -text "Drop files here" -padding 40 -relief groove
pack .drop -padx 20 -pady 20 -fill both -expand 1
tkdnd::drop_target register .drop {DND_Files DND_Text}
bind .drop <<Drop>> {.drop configure -text "[llength %D] items"}

Drag out of a window

ztk::dnd drag drags files or text to Finder or to another app. Call it from a binding of <B1-Motion>. It waits until the drop, and it gives copy, move, or none:

set path [file normalize notes.txt]
ttk::label .file -text "notes.txt" -padding 20
pack .file -padx 20 -pady 20
bind .file <B1-Motion> {
    set how [ztk::dnd drag .file -files [list $path]]
    puts "the drop gave $how"
}

macOS shows the icon of each file under the pointer while the drag runs. ztk::dnd has each option and event.

Window tabs

ztk::mac::tabbing lets the windows of the app join one window with tabs, as the documents of Finder and TextEdit do:

proc newDoc {} {
    set w .doc[clock milliseconds]
    toplevel $w
    wm title $w "Untitled"
    if {[ztk::has mac]} {
        ztk::mac::tabbing $w -mode preferred -group documents
    }
}
newDoc
newDoc
-mode What it does
automatic Follows the setting "Prefer tabs when opening documents" of System Settings. This is the default.
preferred A new window of the group opens as a tab.
disallowed The window has no tab bar.

The windows of one -group join one tab bar.

VoiceOver

VoiceOver and the other assistive tools of macOS read the Tk widgets of a window through the accessibility API of AppKit:

Widget VoiceOver reads VoiceOver press (Control-Option-Space)
A label Its text No action
A button Its text, and "button" Presses the button
A check button, a radio button Its text, and its state Changes the state
An entry, a spinbox, a combobox Its text; the text of an entry with -show stays silent No action
A widget with -state disabled It, and "dimmed" No action

VoiceOver goes through the widgets in the order of the widget tree, follows the keyboard focus of Tk, and reads the window again after a change of the widgets. A button with an image and no text has no name, so give it a -text.

The full screen and floating windows

wm attributes puts a window in the full screen of macOS, a space of its own. It can also keep a window above the windows of other apps:

Command The window
wm attributes . -fullscreen 1 Goes to the full screen.
wm attributes . -fullscreen 0 Comes back from the full screen.
wm attributes . -topmost 1 Stays above the windows of other apps.
A ztk window in the full screen of macOS, with no title bar
A window after wm attributes . -fullscreen 1.

The green button of the title bar also puts the window in the full screen. The next page, Limits, lists what does not work on macOS.