On Aug 12, 2009, at 10:10 AM, Jerry Krinock wrote:

Because my application deals with a small number of documents -- most users will have only one, and because these documents have "agents" associated with them and are often not edited, I want my app to warn the user immediately upon opening a document that is in the Trash, instead of the default Cocoa behavior which is to open it and complain later only if the user attempts to save changes.

Unfortunately, the correct way to determine if a file is in a special directory, using Folder Manager function FSDetermineIfRefIsEnclosedByFolder(), does not seem able to determine whether or not a path that I get from the url passed to - [NSDocument readFromURL:ofType:error:] is temporary. The first problem is that Cocoa apparently copies the file to a temporary directory, something like
    /private/var/folders/PR/PRtZlutkFa82jPnfdYcUUk+++TI/-Tmp-/
before opening it, so this path is indeed "not in the trash".

OK, so then I try FSDetermineIfRefIsEnclosedByFolder() again, and again, passing kTemporaryFolderType, kChewableItemsFolderType, kTemporaryItemsInCacheDataFolderType, and kWhereToEmptyTrashFolderType in succession. But it in each case, it returns 'false' -- meaning that the file is not in the specified special directory.

So, finally I resorted to the obviously wrong kludge of getting the "Cocoa" temporary directory from NSTemporaryDirectory() and testing whether or not this is a prefix of the given path. Of course, this works.

What's the correct way to do this?

I faced a very similar issue recently, but on the save side, not the read side. I solved it by overriding one of the Cocoa methods that is called during an NSDocument save operation, in my NSDocument subclass, to get the URL of the final save location, when the document is being moved from the temporary save location to the final save location. I determined which method to override by studying the detailed roadmap of the NSDocument save operation in Apple's "Document-Based Application Overview" document.

Apple's document includes a similar roadmap to NSDocument's read operation. I would guess that you'll find in that roadmap an appropriate method to override in order to obtain the original URL of the document you are reading.


--

Bill Cheeseman
b...@cheeseman.name

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to