Workspace¶
The Workspace service opens files and URLs, lists and controls running apps, and draws file icons, through NSWorkspace. Access it via app.workspace. Paths may be relative or start with ~.
app.workspace.open("https://codeberg.org/edo/nib")
app.workspace.reveal("~/Downloads/report.pdf")
front = app.workspace.frontmost_app()
Methods¶
| Method | Returns | Description |
|---|---|---|
open(target, app=None) |
bool |
Open a path or a URL with a scheme (https:, mailto:...). app is a bundle identifier, such as "com.apple.TextEdit", to open it with |
reveal(path) |
None |
Show a file or folder selected in a Finder window |
running_apps() |
list[RunningApp] |
The running apps that have a Dock presence |
frontmost_app() |
RunningApp \| None |
The app in front |
launch(bundle_id) |
None |
Launch an app, or bring it to the front if it is running |
quit(bundle_id, force=False) |
bool |
Ask an app to quit, or force it. True if a running app accepted; it may still ask to save first |
app_path(bundle_id) |
str \| None |
Where an app is installed |
icon(path, size=64) |
bytes |
The Finder icon of a file, folder or app as PNG, size points square |
A refused call, such as an unknown bundle identifier, raises nib.ServiceError.
RunningApp¶
| Field | Type | Description |
|---|---|---|
name |
str |
Localized name, such as "Safari" |
bundle_id |
str |
Bundle identifier |
pid |
int |
Process identifier |
active |
bool |
Whether it is in front |
hidden |
bool |
Whether its windows are hidden |
path |
str |
Path of its bundle |
Example¶
import nib
def main(app: nib.App):
app.title = "Apps"
app.icon = nib.SFSymbol("square.grid.2x2")
rows = nib.VStack(controls=[], spacing=6, padding=16)
def refresh():
rows.controls = [
nib.HStack(controls=[
nib.Image(src=app.workspace.icon(a.path, size=20), width=20, height=20),
nib.Text(a.name),
])
for a in app.workspace.running_apps()
]
app.on_appear = refresh
app.build(rows)
nib.run(main)
Related¶
- AppleScript: control what an app does once it runs
- QuickLook: file thumbnails instead of icons
- Services