Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 37 additions & 34 deletions TextGrabber2/Resources/Services/en-US.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
[
{
"serviceName": "Look Up in Dictionary",
"displayName": "Look Up in Dictionary"
},
{
"serviceName": "Search With %WebSearchProvider@",
"displayName": "Search in Safari"
},
{
"serviceName": "SEARCH_WITH_SPOTLIGHT",
"displayName": "Search with Spotlight"
},
{
"serviceName": "Convert Text from Simplified to Traditional Chinese",
"displayName": "Convert to Traditional Chinese"
},
{
"serviceName": "Convert Text from Traditional to Simplified Chinese",
"displayName": "Convert to Simplified Chinese"
},
{
"serviceName": "New TextEdit Window Containing Selection",
"displayName": "New File in TextEdit"
},
{
"serviceName": "Make Sticky",
"displayName": "Make New Sticky Note"
},
{
"serviceName": "Summarize",
"displayName": "Summarize"
}
]
{
"$schema": "https://raw.githubusercontent.com/TextGrabber2-app/schemas/main/services.json",
"items": [
{
"serviceName": "Look Up in Dictionary",
"displayName": "Look Up in Dictionary"
},
{
"serviceName": "Search With %WebSearchProvider@",
"displayName": "Search in Safari"
},
{
"serviceName": "SEARCH_WITH_SPOTLIGHT",
"displayName": "Search with Spotlight"
},
{
"serviceName": "Convert Text from Simplified to Traditional Chinese",
"displayName": "Convert to Traditional Chinese"
},
{
"serviceName": "Convert Text from Traditional to Simplified Chinese",
"displayName": "Convert to Simplified Chinese"
},
{
"serviceName": "New TextEdit Window Containing Selection",
"displayName": "New File in TextEdit"
},
{
"serviceName": "Make Sticky",
"displayName": "Make New Sticky Note"
},
{
"serviceName": "Summarize",
"displayName": "Summarize"
}
]
}
71 changes: 37 additions & 34 deletions TextGrabber2/Resources/Services/zh-Hans.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
[
{
"serviceName": "Look Up in Dictionary",
"displayName": "在词典中查询"
},
{
"serviceName": "Search With %WebSearchProvider@",
"displayName": "在 Safari 中搜索"
},
{
"serviceName": "SEARCH_WITH_SPOTLIGHT",
"displayName": "用 Spotlight 搜索"
},
{
"serviceName": "Convert Text from Simplified to Traditional Chinese",
"displayName": "转换为繁体中文"
},
{
"serviceName": "Convert Text from Traditional to Simplified Chinese",
"displayName": "转换为简体中文"
},
{
"serviceName": "New TextEdit Window Containing Selection",
"displayName": "在 TextEdit 新建文件"
},
{
"serviceName": "Make Sticky",
"displayName": "创建新便笺条"
},
{
"serviceName": "Summarize",
"displayName": "摘要"
}
]
{
"$schema": "https://raw.githubusercontent.com/TextGrabber2-app/schemas/main/services.json",
"items": [
{
"serviceName": "Look Up in Dictionary",
"displayName": "在词典中查询"
},
{
"serviceName": "Search With %WebSearchProvider@",
"displayName": "在 Safari 中搜索"
},
{
"serviceName": "SEARCH_WITH_SPOTLIGHT",
"displayName": "用 Spotlight 搜索"
},
{
"serviceName": "Convert Text from Simplified to Traditional Chinese",
"displayName": "转换为繁体中文"
},
{
"serviceName": "Convert Text from Traditional to Simplified Chinese",
"displayName": "转换为简体中文"
},
{
"serviceName": "New TextEdit Window Containing Selection",
"displayName": "在 TextEdit 新建文件"
},
{
"serviceName": "Make Sticky",
"displayName": "创建新便笺条"
},
{
"serviceName": "Summarize",
"displayName": "摘要"
}
]
}
71 changes: 37 additions & 34 deletions TextGrabber2/Resources/Services/zh-Hant.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
[
{
"serviceName": "Look Up in Dictionary",
"displayName": "在辭典中查詢"
},
{
"serviceName": "Search With %WebSearchProvider@",
"displayName": "在 Safari 中搜尋"
},
{
"serviceName": "SEARCH_WITH_SPOTLIGHT",
"displayName": "用 Spotlight 搜尋"
},
{
"serviceName": "Convert Text from Simplified to Traditional Chinese",
"displayName": "轉換為繁體中文"
},
{
"serviceName": "Convert Text from Traditional to Simplified Chinese",
"displayName": "轉換為簡體中文"
},
{
"serviceName": "New TextEdit Window Containing Selection",
"displayName": "在 TextEdit 新建檔案"
},
{
"serviceName": "Make Sticky",
"displayName": "建立新便條紙"
},
{
"serviceName": "Summarize",
"displayName": "摘要"
}
]
{
"$schema": "https://raw.githubusercontent.com/TextGrabber2-app/schemas/main/services.json",
"items": [
{
"serviceName": "Look Up in Dictionary",
"displayName": "在辭典中查詢"
},
{
"serviceName": "Search With %WebSearchProvider@",
"displayName": "在 Safari 中搜尋"
},
{
"serviceName": "SEARCH_WITH_SPOTLIGHT",
"displayName": "用 Spotlight 搜尋"
},
{
"serviceName": "Convert Text from Simplified to Traditional Chinese",
"displayName": "轉換為繁體中文"
},
{
"serviceName": "Convert Text from Traditional to Simplified Chinese",
"displayName": "轉換為簡體中文"
},
{
"serviceName": "New TextEdit Window Containing Selection",
"displayName": "在 TextEdit 新建檔案"
},
{
"serviceName": "Make Sticky",
"displayName": "建立新便條紙"
},
{
"serviceName": "Summarize",
"displayName": "摘要"
}
]
}
46 changes: 46 additions & 0 deletions TextGrabber2/Sources/ConfigurationFile.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// ConfigurationFile.swift
// TextGrabber2
//
// Created by cyan on 2026/8/27.
//

