0

I use NSSavePanel only when I deal with text files. If I have an image to export, I use NSOpenPanel so that the user can select a directory, and I don't get caught with in the sandbox file path restriction. This time, though, I want to use NSSavePanel to let the user save an image file (bmp, gif, jpeg, jp2, png).

- (void)exportfile {    
    NSString *documentFolderPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSSavePanel *panel = [NSSavePanel savePanel];

    [panel setMessage:@"Please select a path where to save checkboard as an image."]; // Message inside modal window
    if ([self fileExists:destinationpath]) {
        [panel setDirectoryURL:[NSURL fileURLWithPath:destinationpath]];
    } else {
        [panel setDirectoryURL:[NSURL fileURLWithPath:documentFolderPath]];
    }

    //[panel setAllowedFileTypes:[[NSArray alloc] initWithObjects:@"bmp",@"gif",@"jpg",@"jp2",@"png",nil]];
    [panel setAllowsOtherFileTypes:YES];
    [panel setExtensionHidden:YES];
    [panel setCanCreateDirectories:YES];
    [panel setNameFieldStringValue:filename];
    [panel setTitle:@"Saving checkboard..."]; // Window title
    [panel setAccessoryView:accessoryView1];

    NSInteger result = [panel runModal];
    NSError *error = nil;

    if (result == NSOKButton) {     
        ////////////////////////////////////////////
        NSString *path0 = [[panel URL] path];
        NSMutableString *path1 = [[NSMutableString alloc] initWithString:path0];
        if (formatIndex1 == 0) { // formatIndex1 is the index of NSPopMenu
            [path1 appendString:@".bmp"];
        }
        else if (formatIndex1 == 1) {
            [path1 appendString:@".gif"];
        }
        else if (formatIndex1 == 2) {
            [path1 appendString:@".jpg"];
        }
        else if (formatIndex1 == 3) {
            [path1 appendString:@".jp2"];
        }
        else if (formatIndex1 == 4) {
            [path1 appendString:@".png"];
        }
        [self exportfile2:path1]; // <<<<<<<<<<<<<<< Immediate goal
        ////////////////////////////////////////////

        if (error) {
            [NSApp presentError:error];
        }
    }
}

This NSSavePanel has an AccessoryView control, allowing the user to select a graphic format with an NSPopupMenu. Its index is formatIndex1. I know that the code fails if the application is sandboxed. That's because the exact path that the user selects is path0, and the code above appends a file extension to it before reaching exportfile2. So how do I go about getting the exact path with a file extension. Preview automatically appends a file extension if 'Hide extension' is off. Yet, Preview won't fail to export an image.

Thank you for your help.

El Tomato
  • 6,479
  • 6
  • 46
  • 75
  • It seems that the last person at the following link has a solution. I'm not sure how to exactly implement it. http://t6580.codeinpro.us/q/5080f5b24f1eba38a4d13bd7 – El Tomato Aug 26 '13 at 07:50

1 Answers1

2

Try like this below:-

- (void)exportfile {    
    NSSavePanel *panel = [NSSavePanel savePanel];
NSString *fileName=nil;

        if (formatIndex1 == 0) { // formatIndex1 is the index of NSPopMenu
        fileName=@"untitle.bmp"
        }
        else if (formatIndex1 == 1) {
        fileName=@"untitle.gif"
        }
        else if (formatIndex1 == 2) {
          fileName=@"untitle.jpg"
        }

[panel setMessage:@"Please select a path where to save checkboard as an image."]; // Message inside modal window
    [panel setAllowsOtherFileTypes:YES];
    [panel setExtensionHidden:YES];
    [panel setCanCreateDirectories:YES];
    [panel setNameFieldStringValue:filename];
    [panel setTitle:@"Saving checkboard..."]; // Window title
    [panel setAccessoryView:accessoryView1];

    NSInteger result = [panel runModal];
    NSError *error = nil;

    if (result == NSOKButton) {     
        ////////////////////////////////////////////
        NSString *path0 = [[panel URL] path];


        [self exportfile2:path0]; // <<<<<<<<<<<<<<< Immediate goal
        ////////////////////////////////////////////

        if (error) {
            [NSApp presentError:error];
        }
    }
}
Abdeali Siyawala
  • 80
  • 1
  • 1
  • 6
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
  • No. That's not going to solve anything because the exported file will simply lose a file extension, which would make the file unreadable unless the user manually adds an appropriate one. – El Tomato Aug 26 '13 at 22:43
  • I did not get your problem. Can you please explain? – Hussain Shabbir Aug 27 '13 at 06:59
  • If you don't understand the issue, see the link that I've give at the top or see the following. http://stackoverflow.com/questions/1930352/nssavepanel-squelching-the-confirm-replace-dialog – El Tomato Aug 29 '13 at 07:11