Services¶
Nib provides access to macOS system services through properties on the App instance. Each service communicates with the Swift runtime over the same socket used for view rendering, using a synchronous request/response pattern.
All services are accessed as properties on app:
import nib
def main(app: nib.App):
# Battery
status = app.battery.status()
# Network
info = app.connectivity.status()
# Screen
screen = app.screen.info()
# Keychain
app.keychain.set("MyApp", "token", "secret123")
# Camera
devices = app.camera.devices()
# Launch at Login
app.launch_at_login.set(True)
# Permissions
status = app.permissions.check(nib.Permission.CAMERA)
# AppleScript
disk = app.applescript.run('tell application "Finder" to name of startup disk')
# Files, URLs and apps
app.workspace.open("https://codeberg.org/edo/nib")
# OCR
text = app.vision.text("~/Desktop/screenshot.png")
nib.run(main)
Available Services¶
| Service | Access | Description |
|---|---|---|
| Battery | app.battery |
Battery level, charging state, health, thermal state, and sleep prevention |
| Connectivity | app.connectivity |
Network connection status, type, Wi-Fi SSID, and interface info |
| Screen | app.screen |
Display resolution, brightness, dark mode, screenshots, and multi-display info |
| Keychain | app.keychain |
Secure credential storage using the macOS Keychain |
| Camera | app.camera |
Camera device listing, photo capture, and video frame streaming |
| LaunchAtLogin | app.launch_at_login |
Control whether the app starts automatically at user login |
| Permissions | app.permissions |
Check and request Camera, Microphone, and Notification permissions |
| AppleScript | app.applescript |
Run AppleScript and call its handlers to control other apps |
| Workspace | app.workspace |
Open files and URLs, list, launch and quit apps, file icons |
| Vision | app.vision |
Text recognition (OCR) and barcode reading on the device |
| Language | app.language |
Language detection, tokens, sentiment and sentence distance |
| Speech | app.speech |
Speak text with the system voices |
| Sound | app.sound |
Play alert sounds and audio files |
| Files | app.files |
Watch files and folders for changes |
app.pdf |
Text and page images of PDF files | |
| Spotlight | app.spotlight |
Search files with the Spotlight index |
| Shortcuts | app.shortcuts |
List and run the user's shortcuts |
| QuickLook | app.quicklook |
File thumbnails |
| Audio | app.audio |
Audio devices, default input and output, volume and mute |
| Maps | app.maps |
Place search and geocoding with Apple Maps |
How Services Work¶
Services extend the Service base class, which provides synchronous request/response communication with the Swift runtime. When you call a service method, the SDK:
- Sends a request message over the Unix socket to the Swift runtime
- Blocks the calling thread until a response arrives (default timeout: 10 seconds)
- Parses the response into a typed Python dataclass and returns it
This means service calls behave like regular synchronous function calls. No callbacks or await needed.
# Services are synchronous: call and use the result
status = app.battery.status()
print(f"Battery: {status.level}%")
Timeout
Every call waits app.service_timeout seconds for the runtime, 10 by default, then raises nib.ServiceTimeoutError. Every method also takes timeout= for one call; the ones that can take long on their own (AppleScript, Maps, Spotlight, Shortcuts, QuickLook, Vision, PDF text and rendering) have a longer default of their own. A call that may show a system prompt, such as camera.capture_photo() or a keychain read, is one to give more time.
Errors¶
Every exception nib raises derives from nib.NibError, so except nib.NibError covers the framework and leaves the program's own errors alone.
| Exception | When |
|---|---|
nib.NotConnectedError |
A service, app.settings, app.clipboard, UserDefaults or FilePicker was used before the runtime is connected, in App.__init__ or at module level. Call them from main() or a handler |
nib.ServiceError |
The runtime refused or could not complete the request. The message is the runtime's own. A request this runtime does not know raises it too |
nib.ServiceTimeoutError |
No reply within the timeout. A ServiceError and a TimeoutError |
nib.AppleScriptError, nib.AppleScriptPermissionError |
ServiceError subclasses for scripts, see AppleScript |
Related¶
- System Monitor: services example app
- Permissions