Disabling save menus for a document

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?

Are you using NSDocument (a document-based application)?

Yes.

You should set

for this particular document, so when your document window is frontmost the “Save” command will be disabled.

How would I format that in AS Obj-C?

theDocument’s updateChangeCount_(2)

NSChangeCleared=2 (reset all changes)

And what exactly is that doing?

How would I reverse that?

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.

I’m not sure I follow that. By using that am I telling the document that there are no changes? What I’m really trying to do is not allow:

Save
Save As
Close

Until they get out of this special edit mode.

But I do still need the document to realize there are changes and prompt a save once they are out of that mode.

Does that make sense? Is that possible…?

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.

Would that go into the document script?

Yes.

WOW, that works pretty good.

“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…

In the document .xib, the document is represented by File’s Owner.

So, I dragged the Window up to File’s Owner… and chose delegate. Then I put this in my document script:

on windowShouldClose_(sender)
	log "windowShouldClose_"
	return gBlnEditMode
end

Nothing logs and the window can still be closed and then saved…

??

Choose the Identity inspector and make sure File’s Owner’s class is your document class – it won’t be if you changed from the default name.

Yes, it is.

I just pasted your code into a project here, changed gBlnEditMode to false, and it works fine. So there has to be something else at work.