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

x-macdown URL scheme implementation #985

Merged
merged 4 commits into from
Jun 3, 2018
Merged
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
73 changes: 69 additions & 4 deletions MacDown/Code/Application/MPMainController.m
Original file line number Diff line number Diff line change
@@ -117,6 +117,71 @@ - (void)applicationDidFinishLaunching:(NSNotification *)notification
[invocation invoke];
#pragma clang diagnostic pop
}
[[NSAppleEventManager sharedAppleEventManager]
setEventHandler:self
andSelector:@selector(openUrlSchemeAppleEvent:withReplyEvent:)
forEventClass:kInternetEventClass andEventID:kAEGetURL];
}

// Open a file from a browser with url of the form :
// "x-macdown://open?url=file:///path/to/a/file&line=123&column=45"
- (void)openUrlSchemeAppleEvent:(NSAppleEventDescriptor *)event
withReplyEvent:(NSAppleEventDescriptor *)reply
{
NSString *urlString = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
if (!urlString) {
return;
}
NSURL *url = [[NSURL alloc] initWithString:urlString];
if (!url) {
return;
}
NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:url
resolvingAgainstBaseURL:NO];
if (!urlComponents) {
return;
}
NSString *host = urlComponents.host;
if (!host || ![host isEqualToString:@"open"]) {
return;
}
NSArray *queryItems = urlComponents.queryItems;
if (!queryItems) {
return;
}
NSString *fileParam = [self valueForKey:@"url" fromQueryItems:queryItems];
if (!fileParam) {
return;
}
// FIXME: Could not figure out how to place the insertion point at a given
// line and column.
/* Unused */ NSString *lineParam = [self valueForKey:@"line"
fromQueryItems:queryItems];
/* Unused */ NSString *columnParam = [self valueForKey:@"column"
fromQueryItems:queryItems];
NSLog(@"%@:%@:%@", fileParam, lineParam, columnParam);

NSURL *target = [NSURL URLWithString:fileParam];
if (!target) {
return;
}
NSDocumentController *c = [NSDocumentController sharedDocumentController];
[c openDocumentWithContentsOfURL:target display:YES completionHandler:
^(NSDocument *document, BOOL wasOpen, NSError *error) {
if (!document || wasOpen || error)
return;
NSRect frame = [NSScreen mainScreen].visibleFrame;
for (NSWindowController *wc in document.windowControllers)
[wc.window setFrame:frame display:YES];
}];

}

- (NSString *)valueForKey:(NSString *)key fromQueryItems:(NSArray *)queryItems
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name=%@", key];
NSURLQueryItem *queryItem = [[queryItems filteredArrayUsingPredicate:predicate] firstObject];
return queryItem.value;
}

- (MPPreferences *)preferences
@@ -268,19 +333,19 @@ - (void)openPendingFiles

- (void)openPendingPipedContent {
NSDocumentController *c = [NSDocumentController sharedDocumentController];

if (self.preferences.pipedContentFileToOpen) {
NSURL *pipedContentFileToOpenURL = [NSURL fileURLWithPath:self.preferences.pipedContentFileToOpen];
NSError *readPipedContentError;
NSString *pipedContentString = [NSString stringWithContentsOfURL:pipedContentFileToOpenURL encoding:NSUTF8StringEncoding error:&readPipedContentError];

NSError *openDocumentError;
MPDocument *document = (MPDocument *)[c openUntitledDocumentAndDisplay:YES error:&openDocumentError];

if (document && openDocumentError == nil && readPipedContentError == nil) {
document.markdown = pipedContentString;
}

self.preferences.pipedContentFileToOpen = nil;
[self.preferences synchronize];
}
12 changes: 12 additions & 0 deletions MacDown/MacDown-Info.plist
Original file line number Diff line number Diff line change
@@ -4,6 +4,18 @@
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>Macdown custom control</string>
<key>CFBundleURLSchemes</key>
<array>
<string>x-macdown</string>
</array>
</dict>
</array>

<key>CFBundleDocumentTypes</key>
<array>
<dict>