Solved

How to give focus to or shut down a program with AutoIT

Posted on 2011-02-20
8
1,759 Views
Last Modified: 2012-08-13
I am running four instances of ABBYY (an OCR program), managed by an AutoIT script.  When I see that a specific instance is finished, I need to give it focus to send key strokes to shut it down and start over.  All of the functions I can find for that want to address the program by-title.  But ABBYY will not accept a change in title (WinSetTitle sets the title, then at the next step ABBYY reverts to the standard title).  So I can launch and record PID or Handle, but I cannot find how to give focus using either of them.  Not preferred, but would try just killing the program, but again, no success using PID or Handle to specify which session to kill.

This is a sample which will launch the program, but will not close it down.
#include<file.au3>
#Include<array.au3>


Global $PID[5]

For $i = 1 to 1
    $PID[$i] = RunWait("C:\Program Files (x86)\ABBYY FineReader 10\FineReader.exe")
Next
Sleep(3000)
ProcessClose($PID[1])

Open in new window

0
Comment
Question by:Mike Caldwell
8 Comments
 
LVL 14

Expert Comment

by:spiderfix
ID: 34941033
You can kill the process.

Dim objShell
objShell.Run "taskkill /IM Notepad.exe"


That is for Windows 7 and Windows Vista.
There is a slight difference if you’re killing the process in Windows XP.

Dim objShell
objShell.Run "tskill Notepad"

0
 
LVL 29

Expert Comment

by:matrixnz
ID: 34941932
Have you tried using Advanced Window Titles and Text
Opt("WinTitleMatchMode", 1)

You can then use instance to detect a particular window instance to close
WinWaitActive("[TITLE:My Window; CLASS:My Class; INSTANCE:2]", "")

Check the PID[1] is returning the correct PID using something like:
MsgBox(0, '', $PID[1]) or ConsoleWrite($PID[1]) to get the information from Scite Editor.

Hope that helps.

Cheers
0
 
LVL 1

Author Comment

by:Mike Caldwell
ID: 34942759
Is the objShell.Run an AutoIT function?  It looks like VB Script.
0
 
LVL 1

Author Comment

by:Mike Caldwell
ID: 34942785
I have been using VB Script for a few years, but switched to AutoIT for this particular project because I was unable to control focus with VBS.  Now my problem is that ABBYY will not allow a change in title (it changes, but then at the next program step it reverts back; no problem with Notepad, but with ABBYY it just doesn't persist) and all of the focus commands use title.  I have had lots of sugestions of how to launch the four sessions, all of which work, but nothing yet that will shut them down.  
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 11

Accepted Solution

by:
TheGorby earned 500 total points
ID: 34951276
Hi again Mike, sorry I was away for a few days on your other question but I had an idea this weekend.

First of all, the script you posted above should work if you change lines 7 and 11 accordingly:

For $i = 0 to 4
ProcessClose($PID[0])

Arrays always start at 0, a declaration of 5 would be 0-4. BUT I think this can be better solved using window handles. It allows you to specify a certain window without changing the title. Even if the window's title changes its handle will always stay the same (assuming ABBYY isn't killing the window and starting a new one somewhere in its processes). Try the code below;
#include<file.au3>
#Include<array.au3>

Global $PID
Dim $HND[6]

$HND[0]=5

For $i = 1 to 5
    $PID = RunWait("C:\Program Files (x86)\ABBYY FineReader 10\FineReader.exe")
    $HND[$i]=WinGetHandle("Untitled document - ABBYY FineReader 10 Professional Edition")
Next
Sleep(3000)
WinClose($HND[1])

Open in new window

0
 
LVL 11

Expert Comment

by:TheGorby
ID: 34951318
Oh and you may have guessed but I forgot to mention; a window handle can be used instead of a title in any function that requires a title. When a window handle is used the "text" portion is ignored (and unnecessary).
0
 
LVL 11

Expert Comment

by:TheGorby
ID: 34951386
I realized there was an error in that script I posted. Try this instead, it still uses window handles:
#include<file.au3>
#Include<array.au3>

Global $PID[6]
$PID[0]=5
Dim $HND

For $i = 1 to 5
    $PID[$i] = RunWait("C:\Program Files (x86)\ABBYY FineReader 10\FineReader.exe")
Next
Sleep(3000)
$HND=WinList("Untitled document - ABBYY FineReader 10 Professional Edition")
_ArrayDisplay($HND)
MsgBox(0,"ABBYY Windows Found","Number of ABBYY windows found: " & $HND[0][0])

;Run macros here

WinClose($HND[1][1])
WinClose($HND[2][1])
WinClose($HND[3][1])
WinClose($HND[4][1])
WinClose($HND[5][1])

Open in new window

0
 
LVL 1

Author Closing Comment

by:Mike Caldwell
ID: 34951798
A similar posting in the AutoIT Forum came up with this same solution.  I am not using WinClose but rather giving focus so that I can send keystrokes to more gracefully shut down.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Storage devices are generally used to save the data or sometime transfer the data from one computer system to another system. However, sometimes user accidentally erased their important data from the Storage devices. Users have to know how data reco…
In our personal lives, we have well-designed consumer apps to delight us and make even the most complex transactions simple. Many enterprise applications, however, are a bit behind the times. For an enterprise app to be successful in today's tech wo…
Using Adobe Premiere Pro, the viewer will learn how to set up a sequence with proper settings, importing pictures, rendering, and exporting the finished product.
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now