tk::mac(3tk)
tk::mac β aka "how to talk to your Mac from Tk" π
okay bestie, so this is tk::mac(3tk) from the Tk Built-In Commands manual, and it's literally the whole vibe of "my Tk app wants to be a real macOS citizen" β¨. It lives in the ::tk::mac namespace and it's all about Apple Events, native dialogs, and making your app not look like it time-traveled from 1994. Let's go π«Ά
β¨ NAME β¨
tk::mac β access Mac-specific functionality on macOS from Tk π
πΈ SYNOPSIS πΈ
::tk::mac::DoScriptFile
::tk::mac::DoScriptText
::tk::mac::ShowPreferences
::tk::mac::OpenApplication
::tk::mac::ReopenApplication
::tk::mac::OpenDocument file...
::tk::mac::PrintDocument file...
::tk::mac::Quit
::tk::mac::OnHide
::tk::mac::OnShow
::tk::mac::ShowHelp
::tk::mac::PerformService
::tk::mac::LaunchURL URL...
::tk::mac::GetAppPath
::tk::mac::standardAboutPanel
::tk::mac::useCompatibilityMetrics boolean
::tk::mac::CGAntialiasLimit limit
::tk::mac::antialiasedtext number
::tk::mac::useThemedToplevel boolean
::tk::mac::iconBitmap name width height -kind value
π EVENT HANDLER CALLBACKS π
The Aqua/macOS application environment defines a bunch of extra events that apps should respond to. Tk maps all of those onto calls into the ::tk::mac namespace. And unless it says otherwise below: if the command isn't there, nothing happens. No drama, no error, just silence π
::tk::mac::DoScriptFileβ the default Apple Event handler forAEDoScriptHandler. This one runs a Tcl file when an AppleScript sends a "do script" command to Wish with a file path as a parameter. π::tk::mac::DoScriptTextβ also the default Apple Event handler forAEDoScriptHandler. This one runs Tcl code when an AppleScript sends a "do script" command to Wish with Tcl code or a Tcl procedure as a parameter.::tk::mac::ShowPreferencesβ the default Apple Event handler forkAEShowPreferences,"pref". Heads up: the "Preferences" item in the application menu is only enabled when this proc is defined π. Usually you wrap your own prefs command in it, and that command pops up a preferences window. Something like:
proc ::tk::mac::ShowPreferences {} {
setPref
}
::tk::mac::OpenApplicationβ if you define a proc with this name, it fires when your application is initially opened. It's the default Apple Event handler forkAEOpenApplication,"oapp". π±::tk::mac::ReopenApplicationβ if you define it, it's the default Apple Event handler forkAEReopenApplication,"rapp"β that's the Apple Event sent when your app is opened while it's already running (like, you clicked its icon in the Dock, bestie). Here's a lil sample that raises a minimized window when the Dock icon gets clicked:
proc ::tk::mac::ReopenApplication {} {
if {[wm state .] eq "withdrawn"} {
wm state . normal
} else {
wm deiconify .
}
raise .
}
::tk::mac::OpenDocument file...β if you define it, it's the default Apple Event handler forkAEOpenDocuments,"odoc"β the event that fires when your app is asked to open one or more documents (drag & drop onto the app, or opening a document whose type is associated with the app, that whole thing). Your proc should take the paths to the files to open as arguments, like so:
proc ::tk::mac::OpenDocument {args} {
foreach f $args {my_open_document $f}
}
::tk::mac::PrintDocument file...β if you define it, it's the default Apple Event handler forkAEPrintDocuments,"pdoc"β the event fired when your app is asked to print a document. It takes a single absolute file path as an argument. π::tk::mac::Quitβ if you define it, it's the default Apple Event handler forkAEQuitApplication,"quit"β sent when your app is asked to quit, e.g. via the quit menu item in the application menu, the quit menu item in the Dock menu, or during a logout/restart/shutdown etc. If it's not defined,exitgets called instead. πͺ::tk::mac::OnHideβ if defined, this gets called when your app receives akEventAppHiddenevent, e.g. via the hide menu item in the application or Dock menus. π::tk::mac::OnShowβ if defined, this gets called when your app receives akEventAppShownevent, e.g. via the show all menu item in the application menu, or by clicking the Dock icon of a hidden application. π::tk::mac::ShowHelpβ customizes the behavior of the Apple Help menu. If this procedure is not defined, the platform-specific standard Help menu item"YourApp Help"does the default Cocoa thing: it shows the Help Book configured in the application'sInfo.plist(or displays an alert if no Help Book is set). π::tk::mac::PerformServiceβ runs a Tcl procedure called from the macOS "Services" menu in the Application menu item. The "Services" menu item enables inter-application communication: data from one app (like selected text) can get sent to another app for processing β for example to Safari as a Google search item, or to TextEdit to be appended to a file. Here's an example of the proc, which you should rewrite in an application script for customization:
proc ::tk::mac::PerformService {} {
set data [clipboard get]
$w insert end $data
}
ngl bestie, the mechanism for retrieving the data is from the clipboard β there is literally no other supported way to get that data. If you don't want the Services process, you can delete theNSServiceskeys from the application'sInfo.plistfile. The underlying code supporting this command also lets thetext,entryandttk::entrywidgets access services from other applications via the Services menu. TheNSPortNamekey in Wish'sInfo.plistfile is currently set as"Wish"; if a developer changes the name of the Wish executable to something else, this key should be modified with the same name. π§
::tk::mac::LaunchURL URL...β if defined, launches a URL within Tk. You'd use this if a Tk application wants to handle a URL itself β like displaying data from an RSS feed β instead of launching a default application to handle the URL, though yeah, it can also be defined as such. Wish includes a stub URL scheme of"foo://"in theCFBundleURLSchemeskey of itsInfo.plistfile; this should be customized for the specific URL scheme the developer wants to support. π::tk::mac::GetAppPathβ returns the current application's file path. π
π ADDITIONAL DIALOGS π
Aqua/macOS defines extra dialogs that applications should support, and yes, we're covering them all.
::tk::mac::standardAboutPanelβ brings the standard Cocoa about panel to the front, filled in with info from the application bundle files. The panel displays the application icon and the values associated with theinfo.plistkeys namedCFBundleName,CFBundleShortVersionString,NSAboutPanelOptionVersionandNSHumanReadableCopyright. If a file namedCredits.htmlorCredits.rtfexists in the bundle'sResourcesdirectory, its contents get displayed in a scrolling text box at the bottom of the dialog. See the documentation for-[NSApplication orderFrontStandardAboutPanelWithOptions:]for more details. There's also a hook for a custom About dialog: if a Tcl proc namedtkAboutDialogis defined in the main interpreter, that procedure gets called instead of opening thestandardAboutPanel. β¨
π SYSTEM CONFIGURATION π
There are a bunch of extra global configuration options that control the details of how Tk renders by default. All the aesthetics, basically.
::tk::mac::useCompatibilityMetrics booleanβ preserves compatibility with older Tk/Aqua metrics; set it tofalsefor more native spacing. π::tk::mac::CGAntialiasLimit limitβ sets the antialiasing limit: lines thinner than limit pixels will not be antialiased. It's an integer, set to0by default, which makes all lines be antialiased. ποΈ::tk::mac::antialiasedtext numberβ sets anti-aliased text. Controls text antialiasing; possible values for number are-1(default, use system default for text AA),0(no text AA),1(use text AA). π€::tk::mac::useThemedToplevel booleanβ makes toplevel windows draw with the modern grayish/pinstripe Mac background. Equivalent to configuring the toplevel with"-background systemWindowHeaderBackground", or to using attk::frame. πͺ
π¦ SUPPORT COMMANDS π¦
::tk::mac::iconBitmap name width height -kind valueβ renders native icons and bitmaps in Tk applications (including any image file readable byNSImage). A native bitmap name is interpreted as follows, in order:- predefined builtin 32x32 icon name (
stop,caution,document, etc.) π - name, as defined by
tk::mac::iconBitmap NSImagenamed image nameNSImageurl string- 4-char
OSTypeof IconServices icon
- predefined builtin 32x32 icon name (
The width and height arguments to tk::mac::iconBitmap define the dimensions of the image to create, and -kind must be one of:
-fileβ icon of file at given path-fileTypeβ icon of given file type-osTypeβ icon of given 4-charOSTypefile type-systemTypeβ icon for given IconServices 4-charOSType-namedImageβ namedNSImagefor given name-imageFileβ image at given path
β SEE ALSO β
π KEYWORDS π
about dialog, antialiasing, Apple event, icon, NSImage
and that's the whole page, bestie!! this one shipped with Tk 8.6, filed under tk::mac(3tk), and it's giving "your Tk app finally gets to be a main character on macOS" energy. go forth and define your procs πβ¨π
π¦ where this page comes from
- debian trixie: tk8.6-doc 8.6.16-1
- debian trixie: tk9.0-doc 9.0.1-3
- ubuntu questing: tk8.6-doc 8.6.17-1
- ubuntu questing: tk9.0-doc 9.0.2-1
- ubuntu resolute: tk8.6-doc 8.6.17-1build1
- ubuntu resolute: tk9.0-doc 9.0.3-1
- ubuntu stonking: tk8.6-doc 8.6.18-1
- ubuntu stonking: tk9.0-doc 9.0.4-1
- Copyright (c) 2011 Kevin Walzer
- Copyright (c) 2011 Donal K. Fellows
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms