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.
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.