Skip to content

Commit

Permalink
Merge pull request #2 from martin-braun/xywh
Browse files Browse the repository at this point in the history
allow unattended silent screenshot of given rect
  • Loading branch information
adam-zethraeus authored Jan 3, 2024
2 parents d5edf7f + ae67804 commit 9afd193
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ Takes file, stdin, or screencapture as input.
> cd macOCR
> swift build -c release
> .build/release/ocr -h
USAGE: ocr [--capture] [--stdin] [-i <i>]
USAGE: ocr [--capture] [-r <r>] [--stdin] [-i <i>]
OPTIONS:
-c, --capture Capture screenshot.
-r <r> Rectangle to unattendedly capture (-r x,y,w,h), needs --capture.
-s, --stdin Read stdin binary data.
-i <i> Path to input image.
-h, --help Show help information.
Expand Down
4 changes: 3 additions & 1 deletion Sources/ocr/CLIArgs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ struct CLIArgs {
@Flag(name: [.customShort("c"), .long], help: "Capture screenshot.")
var capture: Bool = false

@Option(name: [.customShort("r")], help: "Rectangle to unattendedly capture (-r x,y,w,h), needs --capture.")
var rectangle: String?

@Flag(name: [.customShort("s"), .long], help: "Read stdin binary data.")
var stdin: Bool = false

@Option(name: [.customShort("i")], help: "Path to input image.", completion: CompletionKind.file(extensions: ["gif", "png", "jpg", "webp", "tiff"]))
var input: String?


}
}
15 changes: 9 additions & 6 deletions Sources/ocr/OCRApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ class OCRApp {
func run() {
let args = CLIArgs.ocr.parseOrExit()
let image: CGImage?
switch (args.input, args.stdin, args.capture) {
case (.some(let path), false, false):
switch (args.input, args.stdin, args.capture, args.rectangle) {
case (.some(let path), false, false, .none):
image = imageFromURL(path.pathAsURL)
case (.none, true, false):
case (.none, true, false, .none):
image = imageFromStdIn()
case (.none, false, true):
case (.none, false, true, .some(let rect)):
let tempURL = URL(fileURLWithPath: "/tmp/ocr-\(UUID().uuidString).png")
image = imageFromURL(captureRegion(destination: tempURL))
image = imageFromURL(captureRegion(destination: tempURL, rect: rect))
try? FileManager.default.removeItem(at: tempURL)
case (.none, false, true, .none):
let tempURL = URL(fileURLWithPath: "/tmp/ocr-\(UUID().uuidString).png")
image = imageFromURL(captureRegion(destination: tempURL, rect: nil))
try? FileManager.default.removeItem(at: tempURL)
default:
CLIArgs.ocr.exit(withError: ArgumentParser.ValidationError("Input not properly specified."))
Expand Down Expand Up @@ -86,7 +90,6 @@ class OCRApp {
return image.cgImage(forProposedRect: &imageRect, context: nil, hints: nil)
}


func imageFromURL(_ inputURL: URL) -> CGImage? {
if let ciImage = CIImage(contentsOf: inputURL),
let cgImage = CIContext(options: nil)
Expand Down
4 changes: 2 additions & 2 deletions Sources/ocr/ScreenCapture.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Foundation

func captureRegion(destination: URL) -> URL {
func captureRegion(destination: URL, rect: String?) -> URL {
let destinationPath = destination.path as String

let task = Process()
task.launchPath = "/usr/sbin/screencapture"
task.arguments = ["-i", "-r", destinationPath]
task.arguments = rect != nil ? ["-R" + rect!, "-x", "-r", destinationPath] : ["-i", "-r", destinationPath]
task.launch()
task.waitUntilExit()

Expand Down

0 comments on commit 9afd193

Please sign in to comment.