KMNSplitPDFWindowController.swift 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. //
  2. // KMNSplitPDFWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by 丁林圭 on 2024/10/24.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMNSplitPDFWindowController: KMNBaseWindowController {
  10. @IBOutlet var progress: NSProgressIndicator!
  11. @IBOutlet var titleLabel: NSTextField!
  12. @IBOutlet var methodLabel: NSTextField!
  13. @IBOutlet var averagePerRadio: ComponentRadio!
  14. @IBOutlet var averagePernInputNumber: ComponentInputNumber!
  15. @IBOutlet var averagePernLabel: NSTextField!
  16. @IBOutlet var averageSplitRadio: ComponentRadio!
  17. @IBOutlet var averageSplitInputNumber: ComponentInputNumber!
  18. @IBOutlet var averageSplitLabel: NSTextField!
  19. @IBOutlet var splitRangeRadio: ComponentRadio!
  20. @IBOutlet var splitRangeSelect: ComponentSelect!
  21. @IBOutlet var fileTitleLabel: NSTextField!
  22. @IBOutlet var fileNameBox: NSBox!
  23. @IBOutlet var fileNameLabel: NSTextField!
  24. @IBOutlet var precedeCheckBox: ComponentCheckBox!
  25. @IBOutlet var tagCheckBox: ComponentCheckBox!
  26. @IBOutlet var tagInput: ComponentInput!
  27. @IBOutlet var separatorCheckBox: ComponentCheckBox!
  28. @IBOutlet var separatorInput: ComponentInput!
  29. @IBOutlet var cancelButton: ComponentButton!
  30. @IBOutlet var splitButton: ComponentButton!
  31. @IBOutlet var cancelWidthButton:NSLayoutConstraint!
  32. @IBOutlet var splitWidthButton:NSLayoutConstraint!
  33. @IBOutlet var averagePerRadioWidthButton:NSLayoutConstraint!
  34. @IBOutlet var averageSplitRadioWidthButton:NSLayoutConstraint!
  35. @IBOutlet var splitRangeRadioWidthButton:NSLayoutConstraint!
  36. @IBOutlet var tagCheckWidthButton:NSLayoutConstraint!
  37. @IBOutlet var separatorCheckWidthButton:NSLayoutConstraint!
  38. private var orgDocument:CPDFDocument?
  39. private var selectionIndexPaths: Set<IndexPath> = []
  40. private func viewFileAtFinder(_ filePath: String) {
  41. let workspace = NSWorkspace.shared
  42. let url = URL(fileURLWithPath: filePath)
  43. workspace.activateFileViewerSelecting([url])
  44. }
  45. override func updateUIThemeColor() {
  46. super.updateUIThemeColor()
  47. window?.contentView?.wantsLayer = true
  48. window?.contentView?.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorBorder/popUp").cgColor
  49. averagePerRadio.reloadData()
  50. averageSplitRadio.reloadData()
  51. splitRangeRadio.reloadData()
  52. precedeCheckBox.reloadData()
  53. tagCheckBox.reloadData()
  54. separatorCheckBox.reloadData()
  55. cancelButton.reloadData()
  56. splitButton.reloadData()
  57. titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
  58. methodLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  59. fileTitleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  60. averagePernLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-field/colorText-filled-dis")
  61. fileNameBox.fillColor = ComponentLibrary.shared.getComponentColorFromKey("comp-field/colorText-filled-dis")
  62. updateButton()
  63. }
  64. override func updateUILanguage() {
  65. super.updateUILanguage()
  66. titleLabel.stringValue = KMLocalizedString("Split")
  67. methodLabel.stringValue = KMLocalizedString("Split Method")
  68. fileTitleLabel.stringValue = KMLocalizedString("File Name")
  69. averagePerRadio.properties.text = KMLocalizedString("Split by every")
  70. averageSplitRadio.properties.text = KMLocalizedString("Split averagely to")
  71. splitRangeRadio.properties.text = KMLocalizedString("Split by page range")
  72. averagePerRadio.reloadData()
  73. averageSplitRadio.reloadData()
  74. splitRangeRadio.reloadData()
  75. averagePernLabel.stringValue = KMLocalizedString("page(s)")
  76. precedeCheckBox.properties.text = KMLocalizedString("Keep the current file name in front of labels")
  77. tagCheckBox.properties.text = KMLocalizedString("Label")
  78. separatorCheckBox.properties.text = KMLocalizedString("Separator")
  79. precedeCheckBox.reloadData()
  80. tagCheckBox.reloadData()
  81. separatorCheckBox.reloadData()
  82. setUpPageSelctProperty()
  83. cancelButton.properties.buttonText = KMLocalizedString("Cancel")
  84. splitButton.properties.buttonText = KMLocalizedString("Split")
  85. cancelButton.reloadData()
  86. splitButton.reloadData()
  87. averagePerRadioWidthButton.constant = averagePerRadio.properties.propertyInfo.viewWidth
  88. averageSplitRadioWidthButton.constant = averageSplitRadio.properties.propertyInfo.viewWidth
  89. splitRangeRadioWidthButton.constant = splitRangeRadio.properties.propertyInfo.viewWidth
  90. tagCheckWidthButton.constant = tagCheckBox.properties.propertyInfo.viewWidth
  91. separatorCheckWidthButton.constant = separatorCheckBox.properties.propertyInfo.viewWidth
  92. cancelWidthButton.constant = cancelButton.properties.propertyInfo.viewWidth
  93. splitWidthButton.constant = splitButton.properties.propertyInfo.viewWidth
  94. let pageRangeSelectIndex = splitRangeSelect.indexOfSelect()
  95. if pageRangeSelectIndex == 0 {
  96. splitRangeSelect.properties.text = KMLocalizedString("Odd Pages Only")
  97. } else {
  98. if(orgDocument?.pageCount ?? 0 > 1 && pageRangeSelectIndex == 1) {
  99. splitRangeSelect.properties.text = KMLocalizedString("Even Pages Only")
  100. }
  101. }
  102. }
  103. override func initContentView() {
  104. super.initContentView()
  105. titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
  106. methodLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
  107. fileTitleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
  108. averagePerRadio.properties = ComponentCheckBoxProperty(size: .s,
  109. state: .normal,
  110. isDisabled: false,
  111. showhelp: false,
  112. text: KMLocalizedString("Split by every"),
  113. checkboxType: .normal)
  114. averagePernLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-regular")
  115. averageSplitRadio.properties = ComponentCheckBoxProperty(size: .s,
  116. state: .normal,
  117. isDisabled: false,
  118. showhelp: false,
  119. text: KMLocalizedString("Split averagely to"),
  120. checkboxType: .normal)
  121. averageSplitLabel.stringValue = KMLocalizedString("PDF files")
  122. averageSplitLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-regular")
  123. fileNameLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-regular")
  124. splitRangeRadio.properties = ComponentCheckBoxProperty(size: .s,
  125. state: .normal,
  126. isDisabled: false,
  127. showhelp: false,
  128. text: KMLocalizedString("Split by page range"),
  129. checkboxType: .selected)
  130. precedeCheckBox.properties = ComponentCheckBoxProperty(size: .s,
  131. state: .normal,
  132. isDisabled: false,
  133. showhelp: false,
  134. text: KMLocalizedString("Keep the current file name in front of labels"),
  135. checkboxType: .selected)
  136. tagCheckBox.properties = ComponentCheckBoxProperty(size: .s,
  137. state: .normal,
  138. isDisabled: false,
  139. showhelp: false,
  140. text: KMLocalizedString("Label"),
  141. checkboxType: .selected)
  142. separatorCheckBox.properties = ComponentCheckBoxProperty(size: .s,
  143. state: .normal,
  144. isDisabled: false,
  145. showhelp: false,
  146. text: KMLocalizedString("Separator"),
  147. checkboxType: .selected)
  148. averagePernInputNumber.properties = ComponentInputNumberProperty(alignment: .left,
  149. size: .s,
  150. state: .normal,
  151. isError: false,
  152. showErrorInfo: false,
  153. isDisabled: true,
  154. showPrefix: false,
  155. showSuffix: false,
  156. minSize: 1,
  157. maxSize: Int(orgDocument?.pageCount ?? 1),
  158. text:"1")
  159. averagePernInputNumber.delegate = self
  160. averageSplitInputNumber.properties = ComponentInputNumberProperty(alignment: .left,
  161. size: .s,
  162. state: .normal,
  163. isError: false,
  164. showErrorInfo: false,
  165. isDisabled: true,
  166. showPrefix: false,
  167. showSuffix: false,
  168. minSize: 1,
  169. maxSize: Int(orgDocument?.pageCount ?? 1),
  170. text: "1")
  171. averageSplitInputNumber.delegate = self
  172. let inputTagProperty: ComponentInputProperty = ComponentInputProperty(size: .s,
  173. state:.pressed ,
  174. isError: false,
  175. showPrefix: false,
  176. showSuffix: false,
  177. showClear: false,
  178. isDisabled: false,
  179. placeholder: "",
  180. text: "Part")
  181. tagInput.properties = inputTagProperty
  182. tagInput.delegate = self
  183. let inputSeparatorProperty: ComponentInputProperty = ComponentInputProperty(size: .s,
  184. state:.pressed ,
  185. isError: false,
  186. showPrefix: false,
  187. showSuffix: false,
  188. showClear: false,
  189. isDisabled: false,
  190. placeholder: "",
  191. text: "-")
  192. separatorInput.properties = inputSeparatorProperty
  193. separatorInput.delegate = self
  194. splitRangeSelect.properties = ComponentSelectProperties(size: .s,
  195. state: .normal,
  196. isDisabled: false,
  197. isError: false,
  198. leftIcon: false,
  199. placeholder: nil,
  200. errorText: nil,
  201. creatable: false,
  202. text: KMLocalizedString("Odd Pages Only", comment: ""))
  203. splitButton.properties = ComponentButtonProperty(type: .primary,
  204. size: .s,
  205. state: .normal,
  206. isDisable: false,
  207. buttonText: KMLocalizedString("Split"))
  208. splitButton.setTarget(self, action: #selector(splitButtonClicked(_ :)))
  209. cancelButton.properties = ComponentButtonProperty(type: .default_tertiary,
  210. size: .s,
  211. state: .normal,
  212. buttonText: KMLocalizedString("Cancel"))
  213. cancelButton.setTarget(self, action: #selector(cancelButtonClicked(_ :)))
  214. averagePerRadio.setTarget(self, action: #selector(splitMethodClicked(_:)))
  215. averageSplitRadio.setTarget(self, action: #selector(splitMethodClicked(_:)))
  216. splitRangeRadio.setTarget(self, action: #selector(splitMethodClicked(_:)))
  217. precedeCheckBox.setTarget(self, action: #selector(changeNameClick(_:)))
  218. tagCheckBox.setTarget(self, action: #selector(changeNameClick(_:)))
  219. separatorCheckBox.setTarget(self, action: #selector(changeNameClick(_:)))
  220. setUpPageSelctProperty()
  221. splitRangeSelect.delegate = self
  222. }
  223. func updateButton() {
  224. if(averageSplitRadio.properties.checkboxType == .selected) {
  225. averagePernLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-field/colorText-filled-dis")
  226. averageSplitLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
  227. } else if (splitRangeRadio.properties.checkboxType == .selected) {
  228. averagePernLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-field/colorText-filled-dis")
  229. averageSplitLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-field/colorText-filled-dis")
  230. } else if (averagePerRadio.properties.checkboxType == .selected) {
  231. averagePernLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
  232. averageSplitLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-field/colorText-filled-dis")
  233. }
  234. }
  235. private func setUpPageSelctProperty() {
  236. var menuItemString:[String] = [KMLocalizedString("Odd Pages Only"),
  237. KMLocalizedString("Even Pages Only"),
  238. KMLocalizedString("e.g. 1,3-5,10")]
  239. if orgDocument?.pageCount ?? 0 <= 1 {
  240. menuItemString.removeObject(KMLocalizedString("Even Pages Only"))
  241. }
  242. var menuItemArr: [ComponentMenuitemProperty] = []
  243. for language in menuItemString {
  244. let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  245. itemSelected: false,
  246. isDisabled: false,
  247. keyEquivalent: nil,
  248. text: language)
  249. menuItemArr.append(itemProperty)
  250. }
  251. splitRangeSelect.updateMenuItemsArr(menuItemArr)
  252. }
  253. override func windowDidLoad() {
  254. super.windowDidLoad()
  255. var isEditIndex:Int = 2
  256. if(orgDocument?.pageCount ?? 0 <= 1) {
  257. isEditIndex = 1
  258. }
  259. splitRangeSelect.selectItemAtIndex(isEditIndex)
  260. splitRangeSelect.properties.creatable = true
  261. splitRangeSelect.properties.text =
  262. KMNTools.parseIndexPathsSet(indexSets: selectionIndexPaths)
  263. splitRangeSelect.reloadData()
  264. updateFileName()
  265. progress.isHidden = true
  266. }
  267. func updateFileName() {
  268. let pdfName = orgDocument?.documentURL.deletingPathExtension().path.lastPathComponent ?? ""
  269. var splitPartString = ""
  270. var separatorString = ""
  271. if (tagCheckBox.properties.checkboxType == .selected) {
  272. splitPartString = tagInput.properties.text
  273. }
  274. if (separatorCheckBox.properties.checkboxType == .selected) {
  275. separatorString = separatorInput.properties.text
  276. }
  277. var partName = ""
  278. if (precedeCheckBox.properties.checkboxType == .selected) {
  279. if (separatorString.isEmpty == false) {
  280. if (partName.isEmpty == false) {
  281. partName.append(separatorString)
  282. } else {
  283. partName = separatorString
  284. }
  285. }
  286. if (splitPartString.isEmpty == false) {
  287. if (partName.isEmpty == false) {
  288. partName.append(splitPartString)
  289. } else {
  290. partName = splitPartString
  291. }
  292. }
  293. fileNameLabel.stringValue = String(format: "%@%@.pdf", pdfName, partName)
  294. } else {
  295. if (splitPartString.isEmpty == false) {
  296. if (partName.isEmpty == false) {
  297. partName.append(splitPartString)
  298. } else {
  299. partName = splitPartString
  300. }
  301. }
  302. if (separatorString.isEmpty == false) {
  303. if (partName.isEmpty == false) {
  304. partName.append(separatorString)
  305. } else {
  306. partName = separatorString
  307. }
  308. }
  309. fileNameLabel.stringValue = String(format: "%@%@.pdf", partName, pdfName)
  310. }
  311. }
  312. convenience init(_ document: CPDFDocument?, selectionIndexPaths: Set<IndexPath>) {
  313. self.init(windowNibName: "KMNSplitPDFWindowController")
  314. orgDocument = document
  315. self.selectionIndexPaths = selectionIndexPaths // 修正此行
  316. }
  317. convenience init(_ filePath: String,password:String?) {
  318. self.init(windowNibName: "KMNSplitPDFWindowController")
  319. let document = CPDFDocument.init(url: URL(fileURLWithPath: filePath))
  320. if password != nil {
  321. document?.unlock(withPassword: password as String?)
  322. }
  323. orgDocument = document
  324. selectionIndexPaths = [IndexPath(item: 0, section: 0)] // 修正此行
  325. }
  326. //MARK: - Action
  327. @objc func cancelButtonClicked(_ sender: NSView) {
  328. own_closeEndSheet()
  329. }
  330. @objc func splitButtonClicked(_ sender: NSView) {
  331. let pageRangeSelectIndex = splitRangeSelect.indexOfSelect()
  332. let fileAttribute = KMNFileAttribute()
  333. if(splitRangeRadio.properties.checkboxType == .selected) {
  334. fileAttribute.password = orgDocument?.password ?? ""
  335. fileAttribute.pdfDocument = orgDocument
  336. fileAttribute.filePath = orgDocument?.documentURL.path ?? ""
  337. if(pageRangeSelectIndex == 0) {
  338. fileAttribute.bAllPage = false
  339. fileAttribute.pagesType = .OnlyOdd
  340. } else {
  341. fileAttribute.bAllPage = false
  342. if(orgDocument?.pageCount ?? 0 <= 1) {
  343. fileAttribute.pagesType = .PagesString
  344. fileAttribute.pagesString = splitRangeSelect.properties.text ?? ""
  345. } else {
  346. if(pageRangeSelectIndex == 2) {
  347. fileAttribute.pagesType = .OnlyEven
  348. } else {
  349. fileAttribute.pagesType = .PagesString
  350. fileAttribute.pagesString = splitRangeSelect.properties.text ?? ""
  351. }
  352. }
  353. }
  354. if (fileAttribute.fetchSelectPages().isEmpty) {
  355. let alert = NSAlert()
  356. alert.alertStyle = .critical
  357. alert.messageText = String(format: "%@ %@", fileAttribute.filePath.lastPathComponent, KMLocalizedString("Invalid page range or the page number is out of range. Please try again."))
  358. alert.runModal()
  359. return
  360. }
  361. }
  362. let panel = NSOpenPanel()
  363. panel.canChooseFiles = false
  364. panel.canChooseDirectories = true
  365. panel.canCreateDirectories = true
  366. panel.beginSheetModal(for: self.window!) { result in
  367. if (result == .OK) {
  368. let outputURL = panel.urls.first
  369. var tFolderPath = outputURL!.path
  370. var index = 0
  371. let folderName = self.fileNameLabel.stringValue.deletingPathExtension.lastPathComponent
  372. var folderPath = "\(tFolderPath)" + "/" + "\(folderName)"
  373. //创建目录
  374. while FileManager.default.fileExists(atPath: folderPath) {
  375. index += 1
  376. folderPath = "\(tFolderPath)" + "/" + "\(folderName)_\(index)"
  377. }
  378. tFolderPath = folderPath
  379. try?FileManager.default.createDirectory(atPath: tFolderPath, withIntermediateDirectories: true)
  380. self.progress.isHidden = false
  381. self.cancelButton.properties.isDisabled = true
  382. self.splitButton.properties.isDisabled = true
  383. self.cancelButton.reloadData()
  384. self.splitButton.reloadData()
  385. self.progress.startAnimation(nil)
  386. if(self.averagePerRadio.properties.checkboxType == .selected) {
  387. let index = Int(self.averagePernInputNumber.properties.text ?? "1")
  388. DispatchQueue.global().async {
  389. let successArray = self.orgDocument?.splitByPagesWith(index ?? 1, folerPath: tFolderPath, fileName: folderName) ?? []
  390. DispatchQueue.main.async {
  391. self.progress.isHidden = true
  392. self.cancelButton.properties.isDisabled = false
  393. self.splitButton.properties.isDisabled = false
  394. self.cancelButton.reloadData()
  395. self.splitButton.reloadData()
  396. if (successArray.isEmpty == false) {
  397. let response = KMAlertTool.runModelForMainThread_r(message: KMLocalizedString("Splitting completed. Tap 'OK' to open the output folder."), buttons: [KMLocalizedString("OK")])
  398. if (response == .alertFirstButtonReturn ) {
  399. self.own_closeEndSheet()
  400. DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
  401. let filePath = successArray.first ?? ""
  402. self.viewFileAtFinder(filePath)
  403. }
  404. }
  405. } else {
  406. let response = KMAlertTool.runModelForMainThread_r(message: KMLocalizedString("Failed to split!"))
  407. if (response == .alertFirstButtonReturn) {
  408. self.own_closeEndSheet()
  409. }
  410. }
  411. }
  412. }
  413. } else if(self.averageSplitRadio.properties.checkboxType == .selected) {
  414. let dex = Int(self.averageSplitInputNumber.properties.text ?? "1")
  415. DispatchQueue.global().async {
  416. let successArray = self.orgDocument?.splitByFileWith(dex ?? 1, folerPath: tFolderPath, fileName: folderName) ?? []
  417. DispatchQueue.main.async {
  418. self.progress.isHidden = true
  419. self.cancelButton.properties.isDisabled = false
  420. self.splitButton.properties.isDisabled = false
  421. self.cancelButton.reloadData()
  422. self.splitButton.reloadData()
  423. if (successArray.isEmpty == false) {
  424. let response = KMAlertTool.runModelForMainThread_r(message: KMLocalizedString("Splitting completed. Tap 'OK' to open the output folder."), buttons: [KMLocalizedString("OK")])
  425. if (response == .alertFirstButtonReturn) {
  426. self.own_closeEndSheet()
  427. DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
  428. let filePath = successArray.first ?? ""
  429. self.viewFileAtFinder(filePath)
  430. }
  431. }
  432. }else{
  433. let response = KMAlertTool.runModelForMainThread_r(message: KMLocalizedString("Failed to split!"))
  434. if (response == .alertFirstButtonReturn) {
  435. self.own_closeEndSheet()
  436. }
  437. }
  438. }
  439. }
  440. } else {
  441. var isSuccessfully:Bool = false
  442. DispatchQueue.global().async { [self] in
  443. var pdfDocument1 = CPDFDocument()
  444. var pdfDocument2 = CPDFDocument()
  445. let pageCnt = self.orgDocument?.pageCount ?? 0
  446. let pages = fileAttribute.fetchSelectPages()
  447. for i in 0 ..< pageCnt {
  448. var isSelected = false
  449. for number in pages {
  450. if number == i+1 {
  451. isSelected = true
  452. if let page = self.orgDocument?.page(at: i) as? CPDFPage {
  453. pdfDocument1?.insertPageObject(page, at: pdfDocument1!.pageCount)
  454. }
  455. }
  456. }
  457. if (!isSelected) {
  458. if let page = self.orgDocument?.page(at: i) as? CPDFPage {
  459. pdfDocument2?.insertPageObject(page, at: pdfDocument2!.pageCount)
  460. }
  461. }
  462. }
  463. if (pdfDocument1!.pageCount > 0) {
  464. let tpath = "\(tFolderPath)/\(folderName) 1.pdf"
  465. isSuccessfully = pdfDocument1?.write(toFile:tpath) ?? false
  466. }
  467. if (pdfDocument2!.pageCount > 0) {
  468. let tPath = "\(tFolderPath)/\(folderName) 2.pdf"
  469. pdfDocument2?.write(toFile: tPath)
  470. }
  471. DispatchQueue.main.async {
  472. self.progress.isHidden = true
  473. self.cancelButton.properties.isDisabled = false
  474. self.splitButton.properties.isDisabled = false
  475. self.cancelButton.reloadData()
  476. self.splitButton.reloadData()
  477. if (isSuccessfully) {
  478. let response = KMAlertTool.runModelForMainThread_r(message: KMLocalizedString("Splitting completed. Tap 'OK' to open the output folder."), buttons: [KMLocalizedString("OK")])
  479. if (response == .alertFirstButtonReturn) {
  480. self.own_closeEndSheet()
  481. DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
  482. let filePath = "\(tFolderPath)/\(folderName) 1.pdf"
  483. self.viewFileAtFinder(filePath)
  484. }
  485. }
  486. } else {
  487. let response = KMAlertTool.runModelForMainThread_r(message: KMLocalizedString("Failed to split!"))
  488. if (response == .alertFirstButtonReturn) {
  489. self.own_closeEndSheet()
  490. }
  491. }
  492. }
  493. }
  494. }
  495. }
  496. }
  497. }
  498. @objc func changeNameClick(_ sender: NSView){
  499. updateFileName()
  500. }
  501. @objc func splitMethodClicked(_ sender: NSView){
  502. var oldSelectRadioBtn:ComponentRadio? = nil
  503. if sender == averagePerRadio {
  504. if(averageSplitRadio.properties.checkboxType == .selected) {
  505. oldSelectRadioBtn = averageSplitRadio
  506. averageSplitInputNumber.properties.isDisabled = true
  507. averageSplitLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-field/colorText-filled-dis")
  508. averageSplitInputNumber.reloadData()
  509. } else if (splitRangeRadio.properties.checkboxType == .selected){
  510. oldSelectRadioBtn = splitRangeRadio
  511. splitRangeSelect.properties.isDisabled = true
  512. splitRangeSelect.reloadData()
  513. }
  514. averagePernInputNumber.properties.isDisabled = false
  515. averagePernLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
  516. averagePernInputNumber.reloadData()
  517. } else if sender == averageSplitRadio {
  518. if(averagePerRadio.properties.checkboxType == .selected) {
  519. oldSelectRadioBtn = averagePerRadio
  520. averagePernInputNumber.properties.isDisabled = true
  521. averagePernLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-field/colorText-filled-dis")
  522. averagePernInputNumber.reloadData()
  523. } else if (splitRangeRadio.properties.checkboxType == .selected){
  524. oldSelectRadioBtn = splitRangeRadio
  525. splitRangeSelect.properties.isDisabled = true
  526. splitRangeSelect.reloadData()
  527. }
  528. averageSplitInputNumber.properties.isDisabled = false
  529. averageSplitLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
  530. averageSplitInputNumber.reloadData()
  531. } else if sender == splitRangeRadio {
  532. if(averagePerRadio.properties.checkboxType == .selected) {
  533. oldSelectRadioBtn = averagePerRadio
  534. averagePernInputNumber.properties.isDisabled = true
  535. averagePernLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-field/colorText-filled-dis")
  536. averagePernInputNumber.reloadData()
  537. } else if (averageSplitRadio.properties.checkboxType == .selected){
  538. oldSelectRadioBtn = averageSplitRadio
  539. averageSplitInputNumber.properties.isDisabled = true
  540. averageSplitLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-field/colorText-filled-dis")
  541. averageSplitInputNumber.reloadData()
  542. }
  543. splitRangeSelect.properties.isDisabled = false
  544. splitRangeSelect.reloadData()
  545. }
  546. if(oldSelectRadioBtn != nil) {
  547. oldSelectRadioBtn?.properties.checkboxType = .normal
  548. oldSelectRadioBtn?.reloadData()
  549. }
  550. }
  551. }
  552. //MARK: - ComponentInputNumberDelegate
  553. extension KMNSplitPDFWindowController: ComponentInputNumberDelegate {
  554. func componentInputNumberDidValueChanged(inputNumber: ComponentInputNumber?) {
  555. }
  556. }
  557. //MARK: - ComponentSelectDelegate
  558. extension KMNSplitPDFWindowController: ComponentSelectDelegate {
  559. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  560. if(view == splitRangeSelect) {
  561. let pageRangeSelectIndex = splitRangeSelect.indexOfSelect()
  562. var isEditIndex:Int = 2
  563. if(orgDocument?.pageCount ?? 0 <= 1) {
  564. isEditIndex = 1
  565. }
  566. if (pageRangeSelectIndex == isEditIndex) {
  567. splitRangeSelect.properties.text = ""
  568. splitRangeSelect.properties.placeholder = KMLocalizedString("e.g. 1,3-5,10")
  569. splitRangeSelect.properties.creatable = true
  570. } else {
  571. splitRangeSelect.properties.placeholder = ""
  572. splitRangeSelect.properties.creatable = false
  573. }
  574. splitRangeSelect.reloadData()
  575. }
  576. }
  577. }
  578. extension KMNSplitPDFWindowController: ComponentInputDelegate {
  579. func componentInputDidEndEditing(inputView: ComponentInput) {
  580. }
  581. func componentInputDidChanged(inputView: ComponentInput) {
  582. updateFileName()
  583. }
  584. }