Recursive AI

macOS desktop services

All oneplatform docs

OnePlatform.Shell.MacDesktop gives an app the parts of a Mac desktop that are not windows: the menu bar, a status item, file panels, notifications and global hotkeys. It is written against Apple's documented AppKit, UserNotifications and Carbon APIs, reached through the Objective-C runtime (objc_msgSend with typed signatures) and Carbon's C functions; nothing is bundled. It is the counterpart of the Linux services in linux-shell.md and uses their types wherever one fits, so an app drives both the same way.

It needs macOS on Apple silicon. On any other system the constructor throws ShellUnavailableException.

Using it

C#
using var app = PlatformApp.Create(new PlatformOptions { Backend = PlatformBackendKind.Cocoa });
using var mac = new MacDesktop(post);   // post: runs an action on the main thread, callable from any thread

Create the PlatformApp first: its application menu is the one MacDesktop names after the bundle and adds the other menus to. Call every member on the main thread, which is the thread that pumps events. What AppKit raises (a menu item chosen, a notification answered, a hotkey pressed) is handed to post, so handlers run where the app wants them, and while the loop is waiting PlatformApp.Wake is the usual way to bring it back.

ServiceMembersApple APILinux counterpart
Menu barSetMenuBar(NativeMenu[]), DescribeMenuBar, ChooseMenuItemNSMenu, NSMenuItemnone (a menu is part of the page)
Status itemShowStatusItem(title, icon, TrayMenuItem[]), DescribeStatusItemNSStatusItemTrayIcon
File panelsOpenFileAsync, OpenFolderAsync, SaveFileAsync(FileChooserOptions) → FileChooserResult; IsPanelOpen, EndPanelNSOpenPanel, NSSavePanelFileChooserPortal
NotificationsShowAsync(DesktopNotification), CloseAsync(id), ActionInvoked(id, action)UNUserNotificationCenterNotificationService
Global hotkeysBindAsync(GlobalShortcut[]), Activated(id, timestamp), PressHotkey(id)Carbon RegisterEventHotKeyGlobalShortcutsSession
One copy per userLocalSingleInstance.Acquire(appId, args, onLaunch)a private Unix socketSingleInstance

Menus are made of TrayMenuItems: Label, Enabled, Visible, Checked, Click, Children, separators, and Shortcut (a key equivalent such as Ctrl+Shift+S, which the menu bar shows and handles; Ctrl means Command as on the other desktops, Alt is Option and MacCtrl is the Control key). The application's own menu stays first and SetMenuBar replaces everything after it. Inside a bundle the application menu reads the bundle's CFBundleName ("Quit Shell Demo") instead of the process file name.

Behaviour worth knowing

  • Notifications need an app bundle with a bundle identifier; from a bare executable the system raises an Objective-C exception that would end the process, so the call throws ShellUnavailableException first. The first notification may make the system ask the user for permission. The app never answers that prompt; if the user has said no, the exception names System Settings, Notifications. A notification shows even while the app is frontmost. DesktopNotification.DefaultAction and each button's Action come back through ActionInvoked.
  • Hotkeys use Carbon, which needs no privacy permission. macOS has no shortcut chooser, so each GlobalShortcut needs a PreferredTrigger in portal syntax (CTRL+ALT+d; CTRL is Control, ALT Option, LOGO, META, CMD and SUPER Command, keys are letters, digits, punctuation, F1 to F12, arrows, Space, Tab, Return, Escape). A key another application holds, or one this table does not know, throws ShellUnavailableException through the task. PressHotkey sends the system's own hotkey event to the app, exercising the registration and the handler without a keyboard.
  • Panels are free-standing; the task completes when the panel closes. File filters are patterns of the form *.ext; anything else is an ArgumentException. One panel at a time. EndPanel ends the open one as its buttons would, which is how code and tests close it.
  • Status item images are drawn 18 points high; the icon is a file path, a named system image, or the application's icon.
  • One copy per user. LocalSingleInstance has the shape of SingleInstance: Acquire, IsPrimary, and ActivationRequests (Open when an argument is a scheme:// URI, otherwise Activate, with all arguments in Arguments) delivered on a background thread. The first launch listens on a socket in a folder only the user can open; a folder others can read is refused. When LaunchServices starts the app, macOS already keeps one copy and brings it forward; the socket covers launches from a terminal or another program.

Errors

A service the system will not give the app as it is throws ShellUnavailableException (a PlatformException) with Service and Requirement naming what to do. Methods that return tasks deliver it through the task. Nothing is skipped silently.

Tests

MacDesktopTests runs the shortcut tables, the typed errors and the single-instance hand-over everywhere, and drives the real AppKit and Carbon on a Mac: menu items chosen and replaced, the status item present with its menu and removed, open, folder and save panels opened and cancelled from code, the hotkey handler run by the system's hotkey event, and the notification error outside a bundle. The AOT smoke app repeats the services in a NativeAOT image. Notifications that actually reach Notification Centre need a signed bundle and the user's permission, so they are exercised by hand, not by the suite.