Post

Replies

Boosts

Views

Activity

NSDocument disregards save panel!
In a previous post entitled “Save fails after Save As” I described a strange problem involving the Save and Save As operations in a macOS app I wrote: see https://developer.apple.com/forums/thread/779755. Since that posting (unanswered up to now) I tried various modifications of my app, in order better to understand the problem. Now, at the time of that posting I was using a version of the app that attempted — clumsily and incompletely — to circumvent the problem. Since then I decided to eliminate from my app this unsuccessful workaround. My app is called Goperfekt (it’s in the App Store) and is meant for macOS 11 to 15. I recently created a “bare bones” version of the app: this bare-bones version is called Goperf and contains the bare minimum necessary to save and open files of the exact same two file types as in Goperfekt, namely gop (an exported type that conforms to public.data), sgf (an imported type that conforms to public.text). Goperf and Goperfekt both use dataAfType:error: as their writing method. (Yes, Objective-C… but I’ve been working on that app on and off for nearly twenty years and when Swift came out my Obj-C project was already too advanced…) The problem is the following. In Goperfekt under macOS 15 the Save and Save As operations do not work as they should (see description below). In Goperfekt under macOS 12 and 11 the Save and Save As operations work perfectly, just as they should. (Unfortunately I do not have machines running macOS 14 or 13 at the moment.) Goperf, the bare-bones version, on the other hand, works perfectly in all three versions of macOS that I have (11, 12, 15). Here is a description of the problem with Goperfekt under macOS 15. The precise app behavior described below presupposes that the user has activated the option System Settings/Desktop & Dock/Windows/Ask to keep changes when closing documents. If you deactivate this option, the app misbehaves similarly, though somewhat differently. First three important facts (Goperfekt and Goperf in macOS 11, 12, 15): I can open an already existing gop file, modify the document, and save it in that gop file, or save it as another gop file, without any problem. I can also open an already existing sgf file, modify the document, and save it in that sgf file, or save it as another sgf file, without any problem. I can also save a new document as a gop file. BUT in Goperfekt in macOS 15 it is possible neither to save a new document as an sgf file, nor to open a gop file and save it as an sgf file, IN CASES 1 AND 2 the parameter typeName received by dataOfType:error: is not “com.red-bean.sgf” (corresponding to the imported sgf extension) as it should, but “com.florrain.goperfekt-document” (corresponding to the exported gop extension). The result is a file with the sgf extension (such as “A.sgf”, as specified in the save panel), but this file is really a gop file with the wrong extension! You can see that by asking Goperfekt to open “A.sgf” (which will generate an alert), or by opening "A.sgf” in TextEdit. You can also add .gop to the name “A.sgf” and then ask Goperfekt to open “A.sgf.gop”, which it will do without a problem. Nor is it possible to open an sgf file and save it as a gop file. Here the parameter typeName received by dataOfType:error: is not “com.florrain.goperfekt-document” (the exported type) as it should, but “com.red-bean.sgf” (the imported type). The result is a file with the gop extension (such as “A.gop”, as specified in the save panel), but this file is really an sgf file with the wrong extension! You can see that by asking Goperfekt to open “A.gop” (which will generate an alert), or by opening "A.gop” in TextEdit. You can also add .sgf to the name “A.gop” and then ask Goperfekt to open “A.gop.sgf”, which it will do without a problem. Somewhere behind the scenes (only in Goperfekt on macOS 15) NSDocument disregards what was specified by the user in the save panel and sends to dataAfType:error: the wrong file type! Why on Earth? If, after having created a file “A.sgf” that is really a gop file, I change something in the document and try to save this change in “A.sgf”, the system displays a somewhat puzzling alert, and diagnostic messages appear in the Xcode console. According to the circumstances, these messages can contain such puzzling labels as NSFileSandboxingRequestRelatedItemExtension or NSFileCoordinator. Similarly for a file “A.gop” that is really an sgf file. Conclusion: search as I may, I could not find what makes Goperfekt misbehave in macOS 15 but not in macOS 11 or 12, while the bare-bones Goperf behaves perfectly in all three versions.
Topic: UI Frameworks SubTopic: AppKit Tags:
3
0
97
May ’25
Save fails after Save As
I have an app with two file types with the following extensions: gop (an exported type), sgf (an imported type). The Save command fails after the following sequence of events: I open a gop file, say the file "A.gop". I save this file as an sgf file, say "A.sgf". This Save As works perfectly and the document name in the document’s title bar has changed to "A.sgf". I change something in the document and then try to Save this change. This should just resave the document to "A.sgf", but "A.sgf" remains untouched. Instead I get a system alert with the message The document “A.sgf” could not be saved. A file with the name “A.gop” already exists. To save the file, either provide a different name, or move aside or delete the existing file, and try again. In the Xcode console I get the following diagnostic: NSFileSandboxingRequestRelatedItemExtension: an error was received from pboxd instead of a token. Domain: NSPOSIXErrorDomain, code: 2 [NSFileCoordinator itemAtURL:willMoveToURL:] could not get a sandbox extension. oldURL: file:///Users/francois/Desktop/A.sgf, newURL: file:///Users/francois/Desktop/A.gop The problem seems to relate to the sandbox. But I am at a loss to find a solution. (After closing the alert, I check that A.sgf did not register the change.) If I open an sgf file, say "B.sgf", save it as "B.gop", make a change in the document and then try to save this change (into "B.gop"), I hit the same problem, with "gop" and "sgf" interchanged. If, instead of saving "A.gop" as "A.sgf", I save it as "B.sgf", make a change in the document and then try to save this change into "B.sgf", I get the following system alert: The document “B.sgf” could not be saved. You don’t have permission. To view or change permissions, select the item in the Finder and choose File > Get Info. And in the Xcode console I get the following diagnostic: NSFileSandboxingRequestRelatedItemExtension: an error was received from pboxd instead of a token. Domain: NSPOSIXErrorDomain, code: 2 [NSFileCoordinator itemAtURL:willMoveToURL:] could not get a sandbox extension. oldURL: file:///Users/francois/Desktop/B.sgf, newURL: file:///Users/francois/Desktop/B.gop Again the sandbox ! (After closing the alert, I check that B.sgf did not register the change.) It’s clear my code is missing something, but what?
0
0
72
Apr ’25
In Save Panel, can’t control the file extension
My Mac app (written in Objective-C) can save a document in two file formats: one with an exported type identifier (extension "gop"), and another with an imported file identifier (extension "sgf"). I want the user to be able to choose in the app’s preferences the default file save format they prefer. My prepareSavePanel: method (see below) reads the user’s preference in this regard, lists the two formats in the correct order — chosen default first. The popup menu in the save panel I get conforms to my expectations (formats in the correct order), BUT the file extension I get in the file name field after the defaultDraftName is always "gop"! Is there any way to get the app to select the extension "sgf" in the save panel popup menu when the user prefers "sgf"? - (BOOL)prepareSavePanel:(NSSavePanel *)savePanel { if (@available(macOS 11.0, *)) { NSArray<UTType *> *UTTypes = nil; UTType * const myTypeGOP = [UTType typeWithFilenameExtension:@"gop"]; UTType * const myTypeSGF = [UTType typeWithFilenameExtension:@"sgf"]; NSInteger const i = [FLPreferenceControler defaultDocumentType]; if (i <= 0) { UTTypes = [NSArray arrayWithObjects:myTypeGOP, myTypeSGF, nil]; } else { /* i > 0. */ UTTypes = [NSArray arrayWithObjects:myTypeSGF, myTypeGOP, nil]; } [savePanel setAllowedContentTypes:aUTTypes]; } else { /* macOS < 11.0. */ NSArray <NSString *> * const UTIs = [self writableTypesForSaveOperation:0]; /* This method does the same as above, but for UTIs instead of UTType’s. */ [savePanel setAllowedFileTypes:UTIs]; } return YES; }
0
0
533
Mar ’23
How to draw in background window
Hello! I’m writing a macOS app. I have a custom view in every document window. Suppose I have two documents open at the same time. Call the two corresponding windows A and B. There are often long background calculations going on in both documents. Suppose A is main. Now, whenever the window behind, B, updates its custom view to reflect the calculations’ progress, B is immediately brought in front of A, but without becoming main! Sometimes both windows will automatically alternate one in front of the other! How can I draw in the custom view of B, the window behind, without bringing it in front of A?
3
0
799
Nov ’20