Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

AppActivate doesn't always work

5,126 views
Skip to first unread message

Nathaniel Hekman

unread,
Jul 19, 2002, 12:49:18 PM7/19/02
to
I wrote a simple script that's just supposed to run an application and send
it a few keystrokes. Unfortunately, WScript.Shell.Run starts the
application but it doesn't always have the focus (sometimes it does,
sometimes not, I can't see why). It's the only application running, but its
icon in the task bar just blinks at me, waiting for me to click on it.
Calling AppActivate does not help.

Any idea what's going on or how I can get by this?


Nate Hekman

Michel Gallant

unread,
Jul 19, 2002, 12:53:16 PM7/19/02
to
can you post the essential part of your code (and what OS) ?
- Mitch

Torgeir Bakken

unread,
Jul 19, 2002, 1:25:10 PM7/19/02
to
Nathaniel Hekman wrote:

Yes. Save yourself some grief, and take a look at the free AutoIt/AutoItX
(AutoItX: COM component that can be used from VBScript):
http://www.hiddensoft.com/AutoIt/

There are several cases where WSH AppActivate will not be able to bring a window
to the top on Win98/ME/2k/XP (but will work for Win95 and NT 4.0). The AutoItX
WinActivate method does not have this shortcoming.

AutoItX also has much more windows handling functionality, and also a _lot_ more
other stuff. All this in a 49 KB dll :-)

Note:
AutoItX window handling defaults to match the start of a window title that you
specify,use "SetTitleMatchMode 2" to specify ANY substring of the window title
you want to match. Also, the Windows titles and text are case sensetive.

--
torgeir


Nathaniel Hekman

unread,
Jul 19, 2002, 1:25:54 PM7/19/02
to
Certainly. I'm running this on XP Pro. Here's the code:

-------------------------------------------------------------------
var oShell = WScript.CreateObject("WScript.Shell");
var sMainWindowTitle = "Prassi PrimoDVD 2.0 - [Job 1";

oShell.Run("d:\\images\\releases\\currentrelease.pxj");
WScript.Sleep(8000);

// at this point the application is running, but does not
// always have the focus

oShell.AppActivate(sMainWindowTitle);
WScript.Sleep(1000);

// I tried the above line in order to give it the focus, but
// if it doesn't have the focus automatically when it starts,
// this doesn't help.
//
// I found that sometimes I could get around the problem
// by have a second application running. If I could switch
// the focus (AppActivate) to the 2nd app and then back
// to the one I really want, that would often work.
-------------------------------------------------------------------

"Michel Gallant" <neu...@istar.ca> wrote in message
news:3D3843FC...@istar.ca...

Mark A. Bystry

unread,
Jul 19, 2002, 4:25:49 PM7/19/02
to
why not try this.....

If WshShell.AppActivate("Prassi PrimoDVD 2.0") then
WScript.Sleep 500

...send your keys here...

else
wscript.quit


Torgeir Bakken

unread,
Jul 19, 2002, 4:48:04 PM7/19/02
to
Nathaniel Hekman wrote:

> var oShell = WScript.CreateObject("WScript.Shell");
> var sMainWindowTitle = "Prassi PrimoDVD 2.0 - [Job 1";
>
> oShell.Run("d:\\images\\releases\\currentrelease.pxj");
> WScript.Sleep(8000);
>
> // at this point the application is running, but does not
> // always have the focus
>
> oShell.AppActivate(sMainWindowTitle);
> WScript.Sleep(1000);
>
> // I tried the above line in order to give it the focus, but
> // if it doesn't have the focus automatically when it starts,
> // this doesn't help.

Hi

This is child's play for AutoItX:

Set oShell = CreateObject("WScript.Shell")
Set oAutoIt = CreateObject("AutoItX.Control")
oAutoIt.SetTitleMatchMode 2

sMainWindowTitle = "Prassi PrimoDVD 2.0 - [Job 1"

oShell.Run "d:\images\releases\currentrelease.pxj"

