There's a file type my application import but not save. I've added an entry to the document types and set it to read-only, but that doesn't yield the import behaviour that I'm looking for. Instead, my app will just open the file and when I save the original file is overwritten in my own file format.
有一个文件类型我的应用程序导入但不保存。我已经在文档类型中添加了一个条目并将其设置为只读,但这并不会产生我正在寻找的导入行为。相反,我的应用程序将只打开文件,当我保存原始文件时,将以我自己的文件格式覆盖。
How to set up my document or document types to make it so that a new document is created with the data from the original document, instead of the original being opened?
如何设置我的文档或文档类型,以便使用原始文档中的数据创建新文档,而不是打开原始文档?
3 个解决方案
#1
1. Declare the file types as Document Types
Within your Xcode project, add a Document Type for all the file formats your application supports. Set the Role of each type according to your application's abilities:
在Xcode项目中,为应用程序支持的所有文件格式添加文档类型。根据应用程序的能力设置每种类型的角色:
- Mark read/write capable file types as Editor;
- Mark import only file types as Viewer.
将具有读/写功能的文件类型标记为编辑器;
标记仅将文件类型导入为Viewer。
Set the Class to the document type you want to handle each file type. One document class can handle multiple file types.
将Class设置为要处理每种文件类型的文档类型。一个文档类可以处理多种文件类型。
In the example below, there are three file types declared: font-pestle, otf, and ttf. The first, font-pestle, is the native format of the application. This type has the role Editor.
在下面的示例中,声明了三种文件类型:font-pestle,otf和ttf。第一个是font-pestle,是应用程序的原生格式。此类型具有角色编辑器。
The remaining two formats, otf and ttf, can be imported but not written by the application; thus they are marked as Viewer.
其余两种格式otf和ttf可以导入但不能由应用程序写入;因此它们被标记为查看者。
2. Additional file types in your NSDocument subclass
With the Document Types added, the application will automatically allow users to open files of the specified types.
添加文档类型后,应用程序将自动允许用户打开指定类型的文件。
You need to add file type handling code to your document class. In the ideal case, add the branching code to the readFromData:ofType:error:
method:
您需要将文件类型处理代码添加到文档类。在理想情况下,将分支代码添加到readFromData:ofType:error:方法:
- (BOOL)readFromData:(NSData*)someData ofType:(NSString*)typeName error:(NSError**)outError
{
if ([NSWorkspace.sharedWorkspace type:@"eu.miln.font-pestle" conformsToType:typeName] == YES)
{
// read native format
}
else if ([NSWorkspace.sharedWorkspace type:@"public.opentype-font" conformsToType:typeName] == YES)
{
// read import only format
// disassociate document from file; makes document "untitled"
self.fileURL = nil;
// associate with primary file type
self.fileType = @"eu.miln.font-pestle";
}
else // ...
}
The self.fileURL = nil;
is important. By setting fileURL to nil, you are saying the document is not associated with any on-disk file and should be treated as a new document.
self.fileURL = nil;很重要通过将fileURL设置为nil,您说该文档与任何磁盘文件都没有关联,应该被视为新文档。
To allow auto-saving, implement the NSDocument method autosavingFileType
to return the primary file type.
要允许自动保存,请实施NSDocument方法autosavingFileType以返回主文件类型。
#2
Alex, thanks for your answer, but I found a way that I like a bit more:
亚历克斯,谢谢你的回答,但我发现了一种我更喜欢的方式:
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName
error:(NSError **)outError
{
*outError = nil;
if ([typeName isEqualToString:@"SomeReadOnlyType"])
{
// .. (load data here)
[self setFileURL:nil];
return result;
}
else
{
// .. (do whatever you do for other documents here)
}
}
This way it's still possible to use the document system provided by Cocoa instead fo rolling my own.
这样,仍然可以使用Cocoa提供的文档系统来代替我自己的文档系统。
I've also documented the solution here: http://www.cocoadev.com/index.pl?CFBundleTypeRole a bit down the page.
我还在这里记录了解决方案:http://www.cocoadev.com/index.pl?CFBundleTypeRole在页面下方。
#3
I don't believe that import functionality is supported by default in Cocoa. When the user clicks the Open button in the open panel, the framework calls openDocumentWithContentsOfURL:display:error:
on NSDocumentController
. This is where the document system figures out what type of file you're opening and consults with the Info.plist file to figure out which NSDocument
subclass to use to open the document.
我不相信Cocoa默认支持导入功能。当用户单击打开面板中的“打开”按钮时,框架会在NSDocumentController上调用openDocumentWithContentsOfURL:display:error :.这是文档系统确定您打开的文件类型的位置,并与Info.plist文件协商以确定用于打开文档的NSDocument子类。
You could subclass NSDocumentController
and override the openDocumentWithContentsOfURL:display:error:
method to intercept the file types that should be imported rather than opened. In your NSDocument
subclass, write a new initializer with a name like initWithImportedContentsOfURL:type:error:
(or something with a better name :-) ) to create a new untitled document and read in the contents of the imported file.
您可以继承NSDocumentController并覆盖openDocumentWithContentsOfURL:display:error:方法来拦截应该导入而不是打开的文件类型。在NSDocument子类中,编写一个名为initWithImportedContentsOfURL的新初始化程序:type:error :(或名称更好的东西:-))以创建新的无标题文档并读入导入文件的内容。
#1
1. Declare the file types as Document Types
Within your Xcode project, add a Document Type for all the file formats your application supports. Set the Role of each type according to your application's abilities:
在Xcode项目中,为应用程序支持的所有文件格式添加文档类型。根据应用程序的能力设置每种类型的角色:
- Mark read/write capable file types as Editor;
- Mark import only file types as Viewer.
将具有读/写功能的文件类型标记为编辑器;
标记仅将文件类型导入为Viewer。
Set the Class to the document type you want to handle each file type. One document class can handle multiple file types.
将Class设置为要处理每种文件类型的文档类型。一个文档类可以处理多种文件类型。
In the example below, there are three file types declared: font-pestle, otf, and ttf. The first, font-pestle, is the native format of the application. This type has the role Editor.
在下面的示例中,声明了三种文件类型:font-pestle,otf和ttf。第一个是font-pestle,是应用程序的原生格式。此类型具有角色编辑器。
The remaining two formats, otf and ttf, can be imported but not written by the application; thus they are marked as Viewer.
其余两种格式otf和ttf可以导入但不能由应用程序写入;因此它们被标记为查看者。
2. Additional file types in your NSDocument subclass
With the Document Types added, the application will automatically allow users to open files of the specified types.
添加文档类型后,应用程序将自动允许用户打开指定类型的文件。
You need to add file type handling code to your document class. In the ideal case, add the branching code to the readFromData:ofType:error:
method:
您需要将文件类型处理代码添加到文档类。在理想情况下,将分支代码添加到readFromData:ofType:error:方法:
- (BOOL)readFromData:(NSData*)someData ofType:(NSString*)typeName error:(NSError**)outError
{
if ([NSWorkspace.sharedWorkspace type:@"eu.miln.font-pestle" conformsToType:typeName] == YES)
{
// read native format
}
else if ([NSWorkspace.sharedWorkspace type:@"public.opentype-font" conformsToType:typeName] == YES)
{
// read import only format
// disassociate document from file; makes document "untitled"
self.fileURL = nil;
// associate with primary file type
self.fileType = @"eu.miln.font-pestle";
}
else // ...
}
The self.fileURL = nil;
is important. By setting fileURL to nil, you are saying the document is not associated with any on-disk file and should be treated as a new document.
self.fileURL = nil;很重要通过将fileURL设置为nil,您说该文档与任何磁盘文件都没有关联,应该被视为新文档。
To allow auto-saving, implement the NSDocument method autosavingFileType
to return the primary file type.
要允许自动保存,请实施NSDocument方法autosavingFileType以返回主文件类型。
#2
Alex, thanks for your answer, but I found a way that I like a bit more:
亚历克斯,谢谢你的回答,但我发现了一种我更喜欢的方式:
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName
error:(NSError **)outError
{
*outError = nil;
if ([typeName isEqualToString:@"SomeReadOnlyType"])
{
// .. (load data here)
[self setFileURL:nil];
return result;
}
else
{
// .. (do whatever you do for other documents here)
}
}
This way it's still possible to use the document system provided by Cocoa instead fo rolling my own.
这样,仍然可以使用Cocoa提供的文档系统来代替我自己的文档系统。
I've also documented the solution here: http://www.cocoadev.com/index.pl?CFBundleTypeRole a bit down the page.
我还在这里记录了解决方案:http://www.cocoadev.com/index.pl?CFBundleTypeRole在页面下方。
#3
I don't believe that import functionality is supported by default in Cocoa. When the user clicks the Open button in the open panel, the framework calls openDocumentWithContentsOfURL:display:error:
on NSDocumentController
. This is where the document system figures out what type of file you're opening and consults with the Info.plist file to figure out which NSDocument
subclass to use to open the document.
我不相信Cocoa默认支持导入功能。当用户单击打开面板中的“打开”按钮时,框架会在NSDocumentController上调用openDocumentWithContentsOfURL:display:error :.这是文档系统确定您打开的文件类型的位置,并与Info.plist文件协商以确定用于打开文档的NSDocument子类。
You could subclass NSDocumentController
and override the openDocumentWithContentsOfURL:display:error:
method to intercept the file types that should be imported rather than opened. In your NSDocument
subclass, write a new initializer with a name like initWithImportedContentsOfURL:type:error:
(or something with a better name :-) ) to create a new untitled document and read in the contents of the imported file.
您可以继承NSDocumentController并覆盖openDocumentWithContentsOfURL:display:error:方法来拦截应该导入而不是打开的文件类型。在NSDocument子类中,编写一个名为initWithImportedContentsOfURL的新初始化程序:type:error :(或名称更好的东西:-))以创建新的无标题文档并读入导入文件的内容。