SplitWindowController.swift 27 KB

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