aboutsummaryrefslogtreecommitdiffstats
path: root/Copy Mailto/AppDelegate.m
blob: d74bf9649885847fe68a48bed35b3f39f1c61e7d (plain)
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
//
//  AppDelegate.m
//  Copy Mailto
//
//  Created by tw on 9/5/18.
//  Copyright © 2018 tw. All rights reserved.
//

#import "AppDelegate.h"

const NSUInteger MAILTO_INDEX = 7;

@implementation AppDelegate

- (void)applicationWillFinishLaunching:(NSNotification *)aNotification {
    [[NSAppleEventManager sharedAppleEventManager]
        setEventHandler:self
        andSelector:@selector(handleURLEvent:withReplyEvent:)
        forEventClass:kInternetEventClass
        andEventID:kAEGetURL];
}

- (void)handleURLEvent:(NSAppleEventDescriptor *)event
    withReplyEvent: (NSAppleEventDescriptor *)replyEvent
{
    NSString *url = [[event paramDescriptorForKeyword:keyDirectObject]
        stringValue];
    [_email_address setStringValue:[url substringFromIndex:MAILTO_INDEX]];
}

- (IBAction)copyEmailToClipboard:(id)sender
{
    NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
    [pasteboard clearContents];
    [pasteboard writeObjects:
        [NSArray arrayWithObject:
            [_email_address stringValue]]];

    [NSApp terminate:self];
}

@end