在非基于文档的应用程序Cocoa中“另存为”

前端之家收集整理的这篇文章主要介绍了在非基于文档的应用程序Cocoa中“另存为”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个基本上通过 shell脚本使用“sox”创建文件的项目.该应用程序不是基于文档的,它所做的只是调用创建文件的脚本,而不是在内部保存任何数据.但是,我需要提示用户保存文件的位置,以及在运行脚本之前要使用的文件名.使用“另存为…”对话框从用户获取文件路径/文件名以传递给shell脚本的最佳方法是什么?
这非常简单.你用 NSSavePanel标记了这个问题,这正是你想要使用的.
  1. - (IBAction)showSavePanel:(id)sender
  2. {
  3. NSSavePanel * savePanel = [NSSavePanel savePanel];
  4. // Restrict the file type to whatever you like
  5. [savePanel setAllowedFileTypes:@[@"txt"]];
  6. // Set the starting directory
  7. [savePanel setDirectoryURL:someURL];
  8. // Perform other setup
  9. // Use a completion handler -- this is a block which takes one argument
  10. // which corresponds to the button that was clicked
  11. [savePanel beginSheetModalForWindow:someWindow completionHandler:^(NSInteger result){
  12. if (result == NSFileHandlingPanelOKButton) {
  13. // Close panel before handling errors
  14. [savePanel orderOut:self];
  15. // Do what you need to do with the selected path
  16. }
  17. }];
  18. }

另请参见“文件系统编程指南”中的“The Save Panel”.

猜你在找的Bash相关文章