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