Sources/Orgstar/DevelopmentHooks.swift

51 lines · 3165 bytes

 1#if !APPSTORE
 2import AppKit
 3import OrgEditorAppKit
 4
 5/// Switches for checking the app from scripts, read from the environment:
 6/// - `ORGSTAR_DATA_DIR`: where the index and roots live (see `WorkspaceModel.defaultDirectory`).
 7/// - `ORGSTAR_OPEN`: a file to select at launch.
 8/// - `ORGSTAR_ROOT`: a folder to add at launch.
 9/// - `ORGSTAR_WINDOW`: a window to open at launch (`agenda`, `settings`); the snapshot renders it.
10/// - `ORGSTAR_COMMAND`: commands to run at launch, separated by commas (`app.import-emacs`); a
11///   sheet or prompt they open is what the snapshot renders.
12/// - `ORGSTAR_ANSWERS`: answers to the prompts those commands ask, in order, separated by commas.
13/// - `ORGSTAR_URL`: an `org-protocol://` link to handle at launch, as if opened from a browser.
14/// - `ORGSTAR_PASTEBOARD`: the name of a pasteboard to paste images and files from, instead of
15///   the clipboard.
16/// - `ORGSTAR_APPEARANCE`: `dark` or `light`, instead of the system's.
17/// - `ORGSTAR_SNAPSHOT`: after `ORGSTAR_SNAPSHOT_DELAY` seconds (default 3), render the main
18///   window to this PNG path and quit. Rendering from inside the app needs no screen-recording
19///   permission.
20enum DevelopmentHooks {
21    static var fileToOpen: String? { ProcessInfo.processInfo.environment["ORGSTAR_OPEN"] }
22    static var rootToAdd: String? { ProcessInfo.processInfo.environment["ORGSTAR_ROOT"] }
23    static var windowToOpen: String? { ProcessInfo.processInfo.environment["ORGSTAR_WINDOW"] }
24    static var commandToRun: String? { ProcessInfo.processInfo.environment["ORGSTAR_COMMAND"] }
25    static var urlToOpen: String? { ProcessInfo.processInfo.environment["ORGSTAR_URL"] }
26    static var answers: [String] { ProcessInfo.processInfo.environment["ORGSTAR_ANSWERS"]?.split(separator: ",").map(String.init) ?? [] }
27
28    @MainActor
29    static func scheduleSnapshot() {
30        let environment = ProcessInfo.processInfo.environment
31        if let name = environment["ORGSTAR_PASTEBOARD"] { OrgEditor.pasteboard = NSPasteboard(name: NSPasteboard.Name(name)) }
32        if let appearance = environment["ORGSTAR_APPEARANCE"] {
33            NSApp.appearance = NSAppearance(named: appearance == "dark" ? .darkAqua : .aqua)
34        }
35        guard let path = environment["ORGSTAR_SNAPSHOT"] else { return }
36        let delay = Double(environment["ORGSTAR_SNAPSHOT_DELAY"] ?? "") ?? 3
37        DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
38            let visible = NSApp.windows.filter(\.isVisible)
39            let chosen = windowToOpen.flatMap { id in visible.first { $0.identifier?.rawValue.lowercased().contains(id) == true } } ?? visible.first
40            let window = chosen?.attachedSheet ?? chosen
41            if let view = window?.contentView?.superview,
42               let bitmap = view.bitmapImageRepForCachingDisplay(in: view.bounds) {
43                view.cacheDisplay(in: view.bounds, to: bitmap)
44                try? bitmap.representation(using: .png, properties: [:])?.write(to: URL(fileURLWithPath: path))
45            }
46            if let chosen, let sheet = chosen.attachedSheet { chosen.endSheet(sheet) }
47            NSApp.terminate(nil)
48        }
49    }
50}
51#endif