Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Save As" in non-document based application Cocoa

I'm working on a project that essentially creates files via a shell script using "sox". The application is not document-based, all its doing is calling a script that creates files, and doesn't internally save any data. However, I need to prompt users where to save the file, and what file name to use before the script is run. What would be the best way to have a "save as.." dialog box to get a filepath/filename from a user to pass to the shell script?

like image 977
minimalpop Avatar asked May 19 '11 21:05

minimalpop


1 Answers

This is pretty straightforward. You tagged this question with NSSavePanel and that's exactly what you want to use.

- (IBAction)showSavePanel:(id)sender
{
    NSSavePanel * savePanel = [NSSavePanel savePanel];
    // Restrict the file type to whatever you like
    [savePanel setAllowedFileTypes:@[@"txt"]];
    // Set the starting directory
    [savePanel setDirectoryURL:someURL];
    // Perform other setup
    // Use a completion handler -- this is a block which takes one argument
    // which corresponds to the button that was clicked
    [savePanel beginSheetModalForWindow:someWindow completionHandler:^(NSInteger result){
        if (result == NSFileHandlingPanelOKButton) {
            // Close panel before handling errors
            [savePanel orderOut:self];
            // Do what you need to do with the selected path
        }
    }];
}

See also "The Save Panel" in the File System Programming Guide.

like image 198
jscs Avatar answered Sep 21 '22 13:09

jscs