2011-05-19 57 views
8

我正在開發一個基本上使用「sox」通過shell腳本創建文件的項目。該應用程序不是基於文檔的,它所做的只是調用一個創建文件的腳本,並且不會在內部保存任何數據。但是,我需要提示用戶在哪裏保存文件,以及在腳本運行之前要使用哪個文件名。有一個「另存爲...」對話框從用戶獲取文件路徑/文件名傳遞給shell腳本的最佳方法是什麼?「另存爲」在基於非文檔的應用程序中可可

回答

21

這很簡單。你用NSSavePanel標記了這個問題,這正是你想要使用的。

- (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 
     } 
    }]; 
} 

另請參閱文件系統編程指南中的"The Save Panel"

相關問題