我正在开发一个项目,该项目使用“sox”通过shell脚本创建文件。该应用程序不基于文档,它只是调用一个创建文件的脚本,并不在内部保存任何数据。但是,在运行脚本之前,我需要提示用户选择文件保存路径和文件名。有什么最好的方法可以弹出“另存为”对话框,以便从用户那里获取文件路径/文件名并传递给shell脚本?
我正在开发一个项目,该项目使用“sox”通过shell脚本创建文件。该应用程序不基于文档,它只是调用一个创建文件的脚本,并不在内部保存任何数据。但是,在运行脚本之前,我需要提示用户选择文件保存路径和文件名。有什么最好的方法可以弹出“另存为”对话框,以便从用户那里获取文件路径/文件名并传递给shell脚本?
这很简单。你使用了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
}
}];
}