Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSSavePanel Changing File Name Extensions With AccessoryView

I have NSSavePanel with accessoryView to let the user select a graphic format so that they can save an image (NSImage) as a file. So far, I have the following. (I'm skipping some lines to make it short.)

- (void)exportFile {    
    NSString *filename;
    if (formatIndex1 == 0) { // Default selection by user in Preferences
        filename = @"Untitled.bmp";
    }
    else if (formatIndex1 == 1) {
        filename = @"Untitled.gif";
    }
    ...

    [panel setAllowedFileTypes:[[NSArray alloc] initWithObjects:@"bmp",@"gif",@"jpg",@"jp2",@"png",nil]];
    [panel setAllowsOtherFileTypes:NO];
    [panel setExtensionHidden:NO];
    [panel setCanCreateDirectories:YES];
    [panel setNameFieldStringValue:filename];
    [panel setAccessoryView:accessoryView1];
    [formatMenu1 setAction:@selector(dropMenuChange:)]; // formatMenu1 is NSPopUpButton
    [formatMenu1 setTarget:self];

    [panel beginSheetModalForWindow:window completionHandler:^(NSInteger result) {
        if (result == NSFileHandlingPanelOKButton) {
            // getting panel url
        }
    }];
}

-(void)dropMenuChange:(NSPopUpButton *)sender { 
    NSSavePanel *savePanel = (NSSavePanel *)[sender window];
    [savePanel setNameFieldStringValue:@"..."];
}

I'm not 100% sure that I'm doing it right. What I want to achieve is that I want to append the right extension to the current file name whenever the user selects a file format on accessoryView's NSPopUpButton. Is there a magical way of doing that? Or do I have to set the current file name with the right extension to setNameFieldStringValue programmatically for myself?

Thank you for your help.

like image 312
El Tomato Avatar asked Aug 26 '13 12:08

El Tomato


1 Answers

What I want to achieve is that I want to append the right extension to the current file name whenever the user selects a file format on accessoryView's NSPopUpButton. Is there a magical way of doing that?

Yes, there is. You need not do it yourself with setNameFieldStringValue: , let the savePanel do it. Let us assume fileName is a full path like /Users/hg/Pictures/2013/08/Airplanes/pic123.png and it exists an accessoryView for the savePanel with a matrix of radio buttons. Each button has a title like @"jpg" or @"png" or... The action of the matrix is -selectFileType:

- (IBAction) selectFileType:(id)sender
{
    [savePanel setAllowedFileTypes:@[ [[sender selectedCell] title] ] ];
    // this will set the right extension
}

For using the savePanel I tried the following code:

- (void) saveImage:(NSImage *) theImg
{
  savePanel = [NSSavePanel savePanel];
  NSString *imageName = [fileName lastPathComponent];
  NSString *suffix = [imageName pathExtension];
  NSString *baseName = [imageName stringByDeletingPathExtension];

  // prepare the savePanel
  [savePanel setAccessoryView:accessoryView];
  [savePanel setAllowedFileTypes:@[ suffix ] ];
  [savePanel setDirectoryURL:[NSURL fileURLWithPath:fileName]];  // convert to URL
  [savePanel setNameFieldStringValue:baseName ];  // without extension !
  // savePanel does append the suffix

  // and now start the savePanel and choose the wanted fileType
  int rtn = [savePanel runModal];   // preferred method since 10.6
  if( rtn==NSFileHandlingPanelCancelButton) return;    // do nothing

  // finally create and save the file
  if( [[[savePanel allowedFileTypes] objectAtIndex:0] isEqualToString:@"jpg" ){
      // save as jpg-file
  }
  // check for other fileTypes
  . . . 
}
like image 200
Heinrich Giesen Avatar answered Oct 04 '22 01:10

Heinrich Giesen