Skip to content

Commit 1d64900

Browse files
authored
Add URL scheme to create new files (#834)
1 parent 9b5659e commit 1d64900

File tree

4 files changed

+50
-3
lines changed

4 files changed

+50
-3
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// URLComponents+Extension.swift
3+
//
4+
// Created by cyan on 1/24/25.
5+
//
6+
7+
import Foundation
8+
9+
public extension URLComponents {
10+
var queryDict: [String: String]? {
11+
queryItems?.reduce(into: [:]) { result, item in
12+
result[item.name] = item.value
13+
}
14+
}
15+
}

MarkEditMac/Sources/Main/Application/AppDelegate+Document.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ extension AppDelegate {
3333
}
3434
case .newDocument:
3535
menu.addItem(withTitle: Localized.Document.openDocument) {
36-
NSApplication.shared.showOpenPanel()
36+
sender.showOpenPanel()
3737
}
3838
}
3939

4040
return menu
4141
}
4242

43-
func createNewFile(initialContent: String?, fileName: String? = nil, isIntent: Bool = false) {
43+
func createNewFile(fileName: String? = nil, initialContent: String? = nil, isIntent: Bool = false) {
4444
// In EditorDocument, this is used as an external filename
4545
AppDocumentController.suggestedFilename = fileName
4646

@@ -61,6 +61,12 @@ extension AppDelegate {
6161
}
6262
}
6363

64+
func createNewFile(queryDict: [String: String]?) {
65+
let fileName = queryDict?["filename"]
66+
let initialContent = queryDict?["initial-content"]
67+
createNewFile(fileName: fileName, initialContent: initialContent)
68+
}
69+
6470
func toggleDocumentWindowVisibility() {
6571
// Order out immaterial windows like settings, about...
6672
for window in NSApp.windows where !(window is EditorWindow) {

MarkEditMac/Sources/Main/Application/AppDelegate.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,27 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
9898
}
9999
}
100100

101+
// MARK: - URL Handling
102+
103+
extension AppDelegate {
104+
func application(_ application: NSApplication, open urls: [URL]) {
105+
for url in urls {
106+
// https://github.com/MarkEdit-app/MarkEdit/wiki/Text-Processing#using-url-schemes
107+
let components = URLComponents(url: url, resolvingAgainstBaseURL: false)
108+
switch components?.host {
109+
case "new-file":
110+
// markedit://new-file?filename=Untitled&initial-content=Hello
111+
createNewFile(queryDict: components?.queryDict)
112+
case "open":
113+
// markedit://open
114+
application.showOpenPanel()
115+
default:
116+
break
117+
}
118+
}
119+
}
120+
}
121+
101122
// MARK: - Private
102123

103124
private extension AppDelegate {

MarkEditMac/Sources/Shortcuts/Intents/CreateNewDocumentIntent.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ struct CreateNewDocumentIntent: AppIntent {
2424

2525
@MainActor
2626
func perform() async throws -> some IntentResult {
27-
NSApp.appDelegate?.createNewFile(initialContent: initialContent, fileName: fileName, isIntent: true)
27+
NSApp.appDelegate?.createNewFile(
28+
fileName: fileName,
29+
initialContent: initialContent,
30+
isIntent: true
31+
)
32+
2833
return .result()
2934
}
3035
}

0 commit comments

Comments
 (0)