mirror of
https://github.com/leonboe1/QuickDrop.git
synced 2026-11-18 20:36:04 +00:00
Add support for sharing plain text
This commit is contained in:
1 parent
a76983df36
commit
f800975607
5 files changed
+67
-39
No files matched your search
@@ -0,0 +1,32 @@
|
||||
//
|
||||
// ClipboardManager.swift
|
||||
// QuickDrop
|
||||
//
|
||||
// Created by Leon Böttger on 17.05.25.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import AppKit
|
||||
|
||||
class ClipboardManager {
|
||||
|
||||
static func saveClipboardToTempFile() -> URL? {
|
||||
let pasteboard = NSPasteboard.general
|
||||
let clipboardString = pasteboard.string(forType: .string) ?? ""
|
||||
|
||||
return saveTextToTempFile(text: clipboardString)
|
||||
}
|
||||
|
||||
static func saveTextToTempFile(text: String) -> URL? {
|
||||
let tempDirectory = FileManager.default.temporaryDirectory
|
||||
let fileURL = tempDirectory.appendingPathComponent("Clipboard".localized() + ".txt")
|
||||
|
||||
do {
|
||||
try text.write(to: fileURL, atomically: true, encoding: .utf8)
|
||||
return fileURL
|
||||
} catch {
|
||||
log("Failed to write file: \(error)")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,9 +13,12 @@
|
||||
SUBQUERY (
|
||||
$extensionItem.attachments,
|
||||
$attachment,
|
||||
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.file-url"
|
||||
OR ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.url"
|
||||
AND NOT (ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.folder")
|
||||
(
|
||||
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.file-url"
|
||||
OR ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.url"
|
||||
OR ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.text"
|
||||
)
|
||||
AND NOT (ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.folder")
|
||||
).@count == $extensionItem.attachments.@count
|
||||
).@count > 0</string>
|
||||
</dict>
|
||||
|
||||
@@ -46,27 +46,32 @@ class ShareViewController: NSViewController, ShareExtensionDelegate{
|
||||
override func loadView() {
|
||||
super.loadView()
|
||||
|
||||
// Insert code here to customize the view
|
||||
let item = self.extensionContext!.inputItems[0] as! NSExtensionItem
|
||||
if let attachments = item.attachments {
|
||||
for attachment in attachments as NSArray{
|
||||
let provider=attachment as! NSItemProvider
|
||||
provider.loadItem(forTypeIdentifier: kUTTypeURL as String) { data, err in
|
||||
if let urlData=data as? Data{
|
||||
if let url=URL(dataRepresentation: urlData, relativeTo: nil, isAbsolute: false){
|
||||
self.urls.append(url)
|
||||
if self.urls.count==attachments.count{
|
||||
DispatchQueue.main.async {
|
||||
self.urlsReady()
|
||||
}
|
||||
|
||||
if let text = item.attributedContentText?.string, attachments.isEmpty, let tempUrl = ClipboardManager.saveTextToTempFile(text: text) {
|
||||
self.urls.append(tempUrl)
|
||||
|
||||
DispatchQueue.main.async {
|
||||
self.urlsReady()
|
||||
}
|
||||
}
|
||||
else {
|
||||
for attachment in attachments {
|
||||
if attachment.hasItemConformingToTypeIdentifier(kUTTypeURL as String) {
|
||||
attachment.loadItem(forTypeIdentifier: kUTTypeURL as String, options: nil) { data, err in
|
||||
if let urlData = data as? Data,
|
||||
let url = URL(dataRepresentation: urlData, relativeTo: nil, isAbsolute: false) {
|
||||
self.urls.append(url)
|
||||
} else if let url = data as? NSURL {
|
||||
self.urls.append(url as URL)
|
||||
}
|
||||
}
|
||||
}else if let url=data as? NSURL{
|
||||
self.urls.append(url as URL)
|
||||
if self.urls.count==attachments.count{
|
||||
DispatchQueue.main.async {
|
||||
self.urlsReady()
|
||||
}
|
||||
}
|
||||
|
||||
if self.urls.count == attachments.count {
|
||||
DispatchQueue.main.async {
|
||||
self.urlsReady()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user