Sources/Orgstar/CaptureIntent.swift
51 lines · 2368 bytes
1import AppIntents
2import AppKit
3import OrgApp
4import OrgCore
5
6/// Shortcuts' "Capture to Orgstar": files text with a capture template without the capture
7/// window. Prompts in the template take their defaults.
8struct CaptureIntent: AppIntent {
9 static let title: LocalizedStringResource = "Capture to orgstar"
10 static let description = IntentDescription("Files text with one of your capture templates.")
11
12 @Parameter(title: "Text")
13 var text: String
14
15 @Parameter(title: "Template Key", description: "A template's key in capture.toml; the first template when empty.")
16 var template: String?
17
18 /// The app's models, once its window is up.
19 @MainActor static var environment: (workspace: WorkspaceModel, session: DocumentSession, clock: ClockModel, commands: AppCommands)?
20
21 struct Failure: Error, CustomLocalizedStringResourceConvertible {
22 let message: String
23 var localizedStringResource: LocalizedStringResource { "\(message)" }
24 }
25
26 @MainActor
27 func perform() async throws -> some IntentResult & ReturnsValue<String> & ProvidesDialog {
28 // Launched for the intent, the window sets the models up shortly.
29 guard ProAccess.shared.isUnlocked else { throw Failure(message: "Shortcuts actions need orgstar+") }
30 for _ in 0..<50 where Self.environment == nil { try? await Task.sleep(for: .milliseconds(100)) }
31 guard let (workspace, session, clock, commands) = Self.environment else { throw Failure(message: "orgstar isn't ready") }
32 let placed = session.capture(
33 text, templateKey: template, templates: CaptureTemplates.load().templates, workspace: workspace, clock: clock,
34 clipboard: NSPasteboard.general.string(forType: .string) ?? "", userName: NSFullUserName()
35 )
36 switch placed {
37 case .failure(let error):
38 throw Failure(message: error.message)
39 case .success(let chosen):
40 let message = "Captured to \(CaptureView.target(chosen))"
41 commands.show(message)
42 return .result(value: message, dialog: "\(message)")
43 }
44 }
45}
46
47struct OrgstarShortcuts: AppShortcutsProvider {
48 static var appShortcuts: [AppShortcut] {
49 AppShortcut(intent: CaptureIntent(), phrases: ["Capture to \(.applicationName)"], shortTitle: "Capture", systemImageName: "tray.and.arrow.down")
50 }
51}