forked from snej/MYUtilities
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMYURLHandler.m
56 lines (46 loc) · 1.66 KB
/
MYURLHandler.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//
// MYURLHandler.m
// MYUtilities
//
// Created by Jens Alfke on 3/15/12.
// Copyright (c) 2012 Jens Alfke. All rights reserved.
//
#import "MYURLHandler.h"
#import <AppKit/AppKit.h>
@implementation MYURLHandler
+ (void) installHandler {
NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
[appleEventManager setEventHandler: self
andSelector: @selector(handleGetURLEvent:withReplyEvent:)
forEventClass: kInternetEventClass
andEventID: kAEGetURL];
}
+ (void) handleGetURLEvent: (NSAppleEventDescriptor *)event
withReplyEvent: (NSAppleEventDescriptor *)reply
{
OSStatus err = paramErr;
NSString *errStr = @"Unknown URL";
NSString *urlStr = [[event paramDescriptorForKeyword: keyDirectObject] stringValue];
if( urlStr ) {
NSURL *url = [NSURL URLWithString: urlStr];
if( url.scheme.length > 0 ) {
id<MYURLHandlerDelegate> delegate = [NSApp delegate];
NSError* error = nil;
if ([delegate openURL: url error: &error]) {
err = noErr;
errStr = nil;
} else if (error) {
err = (OSStatus)error.code;
errStr = error.localizedDescription;
}
}
}
if( reply ) {
[reply setParamDescriptor: [NSAppleEventDescriptor descriptorWithInt32: err]
forKeyword: keyErrorNumber];
if( errStr )
[reply setParamDescriptor: [NSAppleEventDescriptor descriptorWithString: errStr]
forKeyword: keyErrorString];
}
}
@end