Show screenshot window before OCR; fix signing cert setup

This commit is contained in:
2026-08-09 19:08:12 +08:00
parent e814e63c0f
commit c64c7296e2
5 changed files with 194 additions and 56 deletions
+20 -12
View File
@@ -1,8 +1,11 @@
# Kapture # Kapture
A native macOS menu bar app that captures a screen region and extracts the text A native macOS menu bar app that captures a screen region, shows the screenshot
from it using on-device Vision OCR. Press **⌘⇧S**, drag to select an area, and in a window, and extracts text from it on demand with on-device Vision OCR.
the recognized text is copied to your clipboard and shown in a floating window.
Press **⌘⇧S**, drag to select an area, and a window opens with your screenshot.
Click **Get Text** to recognize the text — it is copied to your clipboard and
shown alongside the image.
No network calls, no API keys — everything runs locally. No network calls, no API keys — everything runs locally.
@@ -10,9 +13,12 @@ No network calls, no API keys — everything runs locally.
- Global hotkey **⌘⇧S** works from any app - Global hotkey **⌘⇧S** works from any app
- Region selection overlay (all screens, Escape cancels) - Region selection overlay (all screens, Escape cancels)
- OCR for English, Chinese (simplified/traditional), Japanese, Korean - The screenshot appears in a window (Screenshot / Text tabs) instead of being
- Text is auto-copied to the clipboard and shown in a floating window instantly OCR'd
- Extract text from an existing image file via the menu bar icon - **Get Text** button runs on-device OCR (English, Chinese simplified/traditional,
Japanese, Korean) and auto-copies the result to the clipboard
- **Save PNG…** keeps the screenshot without any OCR
- Open an existing image file (or drag) via the menu bar icon
- Menu bar icon with permission status - Menu bar icon with permission status
## Build & run ## Build & run
@@ -31,10 +37,12 @@ swift run Kapture # runs from terminal (will appear in Dock)
## First launch ## First launch
1. Click the menu bar icon → **Grant Screen Recording Permission…** 1. Click the menu bar icon → **Grant Screen Recording Permission…**
(required so the app can capture the screen; text extraction from files (required so the app can capture the screen; opening image files works
works without it). without it).
2. Press **⌘⇧S**, drag to select a region, release. 2. Press **⌘⇧S**, drag to select a region, release — the screenshot opens in a
3. The extracted text lands on your clipboard and in the result window. window.
3. Click **Get Text** — the extracted text is copied to your clipboard and
shown in the Text tab.
Note: macOS may require re-granting Screen Recording permission after Note: macOS may require re-granting Screen Recording permission after
rebuilding the app, since the binary identity changes. rebuilding the app, since the binary identity changes.
@@ -53,7 +61,7 @@ swift run Kapture --test-ocr # renders text, OCRs it, prints PASS/FAIL
| Selection | Borderless `NSPanel` overlay per screen, dim + clear-cut rectangle | | Selection | Borderless `NSPanel` overlay per screen, dim + clear-cut rectangle |
| Capture | `CGWindowListCreateImage` (retina-aware) | | Capture | `CGWindowListCreateImage` (retina-aware) |
| OCR | Vision `VNRecognizeTextRequest` (accurate, multi-language) | | OCR | Vision `VNRecognizeTextRequest` (accurate, multi-language) |
| UI | SwiftUI `MenuBarExtra` + floating result window | | UI | SwiftUI `MenuBarExtra` + floating result window (Screenshot/Text tabs) |
## Layout ## Layout
@@ -66,5 +74,5 @@ Sources/Kapture/
OverlayView.swift selection overlay panel + drawing OverlayView.swift selection overlay panel + drawing
CaptureController.swift selection flow, screen capture, coordinate mapping CaptureController.swift selection flow, screen capture, coordinate mapping
OCRService.swift Vision text recognition OCRService.swift Vision text recognition
ResultWindow.swift floating result window + image-file extraction ResultWindow.swift floating result window (screenshot view, Get Text, Save PNG)
``` ```
+6 -11
View File
@@ -62,21 +62,16 @@ final class CaptureController: NSObject {
guard let image = ScreenCapture.capture(rect) else { guard let image = ScreenCapture.capture(rect) else {
isSelecting = false isSelecting = false
restorePreviousApp() restorePreviousApp()
ResultWindow.shared.show("Capture failed. Check Screen Recording permission.") Task { @MainActor in
ResultWindow.shared.show(image: ResultWindow.failureImage("Capture failed. Check Screen Recording permission."))
}
return return
} }
OCRService.shared.recognize(image) { [weak self] text in DispatchQueue.main.async { [weak self] in
DispatchQueue.main.async {
self?.isSelecting = false self?.isSelecting = false
self?.restorePreviousApp() self?.restorePreviousApp()
if text.isEmpty { Task { @MainActor in
ResultWindow.shared.show("No text found in the selected area.") ResultWindow.shared.show(image: image)
} else {
let pasteboard = NSPasteboard.general
pasteboard.clearContents()
pasteboard.setString(text, forType: .string)
ResultWindow.shared.show(text)
}
} }
} }
} }
+1 -1
View File
@@ -97,7 +97,7 @@ final class OverlayView: NSView {
let border = NSBezierPath(rect: sel) let border = NSBezierPath(rect: sel)
border.lineWidth = 1.5 border.lineWidth = 1.5
NSColor.systemOrange.setStroke() NSColor.controlAccentColor.setStroke()
border.stroke() border.stroke()
let label = "\(Int(sel.width)) × \(Int(sel.height))" let label = "\(Int(sel.width)) × \(Int(sel.height))"
+162 -27
View File
@@ -7,28 +7,25 @@ final class ResultWindow {
private var window: NSWindow? private var window: NSWindow?
func show(_ text: String) { @MainActor
func show(image: CGImage) {
window?.close() window?.close()
let hosting = NSHostingController(rootView: ResultView(text: text) { [weak self] in let model = CaptureModel(image: image)
let hosting = NSHostingController(rootView: ResultView(model: model) { [weak self] in
self?.window?.close() self?.window?.close()
}) })
let w = NSWindow(contentViewController: hosting) let w = NSWindow(contentViewController: hosting)
w.title = "Kapture — Extracted Text" w.title = "Kapture — Screenshot"
w.level = .floating w.level = .floating
w.styleMask = [.titled, .closable, .resizable] w.styleMask = [.titled, .closable, .resizable]
w.setContentSize(NSSize(width: 520, height: 380)) w.setContentSize(NSSize(width: 694, height: 520))
w.isReleasedWhenClosed = false w.isReleasedWhenClosed = false
w.center()
let mouse = NSEvent.mouseLocation
if let screen = NSScreen.screens.first(where: { $0.frame.contains(mouse) }) ?? NSScreen.main {
let x = min(max(mouse.x - screen.frame.minX - 260, screen.frame.minX + 8), screen.frame.maxX - 528)
let y = min(max(mouse.y - screen.frame.minY + 60, screen.frame.minY + 8), screen.frame.maxY - 388)
w.setFrameOrigin(NSPoint(x: x, y: y))
}
w.makeKeyAndOrderFront(nil) w.makeKeyAndOrderFront(nil)
window = w window = w
} }
@MainActor
func showOpenPanel() { func showOpenPanel() {
let panel = NSOpenPanel() let panel = NSOpenPanel()
panel.allowedContentTypes = [.image] panel.allowedContentTypes = [.image]
@@ -36,40 +33,178 @@ final class ResultWindow {
guard panel.runModal() == .OK, let url = panel.url else { return } guard panel.runModal() == .OK, let url = panel.url else { return }
guard let image = NSImage(contentsOf: url), guard let image = NSImage(contentsOf: url),
let cgImage = image.cgImage(forProposedRect: nil, context: nil, hints: nil) else { let cgImage = image.cgImage(forProposedRect: nil, context: nil, hints: nil) else {
show("Could not load the selected image.") self.show(image: Self.failureImage("Could not load the selected image."))
return return
} }
OCRService.shared.recognize(cgImage) { text in show(image: cgImage)
DispatchQueue.main.async { }
self.show(text.isEmpty ? "No text found in the image." : text)
static func failureImage(_ message: String) -> CGImage {
let size = NSSize(width: 800, height: 200)
guard let rep = NSBitmapImageRep(
bitmapDataPlanes: nil, pixelsWide: Int(size.width), pixelsHigh: Int(size.height),
bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false,
colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0
) else { return NSImage(size: size).cgImage(forProposedRect: nil, context: nil, hints: nil)! }
NSGraphicsContext.saveGraphicsState()
NSGraphicsContext.current = NSGraphicsContext(bitmapImageRep: rep)
NSColor.white.setFill()
NSRect(origin: .zero, size: size).fill()
let attrs: [NSAttributedString.Key: Any] = [
.font: NSFont.systemFont(ofSize: 30),
.foregroundColor: NSColor.systemRed,
]
message.draw(at: NSPoint(x: 40, y: 80), withAttributes: attrs)
NSGraphicsContext.restoreGraphicsState()
return rep.cgImage!
}
}
@MainActor
final class CaptureModel: ObservableObject {
let image: CGImage
@Published var mode: CaptureMode = .screenshot
@Published var extractedText: String?
@Published var isOCRRunning = false
@Published var statusMessage: String?
enum CaptureMode {
case screenshot
case text
}
init(image: CGImage) {
self.image = image
}
var displayText: String {
if isOCRRunning {
return "Recognizing text…"
}
return extractedText ?? "Text appears here — click “Get Text”."
}
func runOCR() {
guard !isOCRRunning else { return }
isOCRRunning = true
statusMessage = nil
OCRService.shared.recognize(image) { [weak self] text in
Task { @MainActor in
guard let self else { return }
self.isOCRRunning = false
self.mode = .text
if text.isEmpty {
self.extractedText = "No text found in the screenshot."
} else {
self.extractedText = text
let pasteboard = NSPasteboard.general
pasteboard.clearContents()
pasteboard.setString(text, forType: .string)
self.statusMessage = "Copied to clipboard."
} }
} }
} }
}
func copyText() {
guard let text = extractedText else { return }
let pasteboard = NSPasteboard.general
pasteboard.clearContents()
pasteboard.setString(text, forType: .string)
statusMessage = "Copied to clipboard."
}
func savePNG() {
let panel = NSSavePanel()
panel.allowedContentTypes = [.png]
panel.nameFieldStringValue = "Kapture-\(Self.timestamp()).png"
guard panel.runModal() == .OK, let url = panel.url else { return }
guard let dest = CGImageDestinationCreateWithURL(url as CFURL, UTType.png.identifier as CFString, 1, nil) else {
statusMessage = "Could not create PNG file."
return
}
CGImageDestinationAddImage(dest, image, nil)
if CGImageDestinationFinalize(dest) {
statusMessage = "Saved PNG."
} else {
statusMessage = "Could not save PNG."
}
}
private static func timestamp() -> String {
let formatter = DateFormatter()
formatter.dateFormat = "yyyyMMdd-HHmmss"
return formatter.string(from: Date())
}
} }
struct ResultView: View { struct ResultView: View {
let text: String @ObservedObject var model: CaptureModel
let onClose: () -> Void let onClose: () -> Void
var body: some View { var body: some View {
VStack(spacing: 12) { VStack(spacing: 10) {
TextEditor(text: .constant(text)) HStack {
Picker("", selection: $model.mode) {
Text("Screenshot").tag(CaptureModel.CaptureMode.screenshot)
Text("Text").tag(CaptureModel.CaptureMode.text)
if model.isOCRRunning {
Text("…").tag(CaptureModel.CaptureMode.text)
}
}
.pickerStyle(.segmented)
.fixedSize()
Spacer()
Button("Save PNG…") { model.savePNG() }
Button("Get Text") { model.runOCR() }
.disabled(model.isOCRRunning || model.extractedText != nil || model.mode == .text)
Button("Close") { onClose() }
}
.padding(.horizontal, 12)
.padding(.top, 10)
if model.mode == .screenshot {
Image(nsImage: NSImage(cgImage: model.image, size: .zero))
.resizable()
.aspectRatio(contentMode: .fit)
.border(Color.secondary.opacity(0.4))
.padding(.horizontal, 12)
} else {
ZStack(alignment: .bottomTrailing) {
ScrollView {
Text(model.displayText)
.font(.system(.body)) .font(.system(.body))
.textSelection(.enabled)
.frame(maxWidth: .infinity, alignment: .leading)
.padding(8)
}
.overlay( .overlay(
RoundedRectangle(cornerRadius: 6) RoundedRectangle(cornerRadius: 6)
.stroke(Color.secondary.opacity(0.4)) .stroke(Color.secondary.opacity(0.4))
) )
HStack {
Button("Copy to Clipboard") { HStack(spacing: 8) {
let pasteboard = NSPasteboard.general if let msg = model.statusMessage {
pasteboard.clearContents() Text(msg)
pasteboard.setString(text, forType: .string) .font(.caption)
.foregroundColor(.secondary)
} }
Spacer() Button("Copy Text") { model.copyText() }
Button("Close") { onClose() } }
.padding(8)
} }
} }
.padding()
.frame(minWidth: 480, minHeight: 340) if model.statusMessage != nil && model.mode == .screenshot {
Text(model.statusMessage!)
.font(.caption)
.foregroundColor(.secondary)
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.horizontal, 12)
}
}
.padding(.bottom, 10)
.frame(minWidth: 480, minHeight: 380)
} }
} }
+1 -1
View File
@@ -8,7 +8,7 @@ BIN=".build/release/Kapture"
APP="Kapture.app" APP="Kapture.app"
CERT="kapture-dev" CERT="kapture-dev"
if ! security find-identity -v -p codesigning 2>/dev/null | grep -q "\"$CERT\""; then if ! security find-identity -p codesigning 2>/dev/null | grep -q "kapture-dev"; then
echo "Creating self-signed code signing identity '$CERT' (one-time)..." echo "Creating self-signed code signing identity '$CERT' (one-time)..."
TMP=$(mktemp -d) TMP=$(mktemp -d)
trap 'rm -rf "$TMP"' EXIT trap 'rm -rf "$TMP"' EXIT