|
@@ -4,38 +4,38 @@ import UniformTypeIdentifiers
|
|
|
|
|
|
|
|
enum PrintService {
|
|
enum PrintService {
|
|
|
static func print(urls: [URL], from window: NSWindow? = nil) {
|
|
static func print(urls: [URL], from window: NSWindow? = nil) {
|
|
|
- _ = window
|
|
|
|
|
guard !urls.isEmpty else { return }
|
|
guard !urls.isEmpty else { return }
|
|
|
|
|
|
|
|
|
|
+ let hostWindow = window ?? NSApp.keyWindow
|
|
|
for url in urls {
|
|
for url in urls {
|
|
|
let accessed = url.startAccessingSecurityScopedResource()
|
|
let accessed = url.startAccessingSecurityScopedResource()
|
|
|
defer {
|
|
defer {
|
|
|
if accessed { url.stopAccessingSecurityScopedResource() }
|
|
if accessed { url.stopAccessingSecurityScopedResource() }
|
|
|
}
|
|
}
|
|
|
- printFile(at: url)
|
|
|
|
|
|
|
+ printFile(at: url, from: hostWindow)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private static func printFile(at url: URL) {
|
|
|
|
|
|
|
+ private static func printFile(at url: URL, from window: NSWindow?) {
|
|
|
let type = contentType(for: url) ?? UTType(filenameExtension: url.pathExtension) ?? .data
|
|
let type = contentType(for: url) ?? UTType(filenameExtension: url.pathExtension) ?? .data
|
|
|
|
|
|
|
|
if type.conforms(to: .pdf), let document = PDFDocument(url: url) {
|
|
if type.conforms(to: .pdf), let document = PDFDocument(url: url) {
|
|
|
- printPDF(document, title: url.lastPathComponent)
|
|
|
|
|
|
|
+ printPDF(document, title: url.lastPathComponent, from: window)
|
|
|
} else if type.conforms(to: .image), let image = NSImage(contentsOf: url) {
|
|
} else if type.conforms(to: .image), let image = NSImage(contentsOf: url) {
|
|
|
- printImage(image, title: url.lastPathComponent)
|
|
|
|
|
|
|
+ printImage(image, title: url.lastPathComponent, from: window)
|
|
|
} else if let image = NSImage(contentsOf: url) {
|
|
} else if let image = NSImage(contentsOf: url) {
|
|
|
- printImage(image, title: url.lastPathComponent)
|
|
|
|
|
|
|
+ printImage(image, title: url.lastPathComponent, from: window)
|
|
|
} else if type.conforms(to: .plainText) || type.conforms(to: .text),
|
|
} else if type.conforms(to: .plainText) || type.conforms(to: .text),
|
|
|
let text = try? String(contentsOf: url, encoding: .utf8) {
|
|
let text = try? String(contentsOf: url, encoding: .utf8) {
|
|
|
- printText(text, title: url.lastPathComponent)
|
|
|
|
|
|
|
+ printText(text, title: url.lastPathComponent, from: window)
|
|
|
} else if type.conforms(to: .rtf),
|
|
} else if type.conforms(to: .rtf),
|
|
|
let data = try? Data(contentsOf: url),
|
|
let data = try? Data(contentsOf: url),
|
|
|
let attributed = NSAttributedString(rtf: data, documentAttributes: nil) {
|
|
let attributed = NSAttributedString(rtf: data, documentAttributes: nil) {
|
|
|
- printAttributedText(attributed, title: url.lastPathComponent)
|
|
|
|
|
|
|
+ printAttributedText(attributed, title: url.lastPathComponent, from: window)
|
|
|
} else if type.conforms(to: .html),
|
|
} else if type.conforms(to: .html),
|
|
|
let data = try? Data(contentsOf: url),
|
|
let data = try? Data(contentsOf: url),
|
|
|
let attributed = NSAttributedString(html: data, documentAttributes: nil) {
|
|
let attributed = NSAttributedString(html: data, documentAttributes: nil) {
|
|
|
- printAttributedText(attributed, title: url.lastPathComponent)
|
|
|
|
|
|
|
+ printAttributedText(attributed, title: url.lastPathComponent, from: window)
|
|
|
} else {
|
|
} else {
|
|
|
showUnsupportedAlert(for: url)
|
|
showUnsupportedAlert(for: url)
|
|
|
}
|
|
}
|
|
@@ -51,26 +51,26 @@ enum PrintService {
|
|
|
return UTType(filenameExtension: ext)
|
|
return UTType(filenameExtension: ext)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private static func printPDF(_ document: PDFDocument, title: String) {
|
|
|
|
|
|
|
+ private static func printPDF(_ document: PDFDocument, title: String, from window: NSWindow?) {
|
|
|
let printInfo = configuredPrintInfo()
|
|
let printInfo = configuredPrintInfo()
|
|
|
guard let operation = document.printOperation(
|
|
guard let operation = document.printOperation(
|
|
|
for: printInfo,
|
|
for: printInfo,
|
|
|
scalingMode: .pageScaleToFit,
|
|
scalingMode: .pageScaleToFit,
|
|
|
autoRotate: true
|
|
autoRotate: true
|
|
|
) else { return }
|
|
) else { return }
|
|
|
- runPrintOperation(operation, title: title)
|
|
|
|
|
|
|
+ runPrintOperation(operation, title: title, from: window)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private static func printImage(_ image: NSImage, title: String) {
|
|
|
|
|
|
|
+ private static func printImage(_ image: NSImage, title: String, from window: NSWindow?) {
|
|
|
guard let document = pdfDocument(from: image) else { return }
|
|
guard let document = pdfDocument(from: image) else { return }
|
|
|
- printPDF(document, title: title)
|
|
|
|
|
|
|
+ printPDF(document, title: title, from: window)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private static func printText(_ text: String, title: String) {
|
|
|
|
|
- printAttributedText(NSAttributedString(string: text), title: title)
|
|
|
|
|
|
|
+ private static func printText(_ text: String, title: String, from window: NSWindow?) {
|
|
|
|
|
+ printAttributedText(NSAttributedString(string: text), title: title, from: window)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private static func printAttributedText(_ text: NSAttributedString, title: String) {
|
|
|
|
|
|
|
+ private static func printAttributedText(_ text: NSAttributedString, title: String, from window: NSWindow?) {
|
|
|
let printInfo = configuredPrintInfo()
|
|
let printInfo = configuredPrintInfo()
|
|
|
let pageSize = printInfo.paperSize
|
|
let pageSize = printInfo.paperSize
|
|
|
let textView = NSTextView(frame: NSRect(origin: .zero, size: pageSize))
|
|
let textView = NSTextView(frame: NSRect(origin: .zero, size: pageSize))
|
|
@@ -78,10 +78,10 @@ enum PrintService {
|
|
|
textView.isEditable = false
|
|
textView.isEditable = false
|
|
|
|
|
|
|
|
let operation = NSPrintOperation(view: textView, printInfo: printInfo)
|
|
let operation = NSPrintOperation(view: textView, printInfo: printInfo)
|
|
|
- runPrintOperation(operation, title: title)
|
|
|
|
|
|
|
+ runPrintOperation(operation, title: title, from: window)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private static func runPrintOperation(_ operation: NSPrintOperation, title: String) {
|
|
|
|
|
|
|
+ private static func runPrintOperation(_ operation: NSPrintOperation, title: String, from window: NSWindow?) {
|
|
|
operation.showsPrintPanel = true
|
|
operation.showsPrintPanel = true
|
|
|
operation.showsProgressPanel = true
|
|
operation.showsProgressPanel = true
|
|
|
operation.jobTitle = title
|
|
operation.jobTitle = title
|
|
@@ -92,7 +92,12 @@ enum PrintService {
|
|
|
.showsOrientation,
|
|
.showsOrientation,
|
|
|
.showsScaling,
|
|
.showsScaling,
|
|
|
])
|
|
])
|
|
|
- operation.run()
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if let window {
|
|
|
|
|
+ operation.runModal(for: window, delegate: nil, didRun: nil, contextInfo: nil)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ operation.run()
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private static func configuredPrintInfo() -> NSPrintInfo {
|
|
private static func configuredPrintInfo() -> NSPrintInfo {
|