tk::mac(3tk)

πŸ“¦
version 9.0.4 Β· 2 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

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 for AEDoScriptHandler. 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 for AEDoScriptHandler. 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 for kAEShowPreferences, "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 for kAEOpenApplication, "oapp". 🌱
  • ::tk::mac::ReopenApplication β€” if you define it, it's the default Apple Event handler for kAEReopenApplication, "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 for kAEOpenDocuments, "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 for kAEPrintDocuments, "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 for kAEQuitApplication, "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, exit gets called instead. πŸšͺ
  • ::tk::mac::OnHide β€” if defined, this gets called when your app receives a kEventAppHidden event, 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 a kEventAppShown event, 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's Info.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 the NSServices keys from the application's Info.plist file. The underlying code supporting this command also lets the text, entry and ttk::entry widgets access services from other applications via the Services menu. The NSPortName key in Wish's Info.plist file 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 the CFBundleURLSchemes key of its Info.plist file; 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 the info.plist keys named CFBundleName, CFBundleShortVersionString, NSAboutPanelOptionVersion and NSHumanReadableCopyright. If a file named Credits.html or Credits.rtf exists in the bundle's Resources directory, 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 named tkAboutDialog is defined in the main interpreter, that procedure gets called instead of opening the standardAboutPanel. ✨

🌈 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 to false for 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 to 0 by 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 a ttk::frame. πŸͺŸ

πŸ¦‹ SUPPORT COMMANDS πŸ¦‹

  • ::tk::mac::iconBitmap name width height -kind value β€” renders native icons and bitmaps in Tk applications (including any image file readable by NSImage). 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
    • NSImage named image name
    • NSImage url string
    • 4-char OSType of IconServices icon

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-char OSType file type
  • -systemType β€” icon for given IconServices 4-char OSType
  • -namedImage β€” named NSImage for given name
  • -imageFile β€” image at given path

⭐ SEE ALSO ⭐

bind(3tk), wm(3tk)


πŸ‘‘ 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