Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for URL schemes #834

Merged
merged 1 commit into from
Jan 24, 2025
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
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()
}
}