KMDocumentController.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // KMDocumentController.swift
  3. // PDF Master
  4. //
  5. // Created by wanjun on 2022/12/5.
  6. //
  7. import Cocoa
  8. let KMPDFDocumentType: String = "com.adobe.pdf"
  9. let KMPDFBundleDocumentType: String = "net.sourceforge.skim-app.pdfd"
  10. let KMNotesDocumentType: String = "net.sourceforge.skim-app.skimnotes"
  11. let KMNotesTextDocumentType: String = "public.plain-text"
  12. let KMNotesRTFDocumentType: String = "public.rtf"
  13. let KMNotesRTFDDocumentType: String = "com.apple.rtfd"
  14. let KMNotesFDFDocumentType: String = "com.adobe.fdf"
  15. let KMPostScriptDocumentType: String = "com.adobe.postscript"
  16. let KMEncapsulatedPostScriptDocumentType: String = "com.adobe.encapsulated-postscript"
  17. let KMDVIDocumentType: String = "org.tug.tex.dvi"
  18. let KMXDVDocumentType: String = "org.tug.tex.xdv"
  19. let KMFolderDocumentType: String = "public.folder"
  20. let KMDocumentSetupAliasKey: String = "_BDAlias"
  21. let KMDocumentSetupFileNameKey: String = "fileName"
  22. class KMDocumentController: NSDocumentController {
  23. func fetchUniquePath(_ originalPath: String) -> String {
  24. var path = originalPath
  25. let dManager = FileManager.default
  26. if !dManager.fileExists(atPath: path) {
  27. if path.extension.count < 1 {
  28. path = path.stringByAppendingPathExtension("pdf")
  29. }
  30. return path
  31. } else {
  32. let originalFullFileName = path.lastPathComponent
  33. let originalFileName = path.lastPathComponent.deletingPathExtension
  34. let originalExtension = path.extension
  35. let startIndex: Int = 0
  36. let endIndex: Int = startIndex + originalPath.count - originalFullFileName.count - 1
  37. let fileLocatePath = originalPath.substring(to: endIndex)
  38. var i = 1
  39. while (1 != 0) {
  40. var newName = String(format: "%@%ld", originalFileName, i)
  41. newName = newName.stringByAppendingPathExtension(originalExtension)
  42. let newPath = fileLocatePath.stringByAppendingPathComponent(newName)
  43. if !dManager.fileExists(atPath: newPath) {
  44. return newPath
  45. } else {
  46. i+=1
  47. continue
  48. }
  49. }
  50. }
  51. }
  52. func kNewDocumentTempSavePath(_ fileName: String) -> String {
  53. let searchPath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last
  54. let append1 = searchPath?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!)
  55. let append2 = append1!.stringByAppendingPathComponent(String(format: "%@", fileName))
  56. return append2
  57. }
  58. func savePdf(_ filePath: String) -> Void {
  59. let pdfData = try! Data(contentsOf: URL(fileURLWithPath: filePath))
  60. let document = try! self.makeUntitledDocument(ofType: KMPDFDocumentType) as NSDocument
  61. if ((try? document.read(from: pdfData, ofType: KMPDFDocumentType)) != nil) {
  62. self.addDocument(document)
  63. document.makeWindowControllers()
  64. document.showWindows()
  65. }
  66. }
  67. // MARK: Action
  68. @IBAction func importFromFile(_ sender: Any) {
  69. }
  70. @IBAction func openBlankPage(_ sender: Any) {
  71. let fileName = String(format: "%@.pdf", NSLocalizedString("Untitled", comment: ""))
  72. let savePath = fetchUniquePath(kNewDocumentTempSavePath(fileName))
  73. let pdfDocument = CPDFDocument()
  74. pdfDocument?.insertPage(CGSize(width: 595, height: 842), at: 0)
  75. pdfDocument?.write(to: URL(fileURLWithPath: savePath))
  76. NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePath), display: true) { document, documentWasAlreadyOpen, error in
  77. if document is KMMainDocument {
  78. let newDocument = document
  79. (newDocument as! KMMainDocument).isNewCreated = false
  80. }
  81. }
  82. }
  83. @IBAction func newDocumentFromImage(_ sender: Any) {
  84. let openPanel = NSOpenPanel()
  85. openPanel.allowedFileTypes = KMBatchProcessingTableViewModel.supportedImageTypes()
  86. openPanel.allowsMultipleSelection = true
  87. 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: "")
  88. openPanel.beginSheetModal(for: NSApp.mainWindow!) { response in
  89. let savePath = self.kNewDocumentTempSavePath("convertToPDF.pdf")
  90. if response == .OK {
  91. KMConvertPDFManager.convertImages(openPanel.urls, savePath: savePath) { success, errorDic in
  92. if !success || !FileManager.default.fileExists(atPath: savePath) {
  93. if FileManager.default.fileExists(atPath: savePath) {
  94. try? FileManager.default.removeItem(atPath: savePath)
  95. }
  96. let alert = NSAlert()
  97. alert.alertStyle = .critical
  98. alert.messageText = NSLocalizedString("Conversion Failed", comment: "")
  99. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  100. alert.runModal()
  101. return
  102. }
  103. self.savePdf(savePath)
  104. try? FileManager.default.removeItem(atPath: savePath)
  105. }
  106. }
  107. }
  108. }
  109. @IBAction func importFromWebPage(_ sender: Any) {
  110. let windowController: KMURLToPDFWindowController = KMURLToPDFWindowController.init(windowNibName: NSNib.Name("KMURLToPDFWindowController"))
  111. windowController.beginSheetModal(for: NSApp.mainWindow!) { filePath in
  112. if filePath != nil && FileManager.default.fileExists(atPath: filePath) {
  113. NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: filePath), display: true) { document, documentWasAlreadyOpen, error in
  114. if document is KMMainDocument {
  115. (document as! KMMainDocument).isNewCreated = true
  116. }
  117. }
  118. }
  119. }
  120. }
  121. @IBAction func importFromScanner(_ sender: Any) {
  122. }
  123. }