Skip to content

Commit

Permalink
Add URL scheme to create new files (#834)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanzhong authored Jan 24, 2025
1 parent 9b5659e commit 1d64900
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// URLComponents+Extension.swift
//
// Created by cyan on 1/24/25.
//

import Foundation

public extension URLComponents {
var queryDict: [String: String]? {
queryItems?.reduce(into: [:]) { result, item in
result[item.name] = item.value
}
}
}
10 changes: 8 additions & 2 deletions MarkEditMac/Sources/Main/Application/AppDelegate+Document.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ extension AppDelegate {
}
case .newDocument:
menu.addItem(withTitle: Localized.Document.openDocument) {
NSApplication.shared.showOpenPanel()
sender.showOpenPanel()
}
}

return menu
}

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

Expand All @@ -61,6 +61,12 @@ extension AppDelegate {
}
}

func createNewFile(queryDict: [String: String]?) {
let fileName = queryDict?["filename"]
let initialContent = queryDict?["initial-content"]
createNewFile(fileName: fileName, initialContent: initialContent)
}

func toggleDocumentWindowVisibility() {
// Order out immaterial windows like settings, about...
for window in NSApp.windows where !(window is EditorWindow) {
Expand Down
21 changes: 21 additions & 0 deletions MarkEditMac/Sources/Main/Application/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,27 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
}
}

// MARK: - URL Handling

extension AppDelegate {
func application(_ application: NSApplication, open urls: [URL]) {
for url in urls {
// https://github.com/MarkEdit-app/MarkEdit/wiki/Text-Processing#using-url-schemes
let components = URLComponents(url: url, resolvingAgainstBaseURL: false)
switch components?.host {
case "new-file":
// markedit://new-file?filename=Untitled&initial-content=Hello
createNewFile(queryDict: components?.queryDict)
case "open":
// markedit://open
application.showOpenPanel()
default:
break
}
}
}
}

// MARK: - Private

private extension AppDelegate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ struct CreateNewDocumentIntent: AppIntent {

@MainActor
func perform() async throws -> some IntentResult {
NSApp.appDelegate?.createNewFile(initialContent: initialContent, fileName: fileName, isIntent: true)
NSApp.appDelegate?.createNewFile(
fileName: fileName,
initialContent: initialContent,
isIntent: true
)

return .result()
}
}

0 comments on commit 1d64900

Please sign in to comment.