Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions TextGrabber2/Sources/App/App+Detection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import AppKit
// MARK: - Text Detecting

extension App {
func startDetection(userInitiated: Bool = false) async {
func startDetection(userInitiated: Bool = false, allowingFileAccess: Bool = true) async {
let newCount = NSPasteboard.general.changeCount
if silentDetectCount == newCount {
if userInitiated {
Expand All @@ -21,6 +21,7 @@ extension App {
return
}

silentDetectCount = nil
currentResult = nil
clearMenuItems()

Expand All @@ -32,11 +33,17 @@ extension App {
copyAllQuickItem.isHidden = true
copyAllMenuItem.isEnabled = false

let image = NSPasteboard.general.image?.cgImage
let containsFiles = NSPasteboard.general.containsFileReferences
let image = NSPasteboard.general.image(allowingFileAccess: allowingFileAccess)?.cgImage
Comment thread
cyanzhong marked this conversation as resolved.
let text = NSPasteboard.general.string

if let result = await Recognizer.detect(image: image, level: .accurate) {
updateResult(result, textCopied: text, in: mainMenu)

// Skipped or unreadable files must remain eligible for an explicit retry
if !containsFiles || image != nil {
silentDetectCount = newCount
}
} else {
Logger.log(.error, "Failed to detect text from image")
}
Expand All @@ -45,8 +52,7 @@ extension App {
Logger.log(.debug, "Presenting newly detected results")
presentMainMenu()
} else {
Logger.log(.debug, "Silently detected and cached")
silentDetectCount = newCount
Logger.log(.debug, "Silent detection finished")
}
}
}
Expand All @@ -71,6 +77,7 @@ private extension App {
return
}

clearMenuItems()
currentResult = resultData
translateItem.isEnabled = resultData.candidates.hasValue
quickLookItem.isEnabled = resultData.candidates.hasValue
Expand Down
2 changes: 1 addition & 1 deletion TextGrabber2/Sources/App/App+Observer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ extension App {
ContentFilters.processRules(for: pasteboard)

Task {
await self?.startDetection()
await self?.startDetection(allowingFileAccess: false)
}
}

Expand Down
2 changes: 1 addition & 1 deletion TextGrabber2/Sources/App/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class App: NSObject, NSApplicationDelegate {
var copyObserver: Task<Void, Never>?
var currentResult: Recognizer.ResultData?
var previewingFileURL: URL?
var silentDetectCount = 0
var silentDetectCount: Int?

lazy var statusItem: NSStatusItem = {
let item = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
Expand Down
15 changes: 15 additions & 0 deletions TextGrabber2/Sources/Extensions/NSPasteboard+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ extension NSPasteboard {
}

var image: NSImage? {
image(allowingFileAccess: true)
}

var containsFileReferences: Bool {
types?.contains {
$0 == .fileURL || $0.rawValue == "NSFilenamesPboardType"
} ?? false
}

func image(allowingFileAccess: Bool) -> NSImage? {
// Check types before requesting data or letting AppKit resolve file references
if !allowingFileAccess && containsFileReferences {
return nil
}

// Copied file
if let data = data(forType: .fileURL),
let string = String(data: data, encoding: .utf8),
Expand Down
Loading