forked from pixmeo/osirix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMailer.m
120 lines (92 loc) · 3.42 KB
/
Mailer.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
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
112
113
114
115
116
117
118
119
120
/*=========================================================================
Program: OsiriX
Copyright (c) OsiriX Team
All rights reserved.
Distributed under GNU - LGPL
See http://www.osirix-viewer.com/copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
=========================================================================*/
#import "Mailer.h"
#import "NSAppleScript+N2.h"
// if you want check point log info, define CHECK to the next line, uncommented:
#define CHECK NSLog(@"Applescript result code = %d", ok);
// This converts an AEDesc into a corresponding NSValue.
//static id aedesc_to_id(AEDesc *desc)
//{
//OSErr ok;
//
//if (desc->descriptorType == typeChar)
//{
//NSMutableData *outBytes;
//NSString *txt;
//
//outBytes = [[NSMutableData alloc] initWithLength:AEGetDescDataSize(desc)];
//ok = AEGetDescData(desc, [outBytes mutableBytes], [outBytes length]);
//CHECK;
//
//txt = [[NSString alloc] initWithData:outBytes encoding: NSUTF8StringEncoding];
//[outBytes release];
//[txt autorelease];
//
//return txt;
//}
//
//if (desc->descriptorType == typeSInt16)
//{
//SInt16 buf;
//
//AEGetDescData(desc, &buf, sizeof(buf));
//
//return [NSNumber numberWithShort:buf];
//}
//
//return [NSString stringWithFormat:@"[unconverted AEDesc, type=\"%c%c%c%c\"]", ((char *)&(desc->descriptorType))[0], ((char *)&(desc->descriptorType))[1], ((char *)&(desc->descriptorType))[2], ((char *)&(desc->descriptorType))[3]];
//
//}
@implementation Mailer
- (NSString *)mailScriptBody:(NSString *)body to:(NSString *)to subject:(NSString *)subject isMIME:(BOOL)isMIME name:(NSString *)clientName sendNow:(BOOL)sendWithoutUserReview image:(NSString*) imagePath
{
NSMutableString *s = [NSMutableString stringWithCapacity:1000];
[s appendString:@"tell application \"Mail\"\n"];
[s appendString:@"activate\n"];
[s appendString:@"set composeMessage to make new outgoing message with properties {visible:true}\n"];
[s appendString:@"tell composeMessage\n"];
if (isMIME && imagePath != nil && [[NSFileManager defaultManager] fileExistsAtPath:imagePath])
{
[s appendString:[NSString stringWithFormat:@"set aFile to \"%@\"\n",imagePath]];
[s appendString:@"tell content\n"];
[s appendString:@"make new attachment with properties {file name:aFile}\n"];
[s appendString:@"end tell\n"];
}
[s appendString:@"end tell\n"];
[s appendString:@"end tell\n"];
NSLog( @"%@", s);
return s;
}
- (BOOL)sendMail:(NSString *)richBody to:(NSString *)to subject:(NSString *)subject isMIME:(BOOL)isMIME name:(NSString *)client sendNow:(BOOL)sendWithoutUserReview image:(NSString*) imagePath
{
[self runScript:[self mailScriptBody:richBody to:to subject:subject isMIME:isMIME name:client sendNow: sendWithoutUserReview image:imagePath]];
return YES;
}
// initialize it in your init method:
- (id)init {
self = [super init];
if (self) {
//myComponent = OpenDefaultComponent(kOSAComponentType, kOSAGenericScriptingComponentSubtype);
// other initialization code here
}
return self;
}
// do the grunge work -
// the sweetly wrapped method is all we need to know:
- (void)runScript:(NSString *)txt
{
NSAppleScript* as = [[[NSAppleScript alloc] initWithSource:txt] autorelease];
NSDictionary* errs = nil;
[as runWithArguments:nil error:&errs];
if ([errs count])
NSLog(@"Error: AppleScript execution failed: %@", errs);
}
@end