mr_unreliable
Mon Jan 28 12:50:20 CST 2008
Andy, all the other responders are correct in stating that
using Appactivate and SendKeys is an invitation to anxiety,
frustration, despair and ultimately failure.
Back in the days before IM and AV popups (telling you to go
get the latest rule set) and various other system activity
out of your control, appactivate and sendkeys may have worked
reliably enough. In the current environment we live in, you
can expect unreliable results.
The problem is that sendkeys acts like the system keyboard,
and sends keystrokes to the "active" (has system focus) app.
As has already been said, you may attempt to set your app
as active, but then something can happen to steal the focus
away.
There are other problems. Even on the newer very fast
processors with very fast hard drives, apps may take a while
to load. And also, apps sometimes have a "mind-of-their-own"
and only respond (if at all) when they get around to it, or
when some other "processor hog" gives up the processor so
your app can have a go. So there are other reasons for the
appactivate/sendkeys approach to fail, even if no IM's or
other popups crop up. Your keystrokes will go to some other
app, either to be ignored -- or worse, to cause some
irreparable damage.
The only way to really get what you want is to use the system
api. (O.K. all you professional sysAdmins, stifle your groans).
The reason for this is when using the system api, you deal with
the app and its controls via their system "handles". And so
when you send a message to a textbox (a.k.a. and "edit" control)
then that message is sent only to the handle (window or control)
you specify, and not to whatever window currently has focus.
Calling the system api is verboten from script. To get around
this limitation you have two choices. You can use a third-party
control (like DynaWrap) which allows for calling api's from script.
Or, you can use some other scripting language which DOES allow for
calling the system api (there are several: AutoIt and FBSL for two).
cheers, jw
____________________________________________________________
You got questions? WE GOT ANSWERS!!! ..(but,
no guarantee the answers will be applicable to the questions)
--- <DynaWrap Boilerplate> ---
It is possible to declare-and-call an api from script,
but you must use a third-party control to do so,
or else write one yourself.
It has already been correctly pointed out that there
is no api-capability in "pure" script.
If you are willing to use a third-party control, then
one such control, called "DynaWrap", can be found on
Guenter Born's website (note: Guenter refers to it as
"DynaCall"). Here is the link to it:
http://people.freenet.de/gborn/WSHBazaar/WSHDynaCall.htm
- or -
http://ourworld.compuserve.com/homepages/Guenter_Born/WSHBazaar/WSHDynaCall.htm
On that page you will find a download for the control,
plus some code samples.
Note: you may find additional sample code by searching
the archives of the wsh and vbscript ng's.
Note also: DynaWrap does have its limitations. There are
certain things it can't do. For example, you can't call
api's "by ordinal". (I had previously asserted that you
can't use DynaWrap to call api's which take typedefs as
parameters, but I have been proven wrong on that --
although it took me about two weeks to figure out how it
was done). Regardless, Dynawrap will work for most of
"the usual suspects".
And finally, DynaWrap doesn't work entirely as advertised.
For example, it is supposed to allow for the declaration of
several api definitions in one instance of itself. I could
never get that to work (in win9x -- but others have shown
this to work as advertised under winNT). With win98, you
will need a new instance of DynaWrap for every api, or else
re-instantiate the object for every api. Someday I'm going
to learn enough c++ to fix that...
--- </DynaWrap Boilerplate> ---