33635 - undo menuitem in TextArea

The answer to that case is:

Stephane Pinel Today at 09:36
This case has been closed because the behavior described is not a bug. “Undo” menu item
is all about undoing what a user manually did. Not an action performed by code.

I added:
This is false.

Run TextEdit, add a brand new window,
Run AppleScript Editor;

Copy / Paste in the AppleScript Editor:

tell application "TextEdit" set fw to document 1 set text of fw to "Ceci est un texte." end tell

Run,

Go back inside TextEdit,
Issue Cmd-Z

and see the pasted text undoed.

And after I sent that I realized that since the case is closed, I’d better send the answer here instead of letting it dying in a closed feedback.

The whole point of AppleScript is that it appears to the app as user input. So you’re not undoing anything “performed by code”.

If you insert code into TextArea.Text you never give a chance to the system to know about it, as it does not go through the keyboard. So it cannot manage the undo of an event it does no know about. Indeed it is not a bug. To have the system manage undo would require using AppleScript through a shell with osascript to enter “Ceci est un texte.”

When you add text by direct manipulation of the Text property, you have to manually manage the undo action yourself, by storing the current state of the Text property before you insert the text.

For instance, add TextArea1 to a window with initial text of "Dja texte. " and this code :

  • Window property

undo1 As string

  • Menu handler :

Function EditUndo() As Boolean TextArea1.Text = undo1 Undo1 = "" EditUndo.Enabled = False Return True End Function

  • Button :

Sub Action() Undo1 = TextArea1.Text TextArea1.Text = TextArea1.Text+"Ceci est un texte" EditUndo.Enabled = true End Sub

When you run, the first thing to notice is that Undo is enabled.

Then, when you click on the button, the text appears in the TextArea. If you select Edit/Undo, your text disappears and . you go back to “Dja texte”. If you click again, “Dja Texte” disappears too. This is one level of undo but several can be managed with an array.

I did not think about entering text by the keyboard after the Text property insert.

Here is a corrected code :

Window property :

normalundo As boolean = false

Menu handler :

Function EditUndo() As Boolean if normalundo = true then return false normalundo = true TextArea1.Text = undo1 Undo1 = "" EditUndo.Enabled = False Return True End Function

Now if you type normally the system Undo takes over.

I used AppleScript because I do not have “on hand” a code creator to check the assertion.

Anyone care to use XCode to check that ?

[quote=91421:@Emile Schwarz]I used AppleScript because I do not have “on hand” a code creator to check the assertion.

Anyone care to use XCode to check that ?[/quote]

Emile, have you read my post ?

Sorry Michel, I saw it, but I do not really read it, yet.

OK, done. Thank you.