Re: NSDocument Save As... problem?

2015-02-02 Thread Graham Cox

> On 3 Feb 2015, at 11:58 am, Quincey Morris 
>  wrote:
> 
>> The app supports 4 document types. It imports all 4 UTIs corresponding to 
>> these types, but it exports only two. 
> 
> This doesn’t sound right to me. My understanding is that a UTI string is one 
> of three things:
> 


You know, I'm having a strong sensation of deja-vu. I've fallen foul of this 
before - basically mixing up import and export UTIs.

When I see "Import" it suggests to me something I'm bringing into my app, and 
export is something i'm moving from my app into the world. Since UTIs are 
associated with document file types, it seems natural to suppose that this 
refers to reading (import) and writing (export) those files. But this not what 
import and export of UTIs means at all. I did get all this sorted out in my 
mind once before relating to another app, but time passes and I'd forgotten it 
all again (I'm finding that's happening a lot lately, but anyway).

I think having had the lightbulb go on (again), I can sort that out.

> The rest of the document seems to contradict this by repeatedly referring to 
> multiple types per subclass, but it *could* mean that the others are all 
> import/export types. It’s possible the pop-up only appears if there’s more 
> than one subclass.
> 

Well, in fact the file types I can read are variants of the same thing in that 
they all end up producing the same data model, so I can open them all using the 
same document subclass. In other apps I've had the format pop-up appear with a 
single subclass, so that isn't a necessary trigger.

> I’d go to the highest level — specifically the top level that has “fileType” 
> as an explicit parameter. Otherwise you risk inconsistency between use of the 
> original fileType and your substituted fileType *after* you return from 
> whatever method you override.

Yes, I see what you're saying. But I think I can fix things correctly by 
sorting out my incorrect UTIs, so I'll definitely try that before pulling apart 
the document stuff.

--Graham



___

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

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

Re: NSDocument Save As... problem?

2015-02-02 Thread Quincey Morris
On Feb 2, 2015, at 15:21 , Graham Cox  wrote:
> 
> The app supports 4 document types. It imports all 4 UTIs corresponding to 
> these types, but it exports only two.

This doesn’t sound right to me. My understanding is that a UTI string is one of 
three things:

1. A standard UTI, as defined by Apple. The most common of these have symbolic 
names, the rest you have to supply a string with the documented contents.

2. An app-specific UTI, as defined by your app. “Defined” means that your app 
owns and knows the format of the data in a file that has this UTI. Such a UTI 
is *exported* by your app, in the Exported UTIs section of the plist. It may be 
used by just this app, or by other apps as well.

3. A non-standard UTI, as defined by some other app or standard. Such a UTI is 
*imported* by your app if it want to recognize it, in the Imported UTIs section 
of the plist. You include it to ensure that Launch Services knows about the 
UTI, even if the defining application isn’t on your Mac.

So, if your app has 4 document types with UTIs, then there should only be 4 UTI 
declarations total, regardless of whether they’re exported or imported. OTOH, 
I’ve never seen any evidence that Launch Services cares if a definition is in 
both places.

> My Save dialog is not showing the format pop-up

> Alternatively, adding that type as an exported UTI and keeping its role as 
> "editor", I expected to see the format pop-up, but I don't. Why? Don't know.

I can’t tell from the documentation whether there should be a different 
NSDocument subclass for each “natural” document type. The introductory 
discussion seems to say so:


https://developer.apple.com/library/mac/documentation/DataManagement/Conceptual/DocBasedAppProgrammingGuideForOSX/Designing/Designing.html
 


like this:

> A Data Model Corresponds to a Document Type
> 
> … Each document object is a custom subclass of NSDocument designed 
> specifically to handle a particular type of data model. Document-based apps 
> are able to handle one or more types of documents, each with its own type of 
> data model and corresponding NSDocument subclass. Apps use an information 
> property list file, which is stored in the app’s bundle and named, by 
> default, -Info.plist, to specify information that can be used at 
> runtime. Document-based apps use this property list to specify the document 
> types the app can edit or view.

The rest of the document seems to contradict this by repeatedly referring to 
multiple types per subclass, but it *could* mean that the others are all 
import/export types. It’s possible the pop-up only appears if there’s more than 
one subclass.

> I do not have a NSExportableTypes entry - in fact I don't think I've heard of 
> that before, I thought it was figured out from the UTIs and/or the document 
> types (who knows, it's a rabbit warren of interconnected dependencies with no 
> clear canonical source of information that I can see).

Exactly. I never remember this either. It’s for types you can save to, but not 
open as an editor.

> I could try one step up at the URL level, which may be enough, without having 
> to go as high as the document controller level.

I’d go to the highest level — specifically the top level that has “fileType” as 
an explicit parameter. Otherwise you risk inconsistency between use of the 
original fileType and your substituted fileType *after* you return from 
whatever method you override.



___

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

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

Re: NSDocument Save As... problem?

2015-02-02 Thread Graham Cox

