Sources/Orgstar/DevelopmentHooks.swift
62 lines · 4339 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/// for Replace in Files and the renames, the two names, then the search runs.
14/// - `ORGSTAR_URL`: an `org-protocol://` link to handle at launch, as if opened from a browser.
15/// - `ORGSTAR_PASTEBOARD`: the name of a pasteboard to paste images and files from, instead of
16/// the clipboard.
17/// - `ORGSTAR_APPEARANCE`: `dark` or `light`, instead of the system's.
18/// - `ORGSTAR_SNAPSHOT`: after `ORGSTAR_SNAPSHOT_DELAY` seconds (default 3), render the main
19/// window to this PNG path and quit. Rendering from inside the app needs no screen-recording
20/// permission.
21/// The clock title key isn't read from the Keychain in such a run.
22enum DevelopmentHooks {
23 static var fileToOpen: String? { ProcessInfo.processInfo.environment["ORGSTAR_OPEN"] }
24 static var rootToAdd: String? { ProcessInfo.processInfo.environment["ORGSTAR_ROOT"] }
25 static var windowToOpen: String? { ProcessInfo.processInfo.environment["ORGSTAR_WINDOW"] }
26 static var commandToRun: String? { ProcessInfo.processInfo.environment["ORGSTAR_COMMAND"] }
27 static var urlToOpen: String? { ProcessInfo.processInfo.environment["ORGSTAR_URL"] }
28 /// `ORGSTAR_SNAPSHOT` is set: the run renders a window and quits.
29 static var snapshotting: Bool { ProcessInfo.processInfo.environment["ORGSTAR_SNAPSHOT"] != nil }
30 static var answers: [String] { ProcessInfo.processInfo.environment["ORGSTAR_ANSWERS"]?.split(separator: ",").map(String.init) ?? [] }
31
32 @MainActor
33 static func scheduleSnapshot() {
34 let environment = ProcessInfo.processInfo.environment
35 if let name = environment["ORGSTAR_PASTEBOARD"] { OrgEditor.pasteboard = NSPasteboard(name: NSPasteboard.Name(name)) }
36 if let appearance = environment["ORGSTAR_APPEARANCE"] {
37 NSApp.appearance = NSAppearance(named: appearance == "dark" ? .darkAqua : .aqua)
38 }
39 guard let path = environment["ORGSTAR_SNAPSHOT"] else { return }
40 let delay = Double(environment["ORGSTAR_SNAPSHOT_DELAY"] ?? "") ?? 3
41 DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
42 let visible = NSApp.windows.filter(\.isVisible)
43 let chosen = windowToOpen.flatMap { id in visible.first { $0.identifier?.rawValue.lowercased().contains(id) == true } } ?? visible.first
44 let window = chosen?.attachedSheet ?? chosen
45 // The window server's image of the app's own window, which needs no permission and,
46 // unlike cacheDisplay, draws the sidebar. Swift can't call it directly since macOS 15.
47 typealias Capture = @convention(c) (CGRect, CGWindowListOption, CGWindowID, CGWindowImageOption) -> Unmanaged<CGImage>?
48 if let window, let symbol = dlsym(UnsafeMutableRawPointer(bitPattern: -2), "CGWindowListCreateImage"),
49 let image = unsafeBitCast(symbol, to: Capture.self)(.null, .optionIncludingWindow, CGWindowID(window.windowNumber),
50 [.boundsIgnoreFraming, .bestResolution])?.takeRetainedValue() {
51 try? NSBitmapImageRep(cgImage: image).representation(using: .png, properties: [:])?.write(to: URL(fileURLWithPath: path))
52 } else if let view = window?.contentView?.superview,
53 let bitmap = view.bitmapImageRepForCachingDisplay(in: view.bounds) {
54 view.cacheDisplay(in: view.bounds, to: bitmap)
55 try? bitmap.representation(using: .png, properties: [:])?.write(to: URL(fileURLWithPath: path))
56 }
57 if let chosen, let sheet = chosen.attachedSheet { chosen.endSheet(sheet) }
58 NSApp.terminate(nil)
59 }
60 }
61}
62#endif