Re: Import/Export items
Re: Import/Export items
- Subject: Re: Import/Export items
- From: Scott Ellsworth <email@hidden>
- Date: Sat, 26 Mar 2005 00:20:52 -0800
What follows is the code I have, which works reasonably well.
One question: how do i get a pulldown to suggest a filetype? The
types are enforced, but invisibly, and it would be keen if the user
got a filetype pulldown like that provided for types defined in the
info.plist. One less thing for them to think about. (NB - pointers
to places to look are fine.)
The current, pretty much working, code:
- (IBAction)import:(id)sender{
sender = sender;
NSArray *fileTypes = [NSArray arrayWithObjects: @"sec",
@"sectab", nil];
NSOpenPanel *panel = [NSOpenPanel openPanel];
[panel setAllowsMultipleSelection:NO];
[panel setCanChooseFiles:YES];
[panel setCanChooseDirectories:NO];
int result = [panel runModalForTypes:fileTypes];
if (result == NSOKButton) {
NSArray *urlsToOpen = [panel URLs];
int count = [urlsToOpen count];
for (int i=0; i<count; i++) {
NSURL * url = [urlsToOpen objectAtIndex:i];
NSError *error = nil;
NSString * data = [NSString stringWithContentsOfURL:url
encoding:NSMacOSRomanStringEncoding error:&error];
if (data==nil){
NSLog(@"Error reading file at %@: %@", [url path],
error);
} else {
[self setFromString:data];
[self refreshEverything];
}
}
}
}
- (IBAction)export:(id)sender{
sender = sender;
// Is there a way to get the file type goodness in a save panel?
NSSavePanel * sp = [NSSavePanel savePanel];
[sp setAllowedFileTypes:[NSArray
arrayWithObjects:@"sec",@"sectab",nil]];
int runResult = [sp runModal];
if (runResult == NSOKButton) {
NSURL * url = [sp URL];
NSString * path = [url path];
BOOL tabDelimited = [[path pathExtension]
isEqualToString:@"sectab"];
NSString * data = [self dataAsStringTabDelimited:tabDelimited];
NSError *error = nil;
BOOL writeResult = [data writeToURL:url atomically:YES
encoding:NSMacOSRomanStringEncoding error:&error];
if (writeResult == NO){
NSLog(@"Failed to write file at %@, error %@", path,
error);
}
}
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden