"Save As" in non-document based application Cocoa
Asked Answered
K

1

8

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?

Knitted answered 19/5, 2011 at 21:23 Comment(0)
H
21

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.

Hamulus answered 19/5, 2011 at 21:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.