// // KMDocumentController.swift // PDF Master // // Created by wanjun on 2022/12/5. // 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() } } // MARK: Action @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 document is KMMainDocument { let newDocument = document (newDocument as! KMMainDocument).isNewCreated = false } } } @IBAction func newDocumentFromImage(_ sender: Any) { let openPanel = NSOpenPanel() openPanel.allowedFileTypes = KMBatchProcessingTableViewModel.supportedImageTypes() openPanel.allowsMultipleSelection = true openPanel.message = NSLocalizedString("Select images to create a new document. To select multiple files press cmd ⌘ button on keyboard and click on the target files one by one.", comment: "") openPanel.beginSheetModal(for: NSApp.mainWindow!) { response in let savePath = self.kNewDocumentTempSavePath("convertToPDF.pdf") if response == .OK { KMConvertPDFManager.convertImages(openPanel.urls, savePath: savePath) { success, errorDic in if !success || !FileManager.default.fileExists(atPath: savePath) { if FileManager.default.fileExists(atPath: savePath) { try? FileManager.default.removeItem(atPath: savePath) } let alert = NSAlert() alert.alertStyle = .critical alert.messageText = NSLocalizedString("Conversion Failed", comment: "") alert.addButton(withTitle: NSLocalizedString("OK", comment: "")) alert.runModal() return } self.savePdf(savePath) try? FileManager.default.removeItem(atPath: savePath) } } } } @IBAction func importFromWebPage(_ sender: Any) { let windowController: KMURLToPDFWindowController = KMURLToPDFWindowController.init(windowNibName: NSNib.Name("KMURLToPDFWindowController")) windowController.beginSheetModal(for: NSApp.mainWindow!) { filePath in if filePath != nil && FileManager.default.fileExists(atPath: filePath) { NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: filePath), display: true) { document, documentWasAlreadyOpen, error in if document is KMMainDocument { (document as! KMMainDocument).isNewCreated = true } } } } } @IBAction func importFromScanner(_ sender: Any) { } }