|
@@ -158,7 +158,7 @@ extension NSDocumentController {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-// guard theURL.isFileURL else {
|
|
|
+// guard (theURL as AnyObject).isFileURL else {
|
|
|
// if showNotes == false {
|
|
|
// return SKDownloadController.shared.addDownload(for: theURL)
|
|
|
// }
|
|
@@ -166,35 +166,37 @@ extension NSDocumentController {
|
|
|
// }
|
|
|
|
|
|
var document: Any?
|
|
|
-// do {
|
|
|
-// let type = try self.type(forContentsOf: theURL)
|
|
|
-// if showNotes == false || SKNotesDocument.readableTypes.contains(type) {
|
|
|
-// document = try self.openDocument(withContentsOf: theURL, display: true)
|
|
|
-// } else if SKMainDocument.readableTypes.contains(type) {
|
|
|
-// for doc in self.documents {
|
|
|
-// if let sourceURL = (doc as? NSObject)?.value(forKey: "sourceFileURL") as? URL, sourceURL == theURL {
|
|
|
-// document = doc
|
|
|
-// break
|
|
|
-// }
|
|
|
-// }
|
|
|
-// if let existingDoc = document as? NSDocument {
|
|
|
-// existingDoc.showWindows()
|
|
|
-// } else {
|
|
|
-// var error: NSError?
|
|
|
-// var data: Data?
|
|
|
-//
|
|
|
-// if NSWorkspace.shared.type(type, conformsToType: SKPDFBundleDocumentType) {
|
|
|
-// if let skimFileURL = try FileManager.default.bundledFileURL(withExtension: "skim", inPDFBundleAt: theURL) {
|
|
|
-// data = try Data(contentsOf: skimFileURL)
|
|
|
-// }
|
|
|
-// } else {
|
|
|
+ do {
|
|
|
+ let type = try self.typeForContents(of: theURL as! URL)
|
|
|
+// if showNotes == false || KMNotesDocument.readableTypes.contains(type) {
|
|
|
+// document = try self.openDocument(withContentsOf: theURL as! URL, display: true)
|
|
|
+// } else
|
|
|
+ if KMMainDocument.readableTypes.contains(type) {
|
|
|
+ for doc in self.documents {
|
|
|
+ if let sourceURL = (doc as? NSObject)?.value(forKey: "sourceFileURL") as? URL, sourceURL == theURL as! URL {
|
|
|
+ document = doc
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if let existingDoc = document as? NSDocument {
|
|
|
+ existingDoc.showWindows()
|
|
|
+ } else {
|
|
|
+ var error: NSError?
|
|
|
+ var data: Data?
|
|
|
+
|
|
|
+ if NSWorkspace.shared.type(type, conformsToType: KMPDFBundleDocumentType) {
|
|
|
+ let skimFileURL = try self.bundledFileURLWithExtension("skim", inPDFBundleAtURL: theURL as! URL)
|
|
|
+ data = try Data(contentsOf: skimFileURL)
|
|
|
+
|
|
|
+ }
|
|
|
+// else {
|
|
|
// data = try SKNExtendedAttributeManager.shared().extendedAttributeNamed(SKIM_NOTES_KEY, atPath: theURL.path, traverseLink: true)
|
|
|
// }
|
|
|
-//
|
|
|
-// let newDocument = try makeUntitledDocument(ofType: SKNotesDocumentType)
|
|
|
+
|
|
|
+// let newDocument = try makeUntitledDocument(ofType: KMNotesDocumentType)
|
|
|
// newDocument.sourceFileURL = theURL
|
|
|
-//
|
|
|
-// if data == nil || newDocument.read(from: data ?? Data(), ofType: SKNotesDocumentType) {
|
|
|
+//
|
|
|
+// if data == nil || newDocument.read(from: data ?? Data(), ofType: KMNotesDocumentType) {
|
|
|
// self.addDocument(newDocument)
|
|
|
// newDocument.makeWindowControllers()
|
|
|
// newDocument.showWindows()
|
|
@@ -202,64 +204,96 @@ extension NSDocumentController {
|
|
|
// document = nil
|
|
|
// outError?.pointee = error
|
|
|
// }
|
|
|
-// }
|
|
|
-// }
|
|
|
-// } catch {
|
|
|
-// outError?.pointee = error as NSError
|
|
|
-// }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch {
|
|
|
+ outError?.pointee = error as NSError
|
|
|
+ }
|
|
|
|
|
|
return document
|
|
|
}
|
|
|
+
|
|
|
+ func bundledFileURLWithExtension(_ extension: String, inPDFBundleAtURL theURL: URL) throws -> URL {
|
|
|
+ let bundleContents = try FileManager.default.contentsOfDirectory(at: theURL, includingPropertiesForKeys: nil, options: [])
|
|
|
+ for fileURL in bundleContents {
|
|
|
+ if fileURL.pathExtension == `extension` {
|
|
|
+ return fileURL
|
|
|
+ }
|
|
|
+ }
|
|
|
+ throw NSError(domain: "YourDomain", code: 404, userInfo: [NSLocalizedDescriptionKey: "File with extension not found in PDF bundle"])
|
|
|
+ }
|
|
|
|
|
|
|
|
|
func openDocumentWithImageFromPasteboard(_ pboard: NSPasteboard, error outError: NSErrorPointer) -> Any? {
|
|
|
- var document: Any?
|
|
|
-// var data: Data?
|
|
|
-// var type: String?
|
|
|
-//
|
|
|
-// if pboard.canReadItem(withDataConformingToTypes: [NSPasteboard.PasteboardType.PDF]) {
|
|
|
-// pboard.types
|
|
|
-// data = pboard.data(forType: NSPasteboard.PasteboardType.PDF)
|
|
|
-// type = SKPDFDocumentType
|
|
|
-// } else if pboard.canReadItem(withDataConformingToTypes: [SKPasteboardTypePostScript]) {
|
|
|
-// pboard.types
|
|
|
-// data = pboard.data(forType: SKPasteboardTypePostScript)
|
|
|
-// type = isEncapsulatedPostScriptData(data) ? SKEncapsulatedPostScriptDocumentType : SKPostScriptDocumentType
|
|
|
-// } else if pboard.canReadItem(withDataConformingToTypes: [NSPasteboard.PasteboardType.tiff]) {
|
|
|
-// pboard.types
|
|
|
-// data = convertTIFFDataToPDF(pboard.data(forType: NSPasteboard.PasteboardType.tiff))
|
|
|
-// type = SKPDFDocumentType
|
|
|
-// } else {
|
|
|
-// let images = pboard.readObjects(forClasses: [NSImage.self], options: [:]) as? [NSImage]
|
|
|
-// let strings = pboard.readObjects(forClasses: [NSAttributedString.self], options: [:]) as? [NSAttributedString]
|
|
|
-// if let images = images, images.count > 0 {
|
|
|
-// data = convertTIFFDataToPDF(images[0].tiffRepresentation)
|
|
|
-// type = SKPDFDocumentType
|
|
|
-// } else if let strings = strings, strings.count > 0 {
|
|
|
-// data = convertStringsToPDF(strings)
|
|
|
-// type = SKPDFDocumentType
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
-// if let data = data, let type = type {
|
|
|
-// var error: NSError?
|
|
|
-// document = makeUntitledDocument(ofType: type, error: &error)
|
|
|
-//
|
|
|
-// if (document as? NSDocument)?.read(from: data, ofType: type, error: &error) ?? false {
|
|
|
-// addDocument(document as! NSDocument)
|
|
|
-// (document as! NSDocument).makeWindowControllers()
|
|
|
-// (document as! NSDocument).showWindows()
|
|
|
-// } else {
|
|
|
-// document = nil
|
|
|
-// if let outError = outError {
|
|
|
-// outError.pointee = error
|
|
|
-// }
|
|
|
-// }
|
|
|
-// } else if let outError = outError {
|
|
|
-// outError.pointee = NSError.readPasteboardError(withLocalizedDescription: NSLocalizedString("Unable to load data from clipboard", comment: "Error description"))
|
|
|
-// }
|
|
|
-//
|
|
|
+ var document: NSDocument?
|
|
|
+ var data: Data?
|
|
|
+ var type: String?
|
|
|
+
|
|
|
+ if pboard.canReadItem(withDataConformingToTypes: [NSPasteboard.PasteboardType.pdf.rawValue]) {
|
|
|
+ pboard.types
|
|
|
+ data = pboard.data(forType: NSPasteboard.PasteboardType.pdf)
|
|
|
+ type = KMPDFDocumentType
|
|
|
+ } else if pboard.canReadItem(withDataConformingToTypes: [KMEncapsulatedPostScriptDocumentType]) {
|
|
|
+ pboard.types
|
|
|
+ data = pboard.data(forType: NSPasteboard.PasteboardType(rawValue: KMEncapsulatedPostScriptDocumentType))
|
|
|
+ type = isEncapsulatedPostScriptData(data!) ? KMEncapsulatedPostScriptDocumentType : KMPostScriptDocumentType
|
|
|
+ } else if pboard.canReadItem(withDataConformingToTypes: [NSPasteboard.PasteboardType.tiff.rawValue]) {
|
|
|
+ pboard.types
|
|
|
+ data = convertTIFFDataToPDF(pboard.data(forType: NSPasteboard.PasteboardType.tiff)!)
|
|
|
+ type = KMPDFDocumentType
|
|
|
+ } else {
|
|
|
+ let images = pboard.readObjects(forClasses: [NSImage.self], options: [:]) as? [NSImage]
|
|
|
+ let strings = pboard.readObjects(forClasses: [NSAttributedString.self], options: [:]) as? [NSAttributedString]
|
|
|
+ if let images = images, images.count > 0 {
|
|
|
+ data = convertTIFFDataToPDF(images[0].tiffRepresentation!)
|
|
|
+ type = KMPDFDocumentType
|
|
|
+ } else if let strings = strings, strings.count > 0 {
|
|
|
+ data = KMOCTool.convertStringsToPDF(withString: strings) // convertStringsToPDF(strings!)
|
|
|
+ type = KMPDFDocumentType
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if let data = data, let type = type {
|
|
|
+ var error: NSError?
|
|
|
+
|
|
|
+ document = try?makeUntitledDocument(ofType: type)
|
|
|
+
|
|
|
+ if ((try?document?.read(from: data, ofType:type)) != nil) {
|
|
|
+ addDocument(document!)
|
|
|
+ document?.makeWindowControllers()
|
|
|
+ document?.showWindows()
|
|
|
+ } else {
|
|
|
+ document = nil
|
|
|
+ if let outError = outError {
|
|
|
+ outError.pointee = error
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if let outError = outError {
|
|
|
+ outError.pointee = NSError.readPasteboardError(withLocalizedDescription: NSLocalizedString("Unable to load data from clipboard", comment: "Error description"))
|
|
|
+ }
|
|
|
+
|
|
|
return document
|
|
|
}
|
|
|
+
|
|
|
+ func isEncapsulatedPostScriptData(_ data: Data) -> Bool {
|
|
|
+ let epsHeaderData = Data(bytes: [69, 80, 83, 70, 45], count: 5)
|
|
|
+ let rg: Range = (14..<20)
|
|
|
+ return (data.count >= 20 && (data.range(of: epsHeaderData, options: .anchored, in: rg) != nil))
|
|
|
+ }
|
|
|
+
|
|
|
+ func convertTIFFDataToPDF(_ tiffData: Data) -> Data? {
|
|
|
+ guard let imsrc = CGImageSourceCreateWithData(tiffData as CFData, [kCGImageSourceTypeIdentifierHint: kUTTypeTIFF] as CFDictionary), CGImageSourceGetCount(imsrc) > 0, let cgImage = CGImageSourceCreateImageAtIndex(imsrc, 0, nil) else { return nil }
|
|
|
+ let pdfData = NSMutableData(capacity: tiffData.count)
|
|
|
+ let consumer = CGDataConsumer(data: pdfData! as CFMutableData)!
|
|
|
+
|
|
|
+ var rect = CGRect(x: 0, y: 0, width: CGFloat(cgImage.width), height: CGFloat(cgImage.height))
|
|
|
+ let ctxt = CGContext(consumer: consumer, mediaBox: &rect, nil)
|
|
|
+ ctxt!.beginPDFPage(nil)
|
|
|
+ ctxt!.draw(cgImage, in: rect)
|
|
|
+ ctxt!.endPDFPage()
|
|
|
+ ctxt!.flush()
|
|
|
+
|
|
|
+ return pdfData as? Data
|
|
|
+ }
|
|
|
|
|
|
}
|