Skip to content

Instantly share code, notes, and snippets.

@paultopia
Created January 25, 2019 21:08
Show Gist options
  • Select an option

  • Save paultopia/5dffa552e132ebbc149ce77dbfa52861 to your computer and use it in GitHub Desktop.

Select an option

Save paultopia/5dffa552e132ebbc149ce77dbfa52861 to your computer and use it in GitHub Desktop.
// more gui experiments
// see: https://stackoverflow.com/questions/54357317/nssavepanel-name-not-user-editable
import AppKit
import Foundation
import Quartz
NSApplication.shared.setActivationPolicy(.accessory)
func helperReadPDF(_ filename: String) -> String {
let pdata = try! NSData(contentsOfFile: filename) as Data
let pdf = PDFDocument(data: pdata)
return pdf!.string!
}
func selectFile() -> URL? {
let dialog = NSOpenPanel()
dialog.allowedFileTypes = ["pdf"]
guard dialog.runModal() == .OK else { return nil }
return dialog.url
}
func getSaveLocation() -> URL? {
let sa = NSSavePanel()
sa.nameFieldStringValue = "Untitled.txt"
sa.canCreateDirectories = true
sa.allowedFileTypes = ["txt"]
guard sa.runModal() == .OK else { return nil }
return sa.url
}
let file = selectFile()?.path ?? ""
let dest = getSaveLocation()!
try! helperReadPDF(file).write(to: dest, atomically: true, encoding: .utf8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment