I don't know a way to do that, I think maybe you should make your own 'undo' button in the script that will trigger the recorded actions
I found these set of commands while searching for something else, looks like it could help
I don't know how to trigger them but please share if they were helpful