' Loop until the window exists and is active
Do
' Add a sleep so the loop doesn't become a CPU hog.
Wscript.Sleep 100
oAutoIt.WinActivate sMainWindowTitle, ""
Loop Until oAutoIt.IfWinActive(sMainWindowTitle, "") = 1

' Continue script here


*********************
Here is a demo of another way of doing it, just to show AutoItX's capabilities:


oShell.Run "d:\images\releases\currentrelease.pxj"

' Stop script execution until the specified window exists.
' The window does not need be active. Set timeout to
' 5 seconds (optional)

' Checking for timeout
If oAutoIt.WinWait(sMainWindowTitle, "", 5) = 1 Then
WScript.Echo "Timed out. No window found. Quitting!"
WScript.Quit
End If

' If you do not want timeout, you can use this instead:
'oAutoIt.WinWait sMainWindowTitle, ""


' Window exists, now make it active
oAutoIt.WinActivate sMainWindowTitle, ""

' Continue script here

--
torgeir


Nathaniel Hekman

unread,
Jul 19, 2002, 6:02:59 PM7/19/02
to
Because the problem is that the app doesn't get the focus, so the key
strokes don't go to the right window.

"Mark A. Bystry" <mby...@ziggity.com> wrote in message
news:#cwYUM2LCHA.2024@tkmsftngp10...

Nathaniel Hekman

unread,
Jul 19, 2002, 6:05:24 PM7/19/02
to
I was hoping not to have to learn yet another package, but I've seen AutoIt
mentioned often on this list so I finally decided to give it a try. It
seems to live up to its reputation so far! A bit of reading through the
examples and the documentation, and it's working splendidly. Thanks for the
suggestion.


Nate

"Torgeir Bakken" <Torgeir...@hydro.com> wrote in message
news:3D384B76...@hydro.com...

Nathaniel Hekman

unread,
Jul 25, 2002, 10:39:13 AM7/25/02
to
I tried this out with AutoIt, and it works great ... up to a point! It
works only as long as I'm actually connected to the computer running it. I
typically run that computer remotely using Remote Desktop. If I disconnect
the session (but remain logged in) and the script runs while it's in this
disconnected state, then it waits forever at the WinWaitActive line.
Apparently the window doesn't really become "active" until I connect again
with Remote Desktop (or login locally).

I'll see if I can find an AutoIt newsgroup or mailing list where I can see
if others have this problem.


Nate

"Torgeir Bakken" <Torgeir...@hydro.com> wrote in message

news:3D387B03...@hydro.com...

alex_k._angelopoulos_(mvp)

unread,
Jul 25, 2002, 10:57:41 AM7/25/02
to
No need. We've talked about this in the Terminal Services groups
before. It is not a problem with AutoIt (or one which can be resolved
by changes in AutoItX); while you are disconnected, the screen just
"goes away"; there are no windows to activate, and all the GUI related
stuff just stalls.

--
Please respond in the newsgroup so everyone may benefit.
http://www.bittnet.com/winremote
http://www.bittnet.com/scripting


"Nathaniel Hekman" <hekmanATgeo...@no.spam> wrote in message
news:O$Xetj#MCHA.2024@tkmsftngp11...

Nathaniel Hekman

unread,
Jul 25, 2002, 11:04:52 AM7/25/02
to
So there's no solution? And this issue will exist for any and all similar
scripting languages? That's a bummer!

<Alex K. Angelopoulos (MVP)> wrote in message
news:uzr0ht#MCHA.2100@tkmsftngp12...

alex_k._angelopoulos_(mvp)

unread,
Jul 25, 2002, 11:12:58 AM7/25/02
to
Yes, you got it... :(

This is yet another annoyance with having to manipulate "real" windows
as opposed to the data and logic behind them.


--
Please respond in the newsgroup so everyone may benefit.
http://www.bittnet.com/winremote
http://www.bittnet.com/scripting


"Nathaniel Hekman" <hekmanATgeo...@no.spam> wrote in message

news:e9WYCy#MCHA.2420@tkmsftngp11...

0 new messages