-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATMailHelper.m
More file actions
111 lines (94 loc) · 3.54 KB
/
ATMailHelper.m
File metadata and controls
111 lines (94 loc) · 3.54 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
// ATMailHelper.m
// AP
//
// Created by Christopher Atlan on 25.12.09.
// Copyright 2009 Christopher Atlan. All rights reserved.
//
#import "ATMailHelper.h"
#import <WebKit/WebKit.h>
#define MAIL_BUNDLE_ID @"com.apple.mail"
@interface ATMailHelper ()
+ (BOOL)isAppleMailDefault;
+ (void)mailWebArchive:(WebArchive *)webArchive
title:(NSString *)aTitle
URL:(NSString *)aURL
alternateMailToURL:(NSURL *)alternateMailToURL;
@end
@implementation ATMailHelper
+ (void)mailHTMLString:(NSString *)string
baseURL:(NSURL *)URL
subject:(NSString *)aSubject
alternateMailToURL:(NSURL *)alternateMailToURL
{
if (![ATMailHelper isAppleMailDefault]) {
[[NSWorkspace sharedWorkspace] openURL:alternateMailToURL];
return;
}
NSData *htmlData = [string dataUsingEncoding:NSUTF8StringEncoding];
WebResource *mainResource = [[WebResource alloc] initWithData:htmlData
URL:URL
MIMEType:@"text/html"
textEncodingName:@"utf-8"
frameName:nil];
WebArchive *webArchive = [[WebArchive alloc] initWithMainResource:mainResource
subresources:nil
subframeArchives:nil];
[mainResource release];
[ATMailHelper mailWebArchive:webArchive title:aSubject URL:@"" alternateMailToURL:alternateMailToURL];
[webArchive release];
}
+ (void)mailWebArchive:(WebArchive *)webArchive
title:(NSString *)aTitle
URL:(NSString *)aURL
alternateMailToURL:(NSURL *)alternateMailToURL
{
NSData* targetBundleID = [MAIL_BUNDLE_ID dataUsingEncoding:NSUTF8StringEncoding];
NSAppleEventDescriptor *targetDescriptor = nil;
NSAppleEventDescriptor *appleEvent = nil;
targetDescriptor = [NSAppleEventDescriptor descriptorWithDescriptorType:typeApplicationBundleID
data:targetBundleID];
appleEvent = [NSAppleEventDescriptor appleEventWithEventClass:'mail'
eventID:'mlpg'
targetDescriptor:targetDescriptor
returnID:kAutoGenerateReturnID
transactionID:kAnyTransactionID];
[appleEvent setParamDescriptor:[NSAppleEventDescriptor descriptorWithDescriptorType:'tdta'
data:[webArchive data]]
forKeyword:'----'];
[appleEvent setParamDescriptor:[NSAppleEventDescriptor descriptorWithString:aTitle]
forKeyword:'urln'];
[appleEvent setParamDescriptor:[NSAppleEventDescriptor descriptorWithString:aURL]
forKeyword:'url '];
NSString *path = [[NSWorkspace sharedWorkspace] absolutePathForAppBundleWithIdentifier:MAIL_BUNDLE_ID];
NSURL *mailURL = [NSURL fileURLWithString:path];
NSDictionary *confDict = [NSDictionary dictionaryWithObjectsAndKeys:
appleEvent, NSWorkspaceLaunchConfigurationAppleEvent,
nil];
NSError *error;
NSRunningApplication *app = [[NSWorkspace sharedWorkspace] launchApplicationAtURL:mailURL
options:NSWorkspaceLaunchDefault
configuration:confDict
error:&error];
if (!app) {
//[[NSApplication sharedApplication] presentError:error];
[[NSWorkspace sharedWorkspace] openURL:alternateMailToURL];
return;
}
}
+ (BOOL)isAppleMailDefault {
BOOL rtn = NO;
NSString *defaultHandler = (NSString *)LSCopyDefaultHandlerForURLScheme(CFSTR("mailto"));
if (defaultHandler) {
if ([defaultHandler isEqualToString:MAIL_BUNDLE_ID])
rtn = YES;
} else {
NSArray *handlers = (NSArray *)LSCopyAllHandlersForURLScheme(CFSTR("mailto"));
if ([handlers count] == 1 && [[handlers lastObject] isEqualToString:MAIL_BUNDLE_ID])
rtn = YES;
[handlers release];
}
[defaultHandler release];
return rtn;
}
@end