11import Foundation
2- import SQLite3
3- #if canImport(AppKit)
2+ #if os(macOS)
43import AppKit
4+ import SQLite3
55#endif
66
77/// macOSのユーザ辞書データを取り出すためのヘルパー
@@ -12,7 +12,20 @@ import AppKit
1212/// sqlite3 -header -csv ~/Library/KeyboardServices/TextReplacements.db "SELECT ZSHORTCUT, ZPHRASE FROM ZTEXTREPLACEMENTENTRY"
1313/// ```
1414public enum SystemUserDictionaryHelper : Sendable {
15- #if canImport(AppKit)
15+ public struct Entry : Sendable {
16+ public let shortcut : String
17+ public let phrase : String
18+ }
19+
20+ public enum FetchError : Sendable , Error , Equatable {
21+ case unsupportedOperatingSystem
22+ case fileNotExist( String )
23+ case fileNotReadable( String )
24+ case failedToOpenDatabase( status: Int32 )
25+ case failedToPrepareStatement( status: Int32 )
26+ }
27+
28+ #if os(macOS)
1629 /// Delegate that allows the user to choose **only** a directory named "KeyboardServices".
1730 private final class KeyboardServicesDirectoryDelegate : NSObject , NSOpenSavePanelDelegate {
1831 private let allowedFolderName = " KeyboardServices "
@@ -37,37 +50,26 @@ public enum SystemUserDictionaryHelper: Sendable {
3750
3851 /// A shared delegate instance that remains alive for the lifetime of the open panel.
3952 @MainActor private static let keyboardServicesDelegate = KeyboardServicesDirectoryDelegate ( )
40- #endif
4153
42- public struct Entry : Sendable {
43- public let shortcut : String
44- public let phrase : String
45- }
46-
47- public enum FetchError : Sendable , Error {
48- case fileNotExist( String )
49- case fileNotReadable( String )
50- case failedToOpenDatabase( status: Int32 )
51- case failedToPrepareStatement( status: Int32 )
52- }
53-
54- @MainActor static func promptUserForTextReplacementDirectory( ) -> URL ? {
54+ @MainActor private static func promptUserForTextReplacementDirectory( ) -> URL ? {
5555 let panel = NSOpenPanel ( )
5656 panel. title = " システムのユーザ辞書ディレクトリ(KeyboardServices)を選択してください "
5757 panel. message = " システムのユーザ辞書ディレクトリ(KeyboardServices)を選択してください "
5858 panel. canChooseDirectories = true
5959 panel. canChooseFiles = false
6060 panel. allowsMultipleSelection = false
6161 panel. directoryURL = FileManager . default. homeDirectoryForCurrentUser. appendingPathComponent ( " Library/KeyboardServices " )
62- #if canImport(AppKit)
6362 panel. delegate = keyboardServicesDelegate
64- #endif
6563
6664 let response = panel. runModal ( )
6765 return response == . OK ? panel. url : nil
6866 }
67+ #endif
6968
7069 @MainActor public static func fetchEntries( ) throws ( FetchError) -> [ Entry ] {
70+ #if !os(macOS)
71+ throw . unsupportedOperatingSystem
72+ #else
7173 let userName = NSUserName ( )
7274 var dbPath = " /Users/ \( userName) /Library/KeyboardServices/TextReplacements.db "
7375 guard FileManager . default. fileExists ( atPath: dbPath) else {
@@ -134,5 +136,6 @@ public enum SystemUserDictionaryHelper: Sendable {
134136 }
135137
136138 return entries
139+ #endif
137140 }
138141}
0 commit comments