Holy cow, I wrote a book!
Many people like to simulate user actions programmatically.
I'm not going to comment here on whether that's a good idea to begin with;
what I want to point out is that if you're going to do it,
you have to make sure you're simulating things the user can actually do.
For example, if you want to act as if the user clicked the "close"
button in the corner, you have to make sure that the window is
visible and enabled and that the window even has a "close" button
to begin with!
Failing to observe these rules means that
the application you are manipulating is going to experience
The window may very well have been disabled
in order to avoid the problem of the unsupported stack frame.
Programmatically closing the window means that you've reintroduced
the bug that the application programmer was trying to avoid.
Similarly, if you want to simulate menu clicks, you need to check
whether the menu item is enabled because a user can't select
a disabled menu item.
Since most programs do not synchronize menu item states
until the menu is opened
(since the only way a user can invoke a menu item is to open the
enclosing menu first),
you need to open the menu before you check whether the item
There are even more subtle conditions.
For example, you might be dealing with a program that
stops updating its status when the window is not visible,
either because it is covered by another window or because the
user has disconnected the session or locked the workstation.
Under those conditions, the user can't interact with the program,
and simulating such activity may result in the program behaving
strangely because you're doing something "impossible".
In the referenced article, the program wanted to set foreground
activation to a particular window,
but it didn't check whether the window was enabled or even visible!
(And even if it were enabled and visible, it might be a
My goal here is not to come up with a comprehensive list of things
to check before simulating every type of action you might want.
That's something you need to figure out on your own:
Look at what you're trying to simulate and think through what
the window manager requires for that action to be accessible to the user.