Fix clipboard preservation issue for images/screenshots

This commit is contained in:
Beingpax 2025-05-26 15:07:18 +05:45
parent 2484a92070
commit c0de5b3d89

View File

@ -12,26 +12,22 @@ class CursorPaster {
return return
} }
// Save the current pasteboard contents
let pasteboard = NSPasteboard.general let pasteboard = NSPasteboard.general
let oldContents = pasteboard.string(forType: .string) let savedItems = pasteboard.pasteboardItems ?? []
// Set the new text to paste
pasteboard.clearContents() pasteboard.clearContents()
pasteboard.setString(text, forType: .string) pasteboard.setString(text, forType: .string)
// Use the preferred paste method based on user settings
if UserDefaults.standard.bool(forKey: "UseAppleScriptPaste") { if UserDefaults.standard.bool(forKey: "UseAppleScriptPaste") {
_ = pasteUsingAppleScript() _ = pasteUsingAppleScript()
} else { } else {
pasteUsingCommandV() pasteUsingCommandV()
} }
// Restore the original pasteboard content
DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + pasteCompletionDelay) { DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + pasteCompletionDelay) {
pasteboard.clearContents() if !savedItems.isEmpty {
if let oldContents = oldContents { pasteboard.clearContents()
pasteboard.setString(oldContents, forType: .string) pasteboard.writeObjects(savedItems)
} }
} }
} }
@ -47,11 +43,9 @@ class CursorPaster {
if let scriptObject = NSAppleScript(source: script) { if let scriptObject = NSAppleScript(source: script) {
_ = scriptObject.executeAndReturnError(&error) _ = scriptObject.executeAndReturnError(&error)
if error != nil { if error != nil {
print("AppleScript paste failed: \(error?.description ?? "Unknown error")")
logger.notice("AppleScript paste failed with error: \(error?.description ?? "Unknown error")") logger.notice("AppleScript paste failed with error: \(error?.description ?? "Unknown error")")
return false return false
} }
logger.notice("AppleScript paste completed successfully")
return true return true
} }
return false return false
@ -73,8 +67,6 @@ class CursorPaster {
vDown?.post(tap: .cghidEventTap) vDown?.post(tap: .cghidEventTap)
vUp?.post(tap: .cghidEventTap) vUp?.post(tap: .cghidEventTap)
cmdUp?.post(tap: .cghidEventTap) cmdUp?.post(tap: .cghidEventTap)
logger.notice("Command+V paste completed")
} }
} }