I'd like to make a services provider in a SwiftUI application that request the user to select an option in a list.
Here is a sample working code (without asking option to the user). The App is based on a simple macOS SwiftUI application. Only the app.swift code is shown.
//
// ServicesTestApp.swift
// ServicesTest
//
import SwiftUI
import AppKit
@main
struct ServicesTestApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ notification: Notification) {
NSApp.servicesProvider = AppendSomethingServiceProvider()
}
}
class AppendSomethingServiceProvider: NSObject {
static let postfixes = ["one", "two", "three", "four"]
var selectedPostfix = 0
@objc func appendSomething(_ pasteboard: NSPasteboard, userData: String, error: NSErrorPointer) {
guard let entry = pasteboard.string(forType: .string) else {
return
}
// Ask for the string to append (one of AppendSomethingServiceProvider.postfixes strings).
let postfix = AppendSomethingServiceProvider.postfixes.indices.contains(selectedPostfix) ? " \(AppendSomethingServiceProvider.postfixes[selectedPostfix])" : ""
let output = entry + postfix
pasteboard.clearContents()
pasteboard.setString(output, forType: NSPasteboard.PasteboardType.string)
}
}
How could I show a dialog here?