> On 3 Feb 2015, at 10:02 am, Quincey Morris 
>  wrote:
> 
> On Feb 2, 2015, at 14:06 , Graham Cox  wrote:
>> 
>> If my NSDocument subclass opens a file of a certain type, and then I do a 
>> Save As (option click the File menu), choose a new name and CHANGE THE 
>> EXTENSION, shouldn't the document be given the new typeName in 
>> -fileWrapperOfType:error: ?
> 
> I don’t think so, but I’m not sure. I think you’re supposed to convince 
> NSDocument to get the right type from the save panel. For example, it says 
> here:
> 
>   
> https://developer.apple.com/library/mac/documentation/DataManagement/Conceptual/DocBasedAppProgrammingGuideForOSX/AdvancedTopics/AdvancedTopics.html
> 
> this:
> 
>> Customizing the Save Dialog
>> 
>> By default, when NSDocument runs the Save dialog and the document has 
>> multiple writable document types, NSDocument inserts an accessory view near 
>> the bottom of the dialog. This view contains a pop-up menu of the writable 
>> types.
> 
> and I think you get this pop-up menu when you have multiple document types in 
> the app, or when you have “NSExportableTypes” types. I believe I’ve used this 
> in the past, and IIRC you get the type from the pop-up, even if you force the 
> actual file extension to conflict — which sounds like what’s happening to 
> you, except with an implicit pop-up of only one type. (But it would have been 
> pre-UTI days when I did this, so take a grain of salt with it.)


I think I may have got my UTIs slightly wrong - no surprise, they're really 
annoying, hard to understand and fragile.

The app supports 4 document types. It imports all 4 UTIs corresponding to these 
types, but it exports only two. One of the document types' role was set to 
"editor", but I did not have this as a corresponding exported UTI. My Save 
dialog is not showing the format pop-up (I'm also adding my own accessory view, 
but that should be in addition to any format pop-up I believe). I do not have a 
NSExportableTypes entry - in fact I don't think I've heard of that before, I 
thought it was figured out from the UTIs and/or the document types (who knows, 
it's a rabbit warren of interconnected dependencies with no clear canonical 
source of information that I can see).

Changing the role to "viewer", it allows the file to be opened, and then when 
it's saved its type is changed and it gets saved in the new format - I don't 
have to mess about with extensions in the Save dialog. So I probably should 
have had that set as "viewer" all along. Alternatively, adding that type as an 
exported UTI and keeping its role as "editor", I expected to see the format 
pop-up, but I don't. Why? Don't know.

> TBH, your question may be one of those issues it’s no longer possible to 
> adjudicate, because NSDocument has become so layered with obscurity over the 
> years, in the “interests” of preserving backward compatibility.

Yup, it's become fiendishly complex.

> One possible alternative would be to override the highest-level 
> NSDocumentController save method (which might be 
> ‘saveToURL:ofType:forSaveOperation:completionHandler:’), and pass a different 
> file type to super, using the URL file name to choose the type.

It might come to that. At the moment I override NSDocument at the file wrapper 
level. I could try one step up at the URL level, which may be enough, without 
having to go as high as the document controller level. But it says something 
about how complicated NSDocument has become that what used to be simple is now 
easier to do by completely avoiding it altogether.

--Graham




___

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

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

Re: NSDocument Save As... problem?

2015-02-02 Thread Quincey Morris
On Feb 2, 2015, at 14:06 , Graham Cox  wrote:
> 
> If my NSDocument subclass opens a file of a certain type, and then I do a 
> Save As (option click the File menu), choose a new name and CHANGE THE 
> EXTENSION, shouldn't the document be given the new typeName in 
> -fileWrapperOfType:error: ?

I don’t think so, but I’m not sure. I think you’re supposed to convince 
NSDocument to get the right type from the save panel. For example, it says here:


https://developer.apple.com/library/mac/documentation/DataManagement/Conceptual/DocBasedAppProgrammingGuideForOSX/AdvancedTopics/AdvancedTopics.html
 


this:

> Customizing the Save Dialog
> 
> By default, when NSDocument runs the Save dialog and the document has 
> multiple writable document types, NSDocument inserts an accessory view near 
> the bottom of the dialog. This view contains a pop-up menu of the writable 
> types.


and I think you get this pop-up menu when you have multiple document types in 
the app, or when you have “NSExportableTypes” types. I believe I’ve used this 
in the past, and IIRC you get the type from the pop-up, even if you force the 
actual file extension to conflict — which sounds like what’s happening to you, 
except with an implicit pop-up of only one type. (But it would have been 
pre-UTI days when I did this, so take a grain of salt with it.)

TBH, your question may be one of those issues it’s no longer possible to 
adjudicate, because NSDocument has become so layered with obscurity over the 
years, in the “interests” of preserving backward compatibility.

One possible alternative would be to override the highest-level 
NSDocumentController save method (which might be 
‘saveToURL:ofType:forSaveOperation:completionHandler:’), and pass a different 
file type to super, using the URL file name to choose the type.



___

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

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