123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- import Cocoa
- let KMPDFDocumentType: String = "com.adobe.pdf"
- let KMPDFBundleDocumentType: String = "net.sourceforge.skim-app.pdfd"
- let KMNotesDocumentType: String = "net.sourceforge.skim-app.skimnotes"
- let KMNotesTextDocumentType: String = "public.plain-text"
- let KMNotesRTFDocumentType: String = "public.rtf"
- let KMNotesRTFDDocumentType: String = "com.apple.rtfd"
- let KMNotesFDFDocumentType: String = "com.adobe.fdf"
- let KMPostScriptDocumentType: String = "com.adobe.postscript"
- let KMEncapsulatedPostScriptDocumentType: String = "com.adobe.encapsulated-postscript"
- let KMDVIDocumentType: String = "org.tug.tex.dvi"
- let KMXDVDocumentType: String = "org.tug.tex.xdv"
- let KMFolderDocumentType: String = "public.folder"
- let KMDocumentSetupAliasKey: String = "_BDAlias"
- let KMDocumentSetupFileNameKey: String = "fileName"
- class KMDocumentController: NSDocumentController {
-
- func fetchUniquePath(_ originalPath: String) -> String {
- var path = originalPath
- let dManager = FileManager.default
- if !dManager.fileExists(atPath: path) {
- if path.extension.count < 1 {
- path = path.stringByAppendingPathExtension("pdf")
- }
- return path
- } else {
- let originalFullFileName = path.lastPathComponent
- let originalFileName = path.lastPathComponent.deletingPathExtension
- let originalExtension = path.extension
- let startIndex: Int = 0
- let endIndex: Int = startIndex + originalPath.count - originalFullFileName.count - 1
- let fileLocatePath = originalPath.substring(to: endIndex)
- var i = 1
- while (1 != 0) {
- var newName = String(format: "%@%ld", originalFileName, i)
- newName = newName.stringByAppendingPathExtension(originalExtension)
- let newPath = fileLocatePath.stringByAppendingPathComponent(newName)
- if !dManager.fileExists(atPath: newPath) {
- return newPath
- } else {
- i+=1
- continue
- }
- }
- }
- }
-
- func kNewDocumentTempSavePath(_ fileName: String) -> String {
- let searchPath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last
- let append1 = searchPath?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!)
- let append2 = append1!.stringByAppendingPathComponent(String(format: "%@", fileName))
- return append2
- }
-
- func savePdf(_ filePath: String) -> Void {
- let pdfData = try! Data(contentsOf: URL(fileURLWithPath: filePath))
- let document = try! self.makeUntitledDocument(ofType: KMPDFDocumentType) as NSDocument
- if ((try? document.read(from: pdfData, ofType: KMPDFDocumentType)) != nil) {
- self.addDocument(document)
- document.makeWindowControllers()
- document.showWindows()
- }
-
- }
-
-
-
- @IBAction func importFromFile(_ sender: Any) {
-
- }
-
- @IBAction func openBlankPage(_ sender: Any) {
- let fileName = String(format: "%@.pdf", NSLocalizedString("Untitled", comment: ""))
- let savePath = fetchUniquePath(kNewDocumentTempSavePath(fileName))
- let pdfDocument = CPDFDocument()
- pdfDocument?.insertPage(CGSize(width: 595, height: 842), at: 0)
- pdfDocument?.write(to: URL(fileURLWithPath: savePath))
- NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePath), display: true) { document, documentWasAlreadyOpen, error in
- if error != nil {
- NSApp.presentError(error!)
- return
- }
- if document is KMMainDocument {
- let newDocument = document
- (newDocument as! KMMainDocument).isNewCreated = false
- }
- }
- }
- @IBAction func newDocumentFromImage(_ sender: Any) {
- }
-
- @IBAction func importFromWebPage(_ sender: Any) {
- let windowController: KMURLToPDFWindowController = KMURLToPDFWindowController.init(windowNibName: NSNib.Name("KMURLToPDFWindowController"))
-
- windowController.beginSheetModalForWindow(NSWindow.currentWindow()) { filePath in
- if FileManager.default.fileExists(atPath: filePath) {
- NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: filePath), display: true) { document, documentWasAlreadyOpen, error in
- if error != nil {
- NSApp.presentError(error!)
- return
- }
- if document is KMMainDocument {
- (document as! KMMainDocument).isNewCreated = true
- }
- }
- }
- }
- }
-
- @IBAction func importFromScanner(_ sender: Any) {
-
- }
- }
- extension NSDocumentController {
- func openDocumentWithURLFromPasteboard(_ pboard: NSPasteboard, showNotes: Bool, outError: NSErrorPointer) -> Any? {
- guard let theURLs = NSURL.readURLs(from: pboard), let theURL = theURLs.first else {
- if showNotes == false {
- outError?.pointee = NSError.readPasteboardError(withLocalizedDescription: NSLocalizedString("Unable to load data from clipboard", comment: "Error description"))
- }
- return nil
- }
-
-
- var document: Any?
-
- return document
- }
-
- func openDocumentWithImageFromPasteboard(_ pboard: NSPasteboard, error outError: NSErrorPointer) -> Any? {
- var document: Any?
- return document
- }
- }
|