-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathSdefEditor.m
349 lines (307 loc) · 10.5 KB
/
SdefEditor.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* SdefEditor.m
* Sdef Editor
*
* Created by Rainbow Team.
* Copyright © 2006 - 2007 Shadow Lab. All rights reserved.
*/
#import "SdefEditor.h"
#import <WonderBox/WBLSFunctions.h>
#import <WonderBox/WBApplication.h>
#import "SdefSuite.h"
#import "Preferences.h"
#import "SdefDocument.h"
#import "AeteImporter.h"
#import "SdefDictionary.h"
#import "ImporterWarning.h"
#import "OSASdefImporter.h"
#import "CocoaSuiteImporter.h"
#import "SdefObjectInspector.h"
#import "ImportApplicationAete.h"
#if defined (DEBUG)
#import <Foundation/NSDebug.h>
#endif
enum {
kSdefEditorCurrentVersion = 0x010600, /* 1.6.0 */
};
int main(int argc, const char *argv[]) {
#if defined (DEBUG)
NSDebugEnabled = YES;
#endif
return NSApplicationMain(argc, argv);
}
NSString * const ScriptingDefinitionFileType = @"ScriptingDefinition";
NSString * const ScriptingDefinitionFileUTI = @"com.apple.scripting-definition";
const OSType kScriptingDefinitionHFSType = 'Sdef';
NSString * const CocoaSuiteDefinitionFileType = @"AppleScriptSuiteDefinition";
const OSType kCocoaSuiteDefinitionHFSType = 'ScSu';
@interface SdefEditor (DebugFacility)
- (void)createDebugMenu;
@end
@interface SdefDocumentController : NSDocumentController {
}
@end
@implementation SdefEditor
- (id)init {
if (self = [super init]) {
/* Assume we are using Xcode 2.5 or later */
// NSString *sdp = @"/Developer/usr/bin/sdp";
// NSString *rez = @"/Developer/usr/bin/Rez";
/* Initialize custom controller */
SdefDocumentController *ctrl = [[SdefDocumentController alloc] init];
if ([ctrl respondsToSelector:@selector(setAutosavingDelay:)]) {
[ctrl setAutosavingDelay:60];
}
[[NSUserDefaults standardUserDefaults] registerDefaults:@{
@"SdefOpenAtStartup" : @(YES),
@"SdefAutoSelectItem" : @(YES),
// sdp, @"SdefSdpToolPath",
// rez, @"SdefRezToolPath",
}];
[NSApp setDelegate:self];
#if defined (DEBUG)
[[NSUserDefaults standardUserDefaults] registerDefaults:@{
@"SdefDebugMenu" : @(YES),
@"SdefPantherExportEnabled" : @(YES),
// @"YES", @"NSShowNonLocalizedStrings",
// @"NO", @"NSShowAllViews",
// @"6", @"NSDragManagerLogLevel",
// @"YES", @"NSShowNonLocalizableStrings",
// @"1", @"NSScriptingDebugLogLevel",
}];
#endif
}
return self;
}
- (void)showWelcome {
spx_trace();
}
- (void)awakeFromNib {
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"SdefDebugMenu"])
[self createDebugMenu];
#if __LP64__
NSMenu *file = [[[NSApp mainMenu] itemWithTag:1] submenu];
NSMenuItem *export = [file itemWithTag:2];
if (export) {
NSMenuItem *item = [[export submenu] itemAtIndex:0];
if (item) {
[[export submenu] removeItem:item]; // WARNING: item lifecycle
[item setTitle:NSLocalizedString(@"Create Dictionary...", @"Create dictionary 64 bits title")];
NSInteger idx = [file indexOfItem:export];
[file removeItem:export];
[file insertItem:item atIndex:idx];
}
}
#endif
NSUInteger version = [[NSUserDefaults standardUserDefaults] integerForKey:@"SdefEditorVersion"];
if (version < kSdefEditorCurrentVersion) {
[[NSUserDefaults standardUserDefaults] setInteger:kSdefEditorCurrentVersion forKey:@"SdefEditorVersion"];
[self showWelcome];
}
}
- (IBAction)openInspector:(id)sender {
[[SdefObjectInspector sharedInspector] showWindow:sender];
}
- (IBAction)preferences:(id)sender {
static Preferences *preferences = nil;
if (!preferences) {
preferences = [[Preferences alloc] init];
[[preferences window] center];
}
[preferences showWindow:sender];
}
- (IBAction)releaseNotes:(id)sender {
[[NSHelpManager sharedHelpManager] openHelpAnchor:@"SdefReleaseNotes" inBook:@"Sdef Editor Help"];
}
- (IBAction)openSuite:(id)sender {
NSString *suite = nil;
switch ([sender tag]) {
case 1:
suite = @"NSCoreSuite";
break;
case 2:
suite = @"NSTextSuite";
break;
case 3:
suite = @"AppleScriptKit";
break;
case 4:
suite = @"Skeleton";
break;
}
NSString *suitePath = [[NSBundle mainBundle] pathForResource:suite ofType:@"sdef"];
if (suitePath) {
NSDocumentController *ctrl = [NSDocumentController sharedDocumentController];
[ctrl openDocumentWithContentsOfURL:[NSURL fileURLWithPath:suitePath] display:NO
completionHandler:
^(NSDocument * _Nullable document, BOOL documentWasAlreadyOpen, NSError * _Nullable error) {
if (document) {
[document setFileURL:nil];
[document makeWindowControllers];
[document showWindows];
} else if (error) {
[NSApp presentError:error];
}
}];
}
}
- (IBAction)openSdefReference:(id)sender {
}
#pragma mark -
#pragma mark Importation
- (IBAction)openApplicationTerminology:(id)sender {
ImportApplicationAete *panel = [[ImportApplicationAete alloc] initWithWindowNibName:@"ImportApplicationSdef"];
[panel showWindow:sender];
[NSApp runModalForWindow:[panel window]];
WBApplication *appli = [panel selection];
if (appli) {
NSURL *url = appli.URL;
SdefImporter *importer = [[OSASdefImporter alloc] initWithURL:url];
[self importWithImporter:importer];
}
}
- (void)importWithImporter:(SdefImporter *)importer {
@try {
NSArray *suites = [importer sdefSuites];
SdefDictionary *dico = [importer sdefDictionary];
if ([dico hasChildren] || [suites count]) {
NSError *error = nil;
SdefDocument *doc = [[NSDocumentController sharedDocumentController] openUntitledDocumentAndDisplay:NO error:&error];
if (!doc) {
if (error) [NSApp presentError:error];
} else if (dico) {
[doc setDictionary:dico];
} else if ([suites count]) {
[[doc dictionary] removeAllChildren];
for (SdefSuite *suite in suites) {
[[doc dictionary] appendChild:suite];
}
[[doc undoManager] removeAllActions];
[doc updateChangeCount:NSChangeCleared];
}
[doc makeWindowControllers];
[doc showWindows];
if ([importer warnings]) {
ImporterWarning *alert = [[ImporterWarning alloc] init];
[alert setDocument:doc];
[alert setWarnings:[importer warnings]];
[alert setReleasedWhenClosed:YES];
[alert showWindow:nil];
}
} else {
NSRunAlertPanel(@"Importation failed!", @"Sdef Editor cannot import this file. Is it in a valid format?", @"OK", nil, nil);
}
} @catch (id exception) {
spx_log_exception(exception);
NSBeep();
}
}
- (void)importCocoaScriptFile:(NSString *)file {
CocoaSuiteImporter *importer = [[CocoaSuiteImporter alloc] initWithContentsOfFile:file];
if ([importer preload]) {
[self importWithImporter:importer];
} else {
NSRunAlertPanel(@"Sorry! Sdef Editor cannot import this definition", @"Try with desdp(1) tools (see 'man desdp')", @"OK", nil, nil);
}
}
- (IBAction)importCocoaTerminology:(id)sender {
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setPrompt:@"Import"];
[openPanel setMessage:@"Choose a Cocoa .scriptSuite File"];
[openPanel setCanChooseFiles:YES];
[openPanel setCanCreateDirectories:NO];
[openPanel setCanChooseDirectories:NO];
[openPanel setAllowsMultipleSelection:NO];
[openPanel setTreatsFilePackagesAsDirectories:YES];
[openPanel setAllowedFileTypes:[NSArray arrayWithObject:@"scriptSuite"]];
switch([openPanel runModal]) {
case NSModalResponseCancel:
return;
}
if (![[openPanel URLs] count]) return;
NSURL *file = [[openPanel URLs] objectAtIndex:0];
[self importCocoaScriptFile:[file path]];
}
- (IBAction)importAete:(id)sender {
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setPrompt:@"Import"];
[openPanel setMessage:@"Choose an aete Rsrc File"];
[openPanel setCanChooseFiles:YES];
[openPanel setCanCreateDirectories:NO];
[openPanel setCanChooseDirectories:NO];
[openPanel setAllowsMultipleSelection:NO];
[openPanel setTreatsFilePackagesAsDirectories:YES];
switch([openPanel runModal]) {
case NSModalResponseCancel:
return;
}
if (![[openPanel URLs] count]) return;
NSURL *file = [[openPanel URLs] objectAtIndex:0];
AeteImporter *aete = [[AeteImporter alloc] initWithContentsOfFile:[file path]];
[self importWithImporter:aete];
}
- (IBAction)importSystemSuites:(id)sender {
AeteImporter *aete = [[AeteImporter alloc] initWithSystemSuites];
if (aete)
[self importWithImporter:aete];
}
- (IBAction)importApplicationAete:(id)sender {
ImportApplicationAete *panel = [[ImportApplicationAete alloc] init];
[panel showWindow:sender];
[NSApp runModalForWindow:[panel window]];
WBApplication *appli = [panel selection];
if (appli) {
if (![appli isRunning]) {
[appli launch];
}
AeteImporter *aete = nil;
NSString *bid = [appli bundleIdentifier];
if (bid)
aete = [[AeteImporter alloc] initWithApplicationBundleIdentifier:bid];
if (aete) {
[self importWithImporter:aete];
} else {
NSBeep();
}
}
}
#pragma mark -
#pragma mark Application Delegate
- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename {
Boolean isapp = false;
NSURL *url = [NSURL fileURLWithPath:filename];
NSString *type = [[NSDocumentController sharedDocumentController] typeForContentsOfURL:url error:NULL];
if ([type isEqualToString:CocoaSuiteDefinitionFileType]) {
[self importCocoaScriptFile:filename];
return YES;
} else {
if ((noErr == WBLSIsApplicationAtURL(SPXNSToCFURL(url), &isapp)) && isapp) {
SdefImporter *importer = [[OSASdefImporter alloc] initWithURL:url];
[self importWithImporter:importer];
return YES;
}
}
/* lets document manager handle it */
return NO;
}
- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender {
return [[NSUserDefaults standardUserDefaults] boolForKey:@"SdefOpenAtStartup"];
}
#pragma mark -
#pragma mark Debug Menu
- (void)createDebugMenu {
NSMenuItem *debugMenu = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
NSMenu *menu = [[NSMenu alloc] initWithTitle:@"Debug"];
[menu addItemWithTitle:@"Import System Suites" action:@selector(importSystemSuites:) keyEquivalent:@""];
[debugMenu setSubmenu:menu];
[[NSApp mainMenu] insertItem:debugMenu atIndex:[[NSApp mainMenu] numberOfItems] -1];
}
@end
@implementation SdefDocumentController
- (void)noteNewRecentDocument:(NSDocument *)aDocument {
NSString *path = [[aDocument fileURL] path];
if (![path hasPrefix:[[NSBundle mainBundle] bundlePath]]) {
[super noteNewRecentDocument:aDocument];
}
}
@end