如何让基于 NSDocument 的应用程序打开文件夹,但将关联的文档保存到应用程序支持?
在我的基于 NSDocument 的应用程序中,我的文档将与目录关联(它们是目录的索引),
我希望能够根据以下用例打开/保存文档:
打开目录: a) 如果文件夹中有.myapp文件,打开它 b) 否则查看 ~/Library/Application Support/MyApp 中是否有 HASH-OF-PATH-basename.myapp 文件并打开该文件 b) 否则在内存中创建一个新文档
照常打开 xxx.myapp 文件
保存/自动保存 a) 如果当前文档的源是 1b、1c 或 2,则将文件保存到 ~/Library/Application Support/MyApp/HASH-OF-PATH-basename.myapp b) 如果当前文档的源是 1a,则保存到该 .myapp 文件
另存为/另存到 b) 将 xxxxx.myapp 保存到用户指定的任何位置
显而易见的事情似乎只是实现 readFromURL:ofType:error:L 和 writeToURL:ofType:forSaveOperation:originalContentsURL:error: 并根据保存操作的内容根据需要切换 URL 。
这是因为我可以打开文件和文件夹,当我保存打开的文件夹时,我的文件将保存到应用程序支持,但在 writeToURL:ofType:forSaveOperation:originalContentsURL:error 之后,会引发错误并显示一个包含以下错误的对话框:
NSDocument could not delete the temporary item at
file://localhost/private/var/folders/6P/6PNpIB-6HreGE+Ikqf5dWU+++TI/
-Tmp-/TemporaryItems/(A%20Document%20Being%20Saved%20By%20MyApp%203)
/SomeDirectory.
Here's the error:
Error Domain=NSCocoaErrorDomain Code=4 UserInfo=0x200583a00
"“SomeDirectory” couldn’t be removed."
In my NSDocument based app, my documents are to be associated with directories (they are an index of the directory)
I want to be able to open/save documents according to the following use case:
Opening a directory:
a) if there is a .myapp file in the folder, open it
b) else look if there is HASH-OF-PATH-basename.myapp file in ~/Library/Application Support/MyApp and open that
b) else create a new document in memoryOpening an xxx.myapp file, as normal
Save / Autosave
a) if source of current doc was 1b, 1c, or 2 save file to ~/Library/Application Support/MyApp/HASH-OF-PATH-basename.myapp
b) if source of current doc was 1a, save to that .myapp fileSave As / Save To
b) save an xxxxx.myapp to wherever the user specifies
The obvious thing seemed to be just to implement readFromURL:ofType:error:L and writeToURL:ofType:forSaveOperation:originalContentsURL:error: and switch URL as necessary based on what the saveoperation was.
This works in that I can open files and folders, and when I save an open folder my file is saved to Application Support, but then after writeToURL:ofType:forSaveOperation:originalContentsURL:error an error is raised and a dialog shown with the following error:
NSDocument could not delete the temporary item at
file://localhost/private/var/folders/6P/6PNpIB-6HreGE+Ikqf5dWU+++TI/
-Tmp-/TemporaryItems/(A%20Document%20Being%20Saved%20By%20MyApp%203)
/SomeDirectory.
Here's the error:
Error Domain=NSCocoaErrorDomain Code=4 UserInfo=0x200583a00
"“SomeDirectory” couldn’t be removed."
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,这个问题很容易解决。
我覆盖了 saveToURL:ofType:forSaveOperation:error: 并执行了 url &类型交换基于 SaveOperation 类型。
现在一切都按预期进行。
This turned out to be fairly easy to resolve.
I overrode saveToURL:ofType:forSaveOperation:error: and did the url & type swapping based on SaveOperation type there.
Now things are working as expected.