-
Notifications
You must be signed in to change notification settings - Fork 2
/
ScriptEditorDocument.m
590 lines (490 loc) · 24.3 KB
/
ScriptEditorDocument.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
//
// ScriptEditorDocument.m
// Pipe
//
// Created by RenŽ Puls on 14.02.05.
// Copyright 2005 RenŽ Puls. All rights reserved.
//
#import "ScriptEditorDocument.h"
#import "ScriptWindowController.h"
#import "ScriptDocumentError.h"
#import "ScriptVariable.h"
#import "KwikiController.h"
#import "ScriptReference.h"
#import "UserDefaults.h"
#import "SupportFileReference.h"
// Pipe Script document type names
// Don't forget to sync changes with the application Info.plist!
NSString * const PipeScriptDocumentType = @"Pipe Script";
NSString * const PipeScriptFlatDocumentType = @"Pipe Script (flattened)";
NSString * const PipeShellScriptDocumentType = @"Shell Script";
// Property list keys used in Pipe Script bundles
NSString * const PipeScriptArgumentStringPlistKey = @"PipeArgumentString";
NSString * const PipeScriptCodeStringEncodingPlistKey = @"PipeScriptStringEncoding";
NSString * const PipeScriptEditorWindowPropertiesPlistKey = @"PipeEditorWindowProperties";
NSString * const PipeScriptExecutesDirectlyPlistKey = @"PipeExecutesDirectly";
NSString * const PipeScriptHomeURLPlistKey = @"PipeHomeURL";
NSString * const PipeScriptInputStringEncodingPlistKey = @"PipeInputStringEncoding";
NSString * const PipeScriptLastSavedPlistKey = @"PipeLastSaved";
NSString * const PipeScriptOutputStringEncodingPlistKey = @"PipeOutputStringEncoding";
NSString * const PipeScriptOutputTypePlistKey = @"PipeOutputType";
NSString * const PipeScriptPreferredFileNameKey = @"PipePreferredScriptFileName";
NSString * const PipeScriptRunCommandPlistKey = @"PipeRunCommand";
NSString * const PipeScriptServiceCapablePlistKey = @"PipeServiceCapable";
NSString * const PipeScriptSourceAliasPlistKey = @"PipeScriptSourceAlias";
NSString * const PipeScriptVariablesPlistKey = @"PipeVariables";
NSString * const PipeScriptVersionPlistKey = @"PipeScriptVersion";
NSString * const PipeScriptInputWritingDirectionPlistKey = @"PipeInputWritingDirection";
NSString * const PipeScriptOutputWritingDirectionPlistKey = @"PipeOutputWritingDirection";
// Notifications
NSString * const ScriptEditorDocumentApplySavedPropertyListNotification = @"ScriptEditorDocumentApplySavedPropertyListNotification";
NSString * const ScriptEditorDocumentCollectSavedPropertyListNotification = @"ScriptEditorDocumentCollectSavedPropertyListNotification";
@implementation ScriptEditorDocument
#pragma mark Init and Cleanup
- (id)init
{
if ((self = [super init])) {
[self setHasUndoManager:YES];
[[self undoManager] disableUndoRegistration];
[self setExecutesDirectly:YES];
[[self undoManager] enableUndoRegistration];
}
return self;
}
- (id)initWithContentsOfFile:(NSString *)fileName ofType:(NSString *)docType
{
if ((self = [super initWithContentsOfFile:fileName ofType:docType])) {
if ([docType isEqualToString:PipeShellScriptDocumentType]) {
[self setFileType:PipeScriptDocumentType];
[self setFileName:nil];
}
[[self undoManager] removeAllActions];
}
return self;
}
- (id)initWithTemplatePath:(NSString *)aPath
{
NSParameterAssert(aPath != nil);
if ((self = [self initWithContentsOfFile:aPath ofType:PipeScriptDocumentType])) {
[self setFileName:nil];
[self setScriptSourceAlias:nil];
}
return self;
}
- (void)dealloc
{
[lastLoadedPropertyList release];
[scriptSourceAlias release];
[super dealloc];
}
#pragma mark Accessors
- (NDAlias *)scriptSourceAlias
{
return scriptSourceAlias;
}
- (void)setScriptSourceAlias:(NDAlias *)newScriptSourceAlias
{
if (scriptSourceAlias != newScriptSourceAlias) {
[scriptSourceAlias release];
scriptSourceAlias = [newScriptSourceAlias retain];
}
}
- (NSDictionary *)propertyList
{
NSMutableDictionary *propertyList;
propertyList = [NSMutableDictionary dictionary];
// Collect saved property lists from other document components
[[NSNotificationCenter defaultCenter] postNotificationName:ScriptEditorDocumentCollectSavedPropertyListNotification object:self userInfo:[NSDictionary dictionaryWithObject:propertyList forKey:@"propertyList"]];
[propertyList setObject:[NSNumber numberWithBool:[self executesDirectly]]
forKey:PipeScriptExecutesDirectlyPlistKey];
[propertyList setObject:[NSNumber numberWithInt:[self outputType]]
forKey:PipeScriptOutputTypePlistKey];
[propertyList setObject:[NSDate date]
forKey:PipeScriptLastSavedPlistKey];
[propertyList setObject:[NSNumber numberWithInt:1]
forKey:PipeScriptVersionPlistKey];
if ([self runCommand] != nil)
[propertyList setObject:[self runCommand]
forKey:PipeScriptRunCommandPlistKey];
if ([self argumentString] != nil)
[propertyList setObject:[self argumentString]
forKey:PipeScriptArgumentStringPlistKey];
if ([self homeURL] != nil)
[propertyList setObject:[[self homeURL] absoluteString]
forKey:PipeScriptHomeURLPlistKey];
[propertyList setObject:[NSNumber numberWithUnsignedInt:[self scriptStringEncoding]]
forKey:PipeScriptCodeStringEncodingPlistKey];
[propertyList setObject:[NSNumber numberWithUnsignedInt:[self inputStringEncoding]]
forKey:PipeScriptInputStringEncodingPlistKey];
[propertyList setObject:[NSNumber numberWithUnsignedInt:[self outputStringEncoding]]
forKey:PipeScriptOutputStringEncodingPlistKey];
if ([self preferredScriptFileName] != nil)
[propertyList setObject:[self preferredScriptFileName]
forKey:PipeScriptPreferredFileNameKey];
if ([self scriptSourceAlias])
[propertyList setObject:[NSArchiver archivedDataWithRootObject:[self scriptSourceAlias]]
forKey:PipeScriptSourceAliasPlistKey];
[propertyList setObject:[ScriptVariable propertyListFromVariablesArray:[self valueForKey:@"variables"]]
forKey:PipeScriptVariablesPlistKey];
[propertyList setObject:[NSNumber numberWithBool:[self isServiceCapable]]
forKey:PipeScriptServiceCapablePlistKey];
[propertyList setObject:[NSNumber numberWithInt:[self inputWritingDirection]]
forKey:PipeScriptInputWritingDirectionPlistKey];
[propertyList setObject:[NSNumber numberWithInt:[self outputWritingDirection]]
forKey:PipeScriptOutputWritingDirectionPlistKey];
return propertyList;
}
/*! The last property list that was read from disk. */
- (NSDictionary *)lastLoadedPropertyList
{
return lastLoadedPropertyList;
}
- (void)setLastLoadedPropertyList:(NSDictionary *)newPropertyList
{
if (newPropertyList != lastLoadedPropertyList) {
[lastLoadedPropertyList release];
lastLoadedPropertyList = [newPropertyList copy];
}
}
#pragma mark NSDocument
- (void)makeWindowControllers
{
id newWindowController;
if (!didMakeWindowControllers) {
newWindowController = [[ScriptWindowController alloc] init];
[self addWindowController:newWindowController];
[newWindowController release];
if ([self lastLoadedPropertyList] != nil) {
[[NSNotificationCenter defaultCenter] postNotificationName:ScriptEditorDocumentApplySavedPropertyListNotification object:self userInfo:[NSDictionary dictionaryWithObject:[self lastLoadedPropertyList] forKey:@"propertyList"]];
[self setLastLoadedPropertyList:nil];
}
didMakeWindowControllers = YES;
}
}
- (BOOL)revertToSavedFromFile:(NSString *)fileName ofType:(NSString *)type
{
[[NSNotificationCenter defaultCenter] postNotificationName:ScriptDocumentWillRevertNotification object:self];
if ([type isEqualToString:PipeShellScriptDocumentType]) {
[[self undoManager] setActionName:NSLocalizedString(@"Revert to External Source", @"Undo action name (revert to external script source)")];
return [self readFromFile:fileName ofType:type];
}
else {
return [super revertToSavedFromFile:fileName ofType:type];
}
}
- (BOOL)readFromFile:(NSString *)fileName ofType:(NSString *)docType
{
NSFileWrapper *fileWrapper;
fileWrapper = [[[NSFileWrapper alloc] initWithPath:fileName] autorelease];
if ([docType isEqualToString:PipeShellScriptDocumentType])
[self setScriptSourceAlias:[NDAlias aliasWithPath:fileName]];
return [self loadFileWrapperRepresentation:fileWrapper ofType:docType];
}
- (BOOL)loadFileWrapperRepresentation:(NSFileWrapper *)wrapper ofType:(NSString *)docType
{
NSDictionary *newPropertyList;
NSString *errorString = nil;
NSError *error = nil;
NSData *data;
NSString *string;
NSFileWrapper *usageWrapper, *supportWrapper;
id property;
if ([docType isEqualToString:PipeScriptFlatDocumentType]) {
NSFileWrapper *myWrapper;
NSData *serializedData;
serializedData = [wrapper regularFileContents];
myWrapper = [[[NSFileWrapper alloc] initWithSerializedRepresentation:serializedData] autorelease];
return [self loadFileWrapperRepresentation:myWrapper ofType:PipeScriptDocumentType];
}
else if ([docType isEqualToString:PipeScriptDocumentType]) {
// Check if the file wrapper represents a bundle
if (![wrapper isDirectory]) {
error = [ScriptDocumentError errorWithCode:ScriptDocumentNotABundleError userInfo:nil];
goto openError;
}
if ([[wrapper fileWrappers] objectForKey:@"Contents"])
wrapper = [[wrapper fileWrappers] objectForKey:@"Contents"];
// Read Info.plist
data = [[[wrapper fileWrappers] objectForKey:@"Info.plist"] regularFileContents];
if (data == nil) {
error = [ScriptDocumentError errorWithCode:ScriptDocumentIncompleteError userInfo:[NSDictionary dictionaryWithObject:@"Info.plist" forKey:@"missingFileName"]];
goto openError;
}
newPropertyList = [NSPropertyListSerialization propertyListFromData:data mutabilityOption:NSPropertyListImmutable format:NULL errorDescription:&errorString];
if (newPropertyList == nil) {
error = [ScriptDocumentError errorWithCode:ScriptDocumentBadInfoPlistError userInfo:[NSDictionary dictionaryWithObject:errorString forKey:@"errorString"]];
goto openError;
}
[self setArgumentString:[newPropertyList objectForKey:PipeScriptArgumentStringPlistKey]];
[self setRunCommand:[newPropertyList objectForKey:PipeScriptRunCommandPlistKey]];
if ((property = [newPropertyList objectForKey:PipeScriptHomeURLPlistKey]))
[self setHomeURL:[NSURL URLWithString:property]];
else
[self setHomeURL:nil];
if ((property = [newPropertyList objectForKey:PipeScriptExecutesDirectlyPlistKey]))
[self setExecutesDirectly:[property boolValue]];
else
[self setExecutesDirectly:YES];
if ((property = [newPropertyList objectForKey:PipeScriptOutputTypePlistKey]))
[self setOutputType:[property intValue]];
if ((property = [newPropertyList objectForKey:PipeScriptCodeStringEncodingPlistKey]))
[self setScriptStringEncoding:[property unsignedIntValue]];
else
[self setScriptStringEncoding:[JGUserDefaultValue(PipeTextEncodingDefaultKey) unsignedIntValue]];
if ((property = [newPropertyList objectForKey:PipeScriptInputStringEncodingPlistKey]))
[self setInputStringEncoding:[property unsignedIntValue]];
else
[self setInputStringEncoding:[JGUserDefaultValue(PipeTextEncodingDefaultKey) unsignedIntValue]];
if ((property = [newPropertyList objectForKey:PipeScriptOutputStringEncodingPlistKey]))
[self setOutputStringEncoding:[property unsignedIntValue]];
else
[self setOutputStringEncoding:[JGUserDefaultValue(PipeTextEncodingDefaultKey) unsignedIntValue]];
if ((property = [newPropertyList objectForKey:PipeScriptSourceAliasPlistKey]))
[self setScriptSourceAlias:[NSUnarchiver unarchiveObjectWithData:property]];
else
[self setScriptSourceAlias:nil];
if ((property = [newPropertyList objectForKey:PipeScriptServiceCapablePlistKey]))
[self setServiceCapable:[property boolValue]];
else
[self setServiceCapable:YES];
if ((property = [newPropertyList objectForKey:PipeScriptPreferredFileNameKey]))
[self setPreferredScriptFileName:property];
else
[self setPreferredScriptFileName:nil];
if ((property = [newPropertyList objectForKey:PipeScriptInputWritingDirectionPlistKey]))
[self setInputWritingDirection:[property intValue]];
else
[self setInputWritingDirection:NSWritingDirectionLeftToRight];
if ((property = [newPropertyList objectForKey:PipeScriptOutputWritingDirectionPlistKey]))
[self setOutputWritingDirection:[property intValue]];
else
[self setOutputWritingDirection:NSWritingDirectionLeftToRight];
[[self mutableArrayValueForKey:@"variables"] removeAllObjects];
if ((property = [newPropertyList objectForKey:PipeScriptVariablesPlistKey])) {
[[self mutableArrayValueForKey:@"variables"] setArray:[ScriptVariable arrayWithVariablesFromPropertyList:property]];
}
// Load script source
data = [[[wrapper fileWrappers] objectForKey:@"Script.txt"] regularFileContents];
if (data == nil) {
error = [ScriptDocumentError errorWithCode:ScriptDocumentIncompleteError userInfo:[NSDictionary dictionaryWithObject:@"Script.txt" forKey:@"missingFileName"]];
goto openError;
}
string = [[NSString alloc] initWithData:data encoding:[self scriptStringEncoding]];
if (string == nil) {
error = [ScriptDocumentError errorWithCode:ScriptDocumentBadEncodingError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInt:[self scriptStringEncoding]], @"expectedEncoding", @"Script.txt", @"fileName", nil]];
goto openError;
}
[self setScript:string];
[string release];
// Load input
data = [[[wrapper fileWrappers] objectForKey:@"Input.txt"] regularFileContents];
if (data == nil) {
error = [ScriptDocumentError errorWithCode:ScriptDocumentIncompleteError userInfo:[NSDictionary dictionaryWithObject:@"Input.txt" forKey:@"missingFileName"]];
goto openError;
}
string = [[NSString alloc] initWithData:data encoding:[self inputStringEncoding]];
if (string == nil) {
error = [ScriptDocumentError errorWithCode:ScriptDocumentBadEncodingError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInt:[self scriptStringEncoding]], @"expectedEncoding", @"Input.txt", @"fileName", nil]];
goto openError;
}
[self setInput:string];
[string release];
// Load output
data = [[[wrapper fileWrappers] objectForKey:@"Output.txt"] regularFileContents];
if (data == nil) {
error = [ScriptDocumentError errorWithCode:ScriptDocumentIncompleteError userInfo:[NSDictionary dictionaryWithObject:@"Output.txt" forKey:@"missingFileName"]];
goto openError;
}
[self setOutputData:data];
// Load usage info
usageWrapper = [[wrapper fileWrappers] objectForKey:@"Usage.rtfd"];
if (usageWrapper != nil) {
[self setUsageInfo:[[[NSAttributedString alloc] initWithRTFDFileWrapper:usageWrapper documentAttributes:nil] autorelease]];
}
// Load support files
[[self mutableArrayValueForKey:@"supportFiles"] removeAllObjects];
data = [[[wrapper fileWrappers] objectForKey:@"SupportFiles.keyedarchive"] regularFileContents];
if (data != nil) {
[[self mutableArrayValueForKey:@"supportFiles"] setArray:[NSKeyedUnarchiver unarchiveObjectWithData:data]];
}
else if ((supportWrapper = [[wrapper fileWrappers] objectForKey:@"Support Files"])) {
// Import old support directory from previous versions
if ([supportWrapper isDirectory]) {
NSEnumerator *nameEnumerator;
NSString *currentName;
NSFileWrapper *currentWrapper;
SupportFileReference *reference;
nameEnumerator = [[supportWrapper fileWrappers] keyEnumerator];
while ((currentName = [nameEnumerator nextObject])) {
currentWrapper = [[supportWrapper fileWrappers] objectForKey:currentName];
reference = [[SupportFileReference alloc] init];
[reference setFileWrapper:currentWrapper];
[self insertObject:reference inSupportFilesAtIndex:[self countOfSupportFiles]];
[reference release];
}
}
}
// Broadcast the property list to all observers, so they can restore their state
[[NSNotificationCenter defaultCenter] postNotificationName:ScriptEditorDocumentApplySavedPropertyListNotification object:self userInfo:[NSDictionary dictionaryWithObject:newPropertyList forKey:@"propertyList"]];
if (!didMakeWindowControllers)
[self setLastLoadedPropertyList:newPropertyList];
}
else if ([docType isEqualToString:PipeShellScriptDocumentType]) {
NSStringEncoding scriptEncoding;
scriptEncoding = [JGUserDefaultValue(PipeLastSelectedTextEncodingInFilePanelDefaultKey) unsignedIntValue];
string = [[NSString alloc] initWithData:[wrapper regularFileContents] encoding:scriptEncoding];
if (string == nil) {
error = [ScriptDocumentError errorWithCode:ScriptDocumentBadEncodingError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInt:[self scriptStringEncoding]], @"expectedEncoding", nil]];
goto openError;
}
[self setScript:string];
[string release];
}
return YES;
openError:
NSLog(@"Error reading script: %@", error);
return NO;
}
- (void)saveToFile:(NSString *)fileName saveOperation:(NSSaveOperationType)saveOperation delegate:(id)delegate didSaveSelector:(SEL)didSaveSelector contextInfo:(void *)contextInfo
{
[super saveToFile:fileName saveOperation:saveOperation delegate:delegate didSaveSelector:didSaveSelector contextInfo:contextInfo];
[[self reference] updateFromDocument:self];
}
- (BOOL)writeToFile:(NSString *)fullDocumentPath ofType:(NSString *)docType originalFile:(NSString *)fullOriginalDocumentPath saveOperation:(NSSaveOperationType)saveOperationType
{
if (docType == nil)
docType = PipeScriptDocumentType;
// Ugly undo bug workaround
[[NSRunLoop currentRunLoop] performSelector:@selector(fixUpUndoAfterSave:) target:self argument:nil order:NSUndoCloseGroupingRunLoopOrdering+1 modes:[NSArray arrayWithObject:NSDefaultRunLoopMode]];
return [super writeToFile:fullDocumentPath ofType:docType originalFile:fullOriginalDocumentPath saveOperation:saveOperationType];
}
- (NSFileWrapper *)fileWrapperRepresentationOfType:(NSString *)docType
{
NSFileWrapper *contentsWrapper, *dirWrapper, *scriptWrapper, *inputWrapper, *outputWrapper, *propertyListWrapper, *usageWrapper, *supportWrapper;
NSMutableDictionary *wrapperDict;
NSDictionary *newPropertyList;
NSData *propertyListData;
NSString *errorString = nil;
NSError *error = nil;
[[NSNotificationCenter defaultCenter] postNotificationName:ScriptDocumentWillSaveNotification object:self];
if ([docType isEqualToString:PipeScriptFlatDocumentType]) {
NSFileWrapper *myWrapper;
NSData *serializedData;
myWrapper = [self fileWrapperRepresentationOfType:PipeScriptDocumentType];
serializedData = [myWrapper serializedRepresentation];
return [[[NSFileWrapper alloc] initRegularFileWithContents:serializedData] autorelease];
}
else if ([docType isEqualToString:PipeScriptDocumentType]) {
// Create a property list
newPropertyList = [self propertyList];
propertyListData = [NSPropertyListSerialization dataFromPropertyList:newPropertyList format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorString];
if (propertyListData == nil) {
error = [ScriptDocumentError errorWithCode:ScriptDocumentBadInfoPlistError userInfo:[NSDictionary dictionaryWithObject:errorString forKey:@"errorString"]];
return nil;
}
scriptWrapper = [[NSFileWrapper alloc] initRegularFileWithContents:[script dataUsingEncoding:[self scriptStringEncoding]]];
inputWrapper = [[NSFileWrapper alloc] initRegularFileWithContents:[self inputData]];
outputWrapper = [[NSFileWrapper alloc] initRegularFileWithContents:[self outputData]];
propertyListWrapper = [[NSFileWrapper alloc] initRegularFileWithContents:propertyListData];
usageWrapper = [[self usageInfo] RTFDFileWrapperFromRange:NSMakeRange(0, [[self usageInfo] length]) documentAttributes:nil];
supportWrapper = [[NSFileWrapper alloc] initRegularFileWithContents:[NSKeyedArchiver archivedDataWithRootObject:[self valueForKey:@"supportFiles"]]];
wrapperDict = [[NSMutableDictionary alloc] init];
[wrapperDict setObject:scriptWrapper forKey:@"Script.txt"];
[wrapperDict setObject:inputWrapper forKey:@"Input.txt"];
[wrapperDict setObject:outputWrapper forKey:@"Output.txt"];
[wrapperDict setObject:propertyListWrapper forKey:@"Info.plist"];
[wrapperDict setObject:supportWrapper forKey:@"SupportFiles.keyedarchive"];
if (usageWrapper != nil)
[wrapperDict setObject:usageWrapper forKey:@"Usage.rtfd"];
[scriptWrapper release];
[inputWrapper release];
[outputWrapper release];
[propertyListWrapper release];
[supportWrapper release];
contentsWrapper = [[NSFileWrapper alloc] initDirectoryWithFileWrappers:wrapperDict];
[wrapperDict release];
dirWrapper = [[NSFileWrapper alloc] initDirectoryWithFileWrappers:[NSDictionary dictionaryWithObject:contentsWrapper forKey:@"Contents"]];
[contentsWrapper release];
return [dirWrapper autorelease];
}
return nil;
}
- (void)saveDocument:(id)sender
{
[super saveDocument:sender];
[[self reference] updateFromDocument:self];
}
#pragma mark Actions
- (IBAction)saveDocumentAsTemplate:(id)sender
{
NSDocumentController *documentController = [NSDocumentController sharedDocumentController];
NSSavePanel *savePanel = [NSSavePanel savePanel];
NSString *directory;
directory = [NSString stringWithFormat:@"%@/Library/Application Support/%@/Templates", NSHomeDirectory(), [[NSProcessInfo processInfo] processName]];
[savePanel setAllowedFileTypes:[NSArray arrayWithObjects:[[documentController fileExtensionsFromType:PipeScriptDocumentType] objectAtIndex:0], [[documentController fileExtensionsFromType:PipeScriptFlatDocumentType] objectAtIndex:0], nil]];
[savePanel setCanSelectHiddenExtension:YES];
[savePanel beginSheetForDirectory:directory file:nil modalForWindow:[self windowForSheet] modalDelegate:self didEndSelector:@selector(saveDocumentAsTemplateDidEnd:returnCode:contextInfo:) contextInfo:NULL];
}
- (void)saveDocumentAsTemplateDidEnd:(NSSavePanel *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
if (returnCode == NSOKButton) {
[self setFileType:PipeScriptDocumentType];
[self saveToFile:[sheet filename] saveOperation:NSSaveAsOperation delegate:nil didSaveSelector:NULL contextInfo:NULL];
// FIXME some other time -- fileTypeFromLastBlah... returns nil
[self setFileType:PipeScriptDocumentType];
}
}
- (IBAction)revertToExternalScriptSource:(id)sender
{
NSAlert *alert;
alert = [[NSAlert alloc] init];
[alert setMessageText:[NSString stringWithFormat:NSLocalizedString(@"Do you want to revert to the last saved revision of the external source file '%@'?", @"Alert message requesting confirmation for a 'Revert to Script Source' command"), [[NSFileManager defaultManager] displayNameAtPath:[[self scriptSourceAlias] path]]]];
[alert setInformativeText:NSLocalizedString(@"Your current changes to the script source will be lost.", @"Informative text for alert message requesting confirmation for a 'Revert to Script Source' command")];
[alert addButtonWithTitle:NSLocalizedString(@"Revert", @"Button confirming a 'Revert to Script Source' command")];
[alert addButtonWithTitle:NSLocalizedString(@"Cancel", @"Button cancelling a 'Revert to Script Source' command")];
[alert beginSheetModalForWindow:[self windowForSheet] modalDelegate:self didEndSelector:@selector(revertToExternalScriptSourceDidEnd:returnCode:contextInfo:) contextInfo:NULL];
[alert release];
}
- (void)revertToExternalScriptSourceDidEnd:(NSAlert *)alert returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
if (returnCode == NSAlertFirstButtonReturn) {
[self revertToSavedFromFile:[[self scriptSourceAlias] path] ofType:PipeShellScriptDocumentType];
}
}
- (IBAction)saveScriptSourceToFile:(id)sender
{
NSSavePanel *savePanel;
NSString *sourcePath;
sourcePath = [[self scriptSourceAlias] path];
if (([sender tag] == 1) && (sourcePath != nil)) {
[[NSNotificationCenter defaultCenter] postNotificationName:ScriptDocumentWillSaveNotification object:self];
[[self scriptData] writeToFile:sourcePath atomically:YES];
}
else {
savePanel = [NSSavePanel savePanel];
[savePanel beginSheetForDirectory:[sourcePath stringByDeletingLastPathComponent] file:[sourcePath lastPathComponent] modalForWindow:[self windowForSheet] modalDelegate:self didEndSelector:@selector(saveScriptSourceToFileDialogDidEnd:returnCode:contextInfo:) contextInfo:NULL];
}
}
- (void)saveScriptSourceToFileDialogDidEnd:(NSSavePanel *)savePanel returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
if (returnCode == NSOKButton) {
[[NSNotificationCenter defaultCenter] postNotificationName:ScriptDocumentWillSaveNotification object:self];
if ([[self scriptData] writeToFile:[savePanel filename] atomically:YES])
[self setScriptSourceAlias:[NDAlias aliasWithPath:[savePanel filename]]];
}
}
#pragma mark User Interface Validation
- (BOOL)validateMenuItem:(NSMenuItem *)anItem
{
SEL action = [anItem action];
if (action == @selector(revertToExternalScriptSource:)) {
return [[self scriptSourceAlias] path] != nil;
}
return [super validateMenuItem:anItem];
}
#pragma mark Misc Stuff
- (void)fixUpUndoAfterSave:(id)sender
{
[self updateChangeCount:NSChangeCleared];
}
@end