SplitWindowController.swift 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. //
  2. // SplitWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/11/14.
  6. //
  7. import Cocoa
  8. class SplitWindowController: KMNBaseWindowController {
  9. /*
  10. @protocol SplitDelegate <NSObject>
  11. @optional
  12. - (void)didFinshedSplitWithURL:(NSURL*)url;
  13. @end
  14. @property (nonatomic,assign) id<SplitDelegate> splitDelegate;
  15. */
  16. var fileAttribute: KMFileAttribute?
  17. //转圈提示
  18. @IBOutlet var progress: NSProgressIndicator!
  19. //分割后文档存储所在文件夹名称TextField容器View
  20. @IBOutlet var nameView: NSView!
  21. //分割后文档存储所在文件夹名称 TextField
  22. @IBOutlet var partFileNameLabel: NSTextField!
  23. @IBOutlet var originalFileButton: NSButton!
  24. //分割提示 Label
  25. @IBOutlet var splitLabel: NSTextField!
  26. @IBOutlet var splitMethodBox: NSBox!
  27. @IBOutlet var fileNameBox: NSBox!
  28. @IBOutlet var exampleLabel: NSTextField!
  29. //平均页数分割单位Pages Label
  30. @IBOutlet var pagesLabel: NSTextField!
  31. //平均分割为多少个PDF文件单位 Label
  32. @IBOutlet var filesLabel: NSTextField!
  33. //导出目录提示 Label
  34. @IBOutlet var labelingLabel: NSTextField!
  35. //按页分割页数输入 TextField
  36. @IBOutlet var byPageText: NSTextField!
  37. //按文档数量均分文档个数输入 TextField
  38. @IBOutlet var byFileText: NSTextField!
  39. //文档名称分割符输入 TextField
  40. @IBOutlet var separatorText: NSTextField!
  41. //新增文档命名拼接字段输入 TextField
  42. @IBOutlet var renameText: NSTextField!
  43. //按页分割 Button
  44. @IBOutlet var byPageButton: NSButton!
  45. //按分割文档个数分割 Button
  46. @IBOutlet var byFileButton: NSButton!
  47. //手动输入页分割 Button
  48. @IBOutlet var pageFromButton: NSButton!
  49. //取消 Button
  50. @IBOutlet var cancelButton: NSButton!
  51. //分割 Button
  52. @IBOutlet var splitButton: NSButton!
  53. //按文档个数分割,文档个数步进器
  54. @IBOutlet var byFileStepper: NSStepper!
  55. //按页均分分割,单个文档页数步进器
  56. @IBOutlet var byPageStepper: NSStepper!
  57. //新增文件夹名称拼接输入选择 Button
  58. @IBOutlet var renameButton: NSButton!
  59. //新增文件夹名字拼接 Button
  60. @IBOutlet var separatorButton: NSButton!
  61. //页面选择 ComboBox
  62. @IBOutlet var pageRangeComboBox: NSComboBox!
  63. @IBOutlet var splitMethodLabel: NSTextField!
  64. @IBOutlet var fileNameLabel: NSTextField!
  65. private var _fileURL: URL?
  66. private var _PDFDocument: CPDFDocument?
  67. private var _isSuccessfully = false
  68. //密码
  69. var password: String?
  70. //分割样式
  71. private var _splitType: Int = 0
  72. convenience init(document: CPDFDocument) {
  73. self.init(windowNibName: "SplitWindowController")
  74. self.fileAttribute = KMFileAttribute()
  75. self._fileURL = document.documentURL
  76. self._PDFDocument = document
  77. self.fileAttribute?.pdfDocument = document
  78. if IAPProductsManager.default().isAvailableAllFunction() {
  79. self._splitType = 3
  80. }
  81. }
  82. convenience init(fileURL documentPath: URL) {
  83. self.init(windowNibName: "SplitWindowController")
  84. self.fileAttribute = KMFileAttribute()
  85. self._fileURL = documentPath
  86. self._PDFDocument = CPDFDocument(url: documentPath)
  87. }
  88. override func windowDidLoad() {
  89. super.windowDidLoad()
  90. self._localizedLanguage()
  91. self._setInputRange()
  92. self._configuViews()
  93. }
  94. @IBAction func byPageStepperAction(_ sender: AnyObject) {
  95. self.byPageText.stringValue = String(format: "%ld", self.byPageStepper.integerValue)
  96. }
  97. @IBAction func byFileStepperAction(_ sender: AnyObject) {
  98. self.byFileText.stringValue = String(format: "%ld", self.byFileStepper.integerValue)
  99. }
  100. @IBAction func buttonItemClicked_Cancel(_ sender: AnyObject) {
  101. self.km_quick_endSheet()
  102. }
  103. @IBAction func buttonItemClicked_ByPage(_ sender: AnyObject) {
  104. self.byFileButton.state = .off
  105. self.pageFromButton.state = .off
  106. self.byPageText.isEditable = true
  107. self.pageRangeComboBox.isEditable = false
  108. self.pageRangeComboBox.isEditable = false
  109. self.window?.makeFirstResponder(self)
  110. self.pageRangeComboBox.isEnabled = false
  111. self.byFileText.isEditable = false
  112. self.byFileStepper.isEnabled = false
  113. self.byPageStepper.isEnabled = true
  114. self.fileAttribute?.bAllPage = true
  115. }
  116. @IBAction func buttonItemClicked_ByFile(_ sender: AnyObject) {
  117. self.byPageButton.state = .off
  118. self.pageFromButton.state = .off
  119. self.byPageText.isEditable = false
  120. self.pageRangeComboBox.isEditable = false
  121. self.pageRangeComboBox.isSelectable = false
  122. self.window?.makeFirstResponder(self)
  123. self.pageRangeComboBox.isEnabled = false
  124. self.byFileText.isEditable = true
  125. self.byFileStepper.isEnabled = true
  126. self.byPageStepper.isEnabled = false
  127. self.fileAttribute?.bAllPage = true
  128. }
  129. @IBAction func buttonItemClicked_ByRange(_ sender: AnyObject) {
  130. if !IAPProductsManager.default().isAvailableAllFunction() {
  131. pageFromButton.state = .off
  132. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  133. return;
  134. }
  135. self.byFileButton.state = .off
  136. self.byPageButton.state = .off
  137. self.byPageText.isEditable = false
  138. self.pageRangeComboBox.isEnabled = true
  139. if (self.pageRangeComboBox.indexOfSelectedItem == 2) {
  140. self.pageRangeComboBox.isSelectable = true
  141. self.pageRangeComboBox.isEditable = true
  142. self.pageRangeComboBox.stringValue = ""
  143. self.window?.makeFirstResponder(self.pageRangeComboBox)
  144. }
  145. self.byFileText.isEditable = false
  146. self.byFileStepper.isEnabled = false
  147. self.byPageStepper.isEnabled = false
  148. self.fileAttribute?.bAllPage = false
  149. }
  150. @IBAction func buttonItemClicked_Split(_ sender: AnyObject) {
  151. var fileAttribute: KMFileAttribute?
  152. if (self.pageFromButton.state == .on) {
  153. //选择page
  154. fileAttribute = self.fileAttribute
  155. fileAttribute?.filePath = self._fileURL?.path ?? ""
  156. fileAttribute?.bAllPage = false
  157. let pageCnt = self._PDFDocument?.pageCount ?? 0
  158. if self.pageRangeComboBox.stringValue == KMLocalizedString("Odd Pages Only") {
  159. var tPagesString = ""
  160. for i in 0 ..< pageCnt {
  161. if (i%2 == 0) {
  162. if (tPagesString.isEmpty) {
  163. tPagesString.append("\(i+1)")
  164. }else{
  165. tPagesString.append(",\(i+1)")
  166. }
  167. }
  168. }
  169. fileAttribute?.pagesString = tPagesString
  170. fileAttribute?.pagesType = .odd
  171. } else if self.pageRangeComboBox.stringValue == KMLocalizedString("Even Pages Only") {
  172. var tPagesString = ""
  173. for i in 0 ..< pageCnt {
  174. if (i%2 == 1) {
  175. if (tPagesString.isEmpty) {
  176. tPagesString.append("\(i+1)")
  177. }else{
  178. tPagesString.append(",\(i+1)")
  179. }
  180. }
  181. }
  182. fileAttribute?.pagesString = tPagesString
  183. fileAttribute?.pagesType = .even
  184. } else {
  185. fileAttribute?.pagesString = self.pageRangeComboBox.stringValue
  186. fileAttribute?.pagesType = .custom
  187. }
  188. if let data = fileAttribute?.fetchSelectPages().isEmpty, data {
  189. KMAlertTool.runModelForMainThread(message: KMLocalizedString("Invalid page range or the page number is out of range. Please try again."))
  190. return
  191. }
  192. }
  193. let panel = NSOpenPanel()
  194. panel.canChooseFiles = false
  195. panel.canChooseDirectories = true
  196. panel.canCreateDirectories = true
  197. panel.beginSheetModal(for: self.window!) { result in
  198. if (result == .OK) {
  199. let outputURL = panel.urls.first
  200. var tFolderPath = outputURL!.path
  201. var index = 0
  202. let folderName = self.partFileNameLabel.stringValue.deletingPathExtension.lastPathComponent
  203. var folderPath = "\(tFolderPath)" + "/" + "\(folderName)"
  204. //创建目录
  205. while FileManager.default.fileExists(atPath: folderPath) {
  206. index += 1
  207. folderPath = "\(tFolderPath)" + "/" + "\(folderName)_\(index)"
  208. }
  209. tFolderPath = folderPath
  210. try?FileManager.default.createDirectory(atPath: tFolderPath, withIntermediateDirectories: true)
  211. self.progress.isHidden = false
  212. self.cancelButton.isEnabled = false
  213. self.splitButton.isEnabled = false
  214. self.progress.startAnimation(nil)
  215. if (self.byPageButton.state == .on) {
  216. let index = self.byPageStepper.integerValue;
  217. DispatchQueue.global().async {
  218. let successArray = self._PDFDocument?.splitByPagesWith(index, folerPath: tFolderPath, fileName: folderName) ?? []
  219. DispatchQueue.main.async {
  220. self.progress.isHidden = true
  221. self.cancelButton.isEnabled = true
  222. self.splitButton.isEnabled = true
  223. if (successArray.isEmpty == false) {
  224. let response = KMAlertTool.runModelForMainThread_r(message: KMLocalizedString("Splitting completed. Tap 'OK' to open the output folder."), buttons: [KMLocalizedString("OK")])
  225. if (response == .alertFirstButtonReturn ) {
  226. self.km_quick_endSheet()
  227. DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
  228. let filePath = successArray.first ?? ""
  229. self._viewFileAtFinder(filePath)
  230. }
  231. }
  232. } else {
  233. let response = KMAlertTool.runModelForMainThread_r(message: KMLocalizedString("Failed to split!"))
  234. if (response == .alertFirstButtonReturn) {
  235. self.km_quick_endSheet()
  236. }
  237. }
  238. }
  239. }
  240. } else if (self.byFileButton.state == .on) {
  241. let dex = self.byFileStepper.integerValue
  242. DispatchQueue.global().async {
  243. // let successArray = self._PDFDocument?.splitByPagesWith(dex, folerPath: tFolderPath, fileName: folderName)
  244. let successArray = self._PDFDocument?.splitByFileWith(dex, folerPath: tFolderPath, fileName: folderName) ?? []
  245. DispatchQueue.main.async {
  246. self.progress.isHidden = true
  247. self.cancelButton.isEnabled = true
  248. self.splitButton.isEnabled = true
  249. if (successArray.isEmpty == false) {
  250. let response = KMAlertTool.runModelForMainThread_r(message: KMLocalizedString("Splitting completed. Tap 'OK' to open the output folder."), buttons: [KMLocalizedString("OK")])
  251. if (response == .alertFirstButtonReturn) {
  252. self.km_quick_endSheet()
  253. DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
  254. let filePath = successArray.first ?? ""
  255. self._viewFileAtFinder(filePath)
  256. }
  257. }
  258. }else{
  259. let response = KMAlertTool.runModelForMainThread_r(message: KMLocalizedString("Failed to split!"))
  260. if (response == .alertFirstButtonReturn) {
  261. self.km_quick_endSheet()
  262. }
  263. }
  264. }
  265. }
  266. } else {
  267. DispatchQueue.global().async { [self] in
  268. var pdfDocument1 = CPDFDocument()
  269. var pdfDocument2 = CPDFDocument()
  270. let pageCnt = self._PDFDocument?.pageCount ?? 0
  271. let pages = fileAttribute?.fetchSelectPages() ?? []
  272. for i in 0 ..< pageCnt {
  273. var isSelected = false
  274. for number in pages {
  275. if number == i+1 {
  276. isSelected = true
  277. // let page = self._PDFDocument?.page(at: i).copy()
  278. if let page = self._PDFDocument?.page(at: i) as? CPDFPage {
  279. pdfDocument1?.insertPageObject(page, at: pdfDocument1!.pageCount)
  280. }
  281. }
  282. }
  283. if (!isSelected) {
  284. // let page = self._PDFDocument?.page(at: i).copy()
  285. if let page = self._PDFDocument?.page(at: i) as? CPDFPage {
  286. pdfDocument2?.insertPageObject(page, at: pdfDocument2!.pageCount)
  287. }
  288. }
  289. }
  290. if (pdfDocument1!.pageCount > 0) {
  291. let tpath = "\(tFolderPath)/\(folderName) 1.pdf"
  292. _isSuccessfully = pdfDocument1?.write(toFile:tpath) ?? false
  293. }
  294. if (pdfDocument2!.pageCount > 0) {
  295. let tPath = "\(tFolderPath)/\(folderName) 2.pdf"
  296. pdfDocument2?.write(toFile: tPath)
  297. }
  298. DispatchQueue.main.async {
  299. self.progress.isHidden = true
  300. self.cancelButton.isEnabled = true
  301. self.splitButton.isEnabled = true
  302. if (_isSuccessfully) {
  303. // if ([_splitDelegate respondsToSelector:@selector(didFinshedSplitWithURL:)]) {
  304. // [_splitDelegate didFinshedSplitWithURL:_fileURL];
  305. // }
  306. let response = KMAlertTool.runModelForMainThread_r(message: KMLocalizedString("Splitting completed. Tap 'OK' to open the output folder."), buttons: [KMLocalizedString("OK")])
  307. if (response == .alertFirstButtonReturn) {
  308. self.km_quick_endSheet()
  309. DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
  310. let filePath = "\(tFolderPath)/\(folderName) 1.pdf"
  311. self._viewFileAtFinder(filePath)
  312. }
  313. }
  314. }else{
  315. let response = KMAlertTool.runModelForMainThread_r(message: KMLocalizedString("Failed to split!"))
  316. if (response == .alertFirstButtonReturn) {
  317. self.km_quick_endSheet()
  318. }
  319. }
  320. }
  321. }
  322. }
  323. }
  324. }
  325. }
  326. @IBAction func buttonItemClicked_OriginalName(_ sender: AnyObject) {
  327. self._updateSplitFileName()
  328. }
  329. @IBAction func buttonItemClicked_Rename(_ sender: AnyObject) {
  330. self._updateSplitFileName()
  331. }
  332. @IBAction func comboBoxClicked_PageRange(_ sender: NSComboBox) {
  333. if (sender.indexOfSelectedItem == 0) {
  334. //奇数页
  335. sender.isEditable = false
  336. sender.isSelectable = false
  337. self.window?.makeFirstResponder(self)
  338. } else if (sender.indexOfSelectedItem == 1){
  339. //偶数页
  340. sender.isEditable = false
  341. sender.isSelectable = false
  342. self.window?.makeFirstResponder(self)
  343. } else {
  344. //手动输入页数
  345. sender.isEditable = true
  346. sender.isSelectable = true
  347. self.pageRangeComboBox.stringValue = self.pageRangeComboBox.stringValue
  348. (sender.cell as? NSComboBoxCell)?.placeholderString = KMLocalizedString("e.g. 1,3-5,10")
  349. self.window?.makeFirstResponder(sender)
  350. }
  351. }
  352. }
  353. // MARK: - Private Methods
  354. extension SplitWindowController {
  355. private func _localizedLanguage() {
  356. self.splitMethodLabel.stringValue = KMLocalizedString("Split Methods")
  357. // self.splitLabel.stringValue = KMLocalizedString("Split Methods")
  358. self.pagesLabel.stringValue = KMLocalizedString("page(s)")
  359. self.filesLabel.stringValue = KMLocalizedString("PDF file(s)")
  360. // self.labelingLabel.stringValue = KMLocalizedString("File Naming")
  361. self.fileNameLabel.stringValue = KMLocalizedString("File Naming")
  362. self.exampleLabel.stringValue = KMLocalizedString("example")
  363. self.byPageButton.title = KMLocalizedString("Split by every")
  364. self.byFileButton.title = KMLocalizedString("Split averagely to")
  365. self.pageFromButton.title = KMLocalizedString("Split by page range")
  366. self.cancelButton.title = KMLocalizedString("Cancel")
  367. self.splitButton.title = KMLocalizedString("Split")
  368. self.originalFileButton.title = KMLocalizedString("Keep the current file name in front of labels")
  369. self.renameButton.title = KMLocalizedString("Label")
  370. self.separatorButton.title = KMLocalizedString("Separator")
  371. self.byPageButton.toolTip = KMLocalizedString("Split by every")
  372. self.byFileButton.toolTip = KMLocalizedString("Split averagely to")
  373. self.pageFromButton.toolTip = KMLocalizedString("Split by page range")
  374. self.originalFileButton.toolTip = KMLocalizedString("Keep the current file name in front of labels")
  375. }
  376. private func _setInputRange() {
  377. if let data = self._PDFDocument?.isLocked, data {
  378. if let url = self._fileURL {
  379. Self.checkPassword(url: url, type: .owner) { [weak self] success, pwd in
  380. if success {
  381. self?.password = pwd
  382. self?._PDFDocument?.unlock(withPassword: pwd)
  383. let pageNumber = self?._PDFDocument?.pageCount ?? 0
  384. let pdfName = self?._PDFDocument?.documentURL.deletingPathExtension().lastPathComponent ?? ""
  385. self?.byFileStepper.maxValue = Double(pageNumber)
  386. self?.byPageStepper.maxValue = Double(pageNumber)
  387. self?.partFileNameLabel.stringValue = String(format: "%@%@%@.pdf", pdfName, self?.separatorText.stringValue ?? "", self?.renameText.stringValue ?? "")
  388. }
  389. }
  390. }
  391. } else {
  392. let pageNumber = self._PDFDocument?.pageCount ?? 0
  393. let pdfName = self._PDFDocument?.documentURL.deletingPathExtension().path.lastPathComponent ?? ""
  394. self.byFileStepper.maxValue = Double(pageNumber)
  395. self.byPageStepper.maxValue = Double(pageNumber)
  396. if let data = self._PDFDocument?.documentURL.path.isEmpty, data == false {
  397. self.partFileNameLabel.stringValue = String(format: "%@%@%@", pdfName, self.separatorText.stringValue, "\(self.renameText.stringValue).pdf")
  398. } else {
  399. self.partFileNameLabel.stringValue = String(format: "%@%@%@", "Untitled", self.separatorText.stringValue, "\(self.renameText.stringValue).pdf")
  400. }
  401. }
  402. }
  403. private func _configuViews() {
  404. self.nameView.wantsLayer = true
  405. // _nameView.layer.backgroundColor = [KMAppearance KMColor_Layout_L1].CGColor;
  406. //初始化ComboBox
  407. self.pageRangeComboBox.removeAllItems()
  408. let pageRangeArray = [KMLocalizedString("Odd Pages Only"),
  409. KMLocalizedString("Even Pages Only"),
  410. KMLocalizedString("e.g. 1,3-5,10")]
  411. (self.pageRangeComboBox.cell as? NSComboBoxCell)?.placeholderString = KMLocalizedString("e.g. 1,3-5,10")
  412. self.pageRangeComboBox.addItems(withObjectValues: pageRangeArray)
  413. self.pageRangeComboBox.selectItem(at: 0)
  414. self.pageRangeComboBox.isSelectable = false
  415. self.pageRangeComboBox.isEditable = false
  416. self.byFileText.isEditable = false
  417. self.byFileStepper.isEnabled = false
  418. self.byPageText.isEditable = false
  419. self.byPageStepper.isEnabled = true
  420. if (self._splitType == 3) {
  421. self.pageRangeComboBox.selectItem(at: 2)
  422. let isEmpty = self.fileAttribute?.pagesString.isEmpty ?? false
  423. if (isEmpty == false) {
  424. self.pageRangeComboBox.stringValue = self.fileAttribute?.pagesString ?? ""
  425. self.byPageButton.state = .off
  426. self.pageFromButton.state = .on
  427. self.pageRangeComboBox.isEnabled = true
  428. } else {
  429. self.pageRangeComboBox.stringValue = ""
  430. self.byPageButton.state = .on
  431. self.byPageText.isEditable = true
  432. self.pageFromButton.state = .off
  433. self.pageRangeComboBox.isEnabled = false
  434. }
  435. self.pageRangeComboBox.isEditable = true
  436. self.pageRangeComboBox.isSelectable = true
  437. } else {
  438. self.pageRangeComboBox.stringValue = ""
  439. self.pageRangeComboBox.isEnabled = false
  440. self.byPageText.isEditable = true
  441. }
  442. self.byPageStepper.minValue = 1.0
  443. self.byFileStepper.minValue = 1.0
  444. self.byPageStepper.integerValue = 1
  445. self.byFileStepper.integerValue = 1
  446. self.renameText.stringValue = "part"
  447. self.separatorText.stringValue = "-"
  448. self.byPageText.stringValue = "1"
  449. self.byFileText.stringValue = "1"
  450. self.byPageText.formatter = TextFieldFormatter()
  451. self.byFileText.formatter = TextFieldFormatter()
  452. self.byPageText.delegate = self
  453. self.byFileText.delegate = self
  454. self.renameText.delegate = self
  455. self.separatorText.delegate = self
  456. self.progress.isHidden = true
  457. }
  458. private func _viewFileAtFinder(_ filePath: String) {
  459. let workspace = NSWorkspace.shared
  460. let url = URL(fileURLWithPath: filePath)
  461. workspace.activateFileViewerSelecting([url])
  462. }
  463. private func _updateSplitFileName() {
  464. let pdfName = self._PDFDocument?.documentURL.deletingPathExtension().path.lastPathComponent ?? ""
  465. var splitPartString = ""
  466. var separatorString = ""
  467. if (self.renameButton.state == .on) {
  468. splitPartString = self.renameText.stringValue
  469. }
  470. if (self.separatorButton.state == .on) {
  471. separatorString = self.separatorText.stringValue
  472. }
  473. var partName = ""
  474. if (self.originalFileButton.state == .on) {
  475. if (separatorString.isEmpty == false) {
  476. if (partName.isEmpty == false) {
  477. partName.append(separatorString)
  478. } else {
  479. partName = separatorString
  480. }
  481. }
  482. if (splitPartString.isEmpty == false) {
  483. if (partName.isEmpty == false) {
  484. partName.append(splitPartString)
  485. } else {
  486. partName = splitPartString
  487. }
  488. }
  489. self.partFileNameLabel.stringValue = String(format: "%@%@.pdf", pdfName, partName)
  490. } else {
  491. if (splitPartString.isEmpty == false) {
  492. if (partName.isEmpty == false) {
  493. partName.append(splitPartString)
  494. } else {
  495. partName = splitPartString
  496. }
  497. }
  498. if (separatorString.isEmpty == false) {
  499. if (partName.isEmpty == false) {
  500. partName.append(separatorString)
  501. } else {
  502. partName = separatorString
  503. }
  504. }
  505. self.partFileNameLabel.stringValue = String(format: "%@%@.pdf", partName, pdfName)
  506. }
  507. }
  508. }
  509. extension SplitWindowController: NSTextFieldDelegate {
  510. func controlTextDidChange(_ obj: Notification) {
  511. let textField = obj.object as? NSTextField
  512. if (self.byPageText.isEqual(to: textField)) {
  513. if let data = textField?.stringValue.isEmpty, data {
  514. self.byPageText.stringValue = "1"
  515. self.byPageStepper.integerValue = 1
  516. }else if let data = Int(textField!.stringValue), data <= 0 {
  517. self.byPageText.stringValue = "1"
  518. self.byPageStepper.integerValue = 1
  519. } else if (textField!.stringValue.stringToCGFloat() > self.byPageStepper.maxValue){
  520. self.byPageText.stringValue = String(format: "%.0f", self.byPageStepper.maxValue)
  521. self.byPageStepper.integerValue = Int(self.byPageStepper.maxValue)
  522. } else {
  523. self.byPageStepper.integerValue = Int(self.byPageText.stringValue) ?? 0
  524. }
  525. } else if (self.byFileText.isEqual(to: textField)){
  526. if let data = textField?.stringValue.isEmpty, data {
  527. self.byFileText.stringValue = "1"
  528. self.byFileStepper.integerValue = 1
  529. } else if let data = Int(textField!.stringValue), data <= 0 {
  530. self.byFileText.stringValue = "1"
  531. self.byFileStepper.integerValue = 1
  532. }else if (textField!.stringValue.stringToCGFloat() > self.byFileStepper.maxValue){
  533. self.byFileText.stringValue = String(format: "%.0f", self.byFileStepper.maxValue)
  534. self.byFileStepper.integerValue = Int(self.byFileStepper.maxValue)
  535. }else{
  536. self.byFileStepper.integerValue = Int(self.byFileText.stringValue) ?? 0
  537. }
  538. } else if (self.renameText.isEqual(to: textField) || self.separatorText.isEqual(to: textField )) {
  539. self._updateSplitFileName()
  540. }
  541. }
  542. }