forked from pixmeo/osirix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiPhoto.m
121 lines (92 loc) · 3.05 KB
/
iPhoto.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
121
/*=========================================================================
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 "iPhoto.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 iPhoto
- (NSString *) scriptBody:(NSArray*) files
{
NSString *albumNameStr = [[NSUserDefaults standardUserDefaults] stringForKey: @"ALBUMNAME"];
NSMutableString *s = [NSMutableString stringWithCapacity:1000];
[s appendString:@"tell application \"iPhoto\"\n"];
// [s appendString:@"activate\n"];
[s appendString:[NSString stringWithFormat:@"if not (exists album \"%@\") then \n", albumNameStr]];
[s appendString:[NSString stringWithFormat:@"new album name \"%@\" \n", albumNameStr]];
[s appendString:@"end if \n"];
[s appendString:[NSString stringWithFormat:@"set this_album to album \"%@\" \n", albumNameStr]];
[s appendString:@"select this_album \n"];
for( id loopItem in files)
{
[s appendString:[NSString stringWithFormat:@"set this_path to \"%@\" \n",loopItem]];
[s appendString:@"import from this_path to this_album \n"];
}
[s appendString:@"end tell \n"];
return s;
}
- (BOOL)importIniPhoto: (NSArray*) files
{
[self runScript:[self scriptBody:files]];
return YES;
}
// initialize it in your init method:
- (id)init
{
self = [super init];
if (self)
{
}
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