I need to have a document not be able to be saved when it is in a certain state. So, I want to disable the save menus but only for a SINGLE DOCUMENT at a time.
I imagine I would use setEnabled_ but I’m not sure how to do that from a document script/class when they are in the application.xib and that also seems like it would affect all open documents.
Is there a way to make it impossible to save a single document temporarily and the reenable that later?
The “dirty flag” of a NSDocument is set when the user changes something in the document. The “red bubble” of the window is marked with a black dot and the changes have to be saved (all this is automatically managed by the framework).
You “revert” this by letting the framework do its normal job, that is, by not calling your method to clear the changes.
I’ve been warned sometimes in this site NOT to fight against the framework, and I always realized that people saying that where right. By introducing non-standard features, you may confuse your user and have to write a lot of documentation full of warnings.
The canonical way is to override validateUserInterfaceItem_ in your document class. Basically, every time a menu item has to be drawn, this gets called to see if it should be dimmed or not. So something like this (untested):
on validateUserInterfaceItem_(anItem)
set theSel to anItem’s action() as text
if theSel = “Save:” then
if notNowFlag = true then return false – your test
end if
– pass on to the superclass
return (continue validateUserInterfaceItem_(anItem))
end validateUserInterfaceItem_
Keep in mind that this code gets called a lot, so it needs to be as efficient as possible.
“saveDocument:”, “saveDocumentAs:” and “revertDocumentToSaved:” come up and can easily be overridden using that subrouitne! Thanks!! However, close never seems to appear in the log when I log the options. So, the user can still hit close which prompts them to save and then forces them into a save as situation (not through menus but a dialog).
I don’t suppose there is a way around that? I notice canCloseDocumentWithDelegate:shouldCloseSelector:contextInfo: in the documentation but that confuses me a little… and, is that a routine that is hit automatically like the one above if present in the script? Perhaps there is another one I’m overlooking?
I am curious, how does one know, looking at the documentation that this routine can be placed into code and it will automatically get hit by the app?
Man this stuff is confusing and yet exciting at the same time…!!!
That’s because close is targeted at the window, not the document. You can make you document the delegate of its window and implement the window delegate method windowShouldClose_.
Search for “Introduction to User Interface Validation”.
Yep. It’s hard getting started after ASStudio, but once you get into it you realise how much more you can do with it.
Sorry, but how do I make the document the delegate of its window…? I imagine I drag the window to something or something to the window… but is the document represented in the placeholders area or do I need to add a blue cube. Just not 100% sure here…