import Foundation

enum SchemaURLs {
static let contentFilters = "https://raw.githubusercontent.com/TextGrabber2-app/schemas/main/content-filters.json"
static let keyBindings = "https://raw.githubusercontent.com/TextGrabber2-app/schemas/main/key-bindings.json"
static let services = "https://raw.githubusercontent.com/TextGrabber2-app/schemas/main/services.json"
}

struct ConfigurationFile<Item: Decodable>: Decodable {
let items: [Item]

init(from decoder: Decoder) throws {
if let items = try? decoder.singleValueContainer().decode([Item].self) {
self.items = items
return
}

let container = try decoder.container(keyedBy: CodingKeys.self)
items = try container.decode([Item].self, forKey: .items)
}

static func migrate(at fileURL: URL, schemaURL: String) throws {
let data = try Data(contentsOf: fileURL)
guard try JSONSerialization.jsonObject(with: data) is [Any],
let json = String(data: data, encoding: .utf8) else {
return
}
Comment thread
cyanzhong marked this conversation as resolved.

let items = json.trimmingCharacters(in: .whitespacesAndNewlines)
.replacingOccurrences(of: "\n", with: "\n ")
let migratedJSON = "{\n \"$schema\": \"\(schemaURL)\",\n \"items\": \(items)\n}\n"
let migratedData = Data(migratedJSON.utf8)
try migratedData.write(to: fileURL, options: .atomic)
}

private enum CodingKeys: String, CodingKey {
case items
}
}
16 changes: 13 additions & 3 deletions TextGrabber2/Sources/ContentFilters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,22 @@ enum ContentFilters {
}

static func initialize() {
guard !FileManager.default.fileExists(atPath: fileURL.path(percentEncoded: false)) else {
if FileManager.default.fileExists(atPath: fileURL.path(percentEncoded: false)) {
do {
try ConfigurationFile<Rule>.migrate(at: fileURL, schemaURL: SchemaURLs.contentFilters)
} catch {
Logger.log(.error, "Failed to migrate \(Constants.fileName): \(error)")
}

return Logger.log(.info, "\(Constants.fileName) was created before")
}
Comment thread
cyanzhong marked this conversation as resolved.

do {
try "[]\n".write(to: fileURL, atomically: true, encoding: .utf8)
try "{\n \"$schema\": \"\(SchemaURLs.contentFilters)\",\n \"items\": []\n}\n".write(
to: fileURL,
atomically: true,
encoding: .utf8
)
} catch {
Logger.log(.error, "\(error)")
}
Expand Down Expand Up @@ -152,7 +162,7 @@ private extension ContentFilters {
return []
}

guard let rules = try? JSONDecoder().decode([Rule].self, from: data) else {
guard let rules = try? JSONDecoder().decode(ConfigurationFile<Rule>.self, from: data).items else {
Logger.log(.error, "Failed to decode the file")
return []
}
Expand Down
16 changes: 13 additions & 3 deletions TextGrabber2/Sources/KeyBindings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,22 @@ enum KeyBindings {
}

static func initialize() {
guard !FileManager.default.fileExists(atPath: fileURL.path(percentEncoded: false)) else {
if FileManager.default.fileExists(atPath: fileURL.path(percentEncoded: false)) {
do {
try ConfigurationFile<Item>.migrate(at: fileURL, schemaURL: SchemaURLs.keyBindings)
} catch {
Logger.log(.error, "Failed to migrate \(Constants.fileName): \(error)")
}

return Logger.log(.info, "\(Constants.fileName) was created before")
}
Comment thread
cyanzhong marked this conversation as resolved.

do {
try "[]\n".write(to: fileURL, atomically: true, encoding: .utf8)
try "{\n \"$schema\": \"\(SchemaURLs.keyBindings)\",\n \"items\": []\n}\n".write(
to: fileURL,
atomically: true,
encoding: .utf8
)
} catch {
Logger.log(.error, "\(error)")
}
Expand All @@ -32,7 +42,7 @@ enum KeyBindings {
return []
}

guard let items = try? JSONDecoder().decode([Item].self, from: data) else {
guard let items = try? JSONDecoder().decode(ConfigurationFile<Item>.self, from: data).items else {
Logger.log(.error, "Failed to decode the file")
return []
}
Expand Down
10 changes: 8 additions & 2 deletions TextGrabber2/Sources/Services.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ enum Services {
return []
}

guard let items = try? JSONDecoder().decode([Item].self, from: data) else {
guard let items = try? JSONDecoder().decode(ConfigurationFile<Item>.self, from: data).items else {
Logger.log(.error, "Failed to decode the file")
return []
}
Expand All @@ -40,7 +40,13 @@ enum Services {
}()

static func initialize() {
guard !FileManager.default.fileExists(atPath: fileURL.path(percentEncoded: false)) else {
if FileManager.default.fileExists(atPath: fileURL.path(percentEncoded: false)) {
do {
try ConfigurationFile<Item>.migrate(at: fileURL, schemaURL: SchemaURLs.services)
} catch {
Logger.log(.error, "Failed to migrate \(Constants.fileName): \(error)")
}

return Logger.log(.info, "\(Constants.fileName) was created before")
}
Comment thread
cyanzhong marked this conversation as resolved.

Expand Down