Solved

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

Posted on 2011-02-20
8
1,967 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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
 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If your app took Google’s lash recently, here are the 5 most likely reasons.
Skype is a P2P (Peer to Peer) instant messaging and VOIP (Voice over IP) service – as well as a whole lot more.
This video demonstrates how to use each tool, their shortcuts, where and when to use them, and how to use the keyboard to improve workflow.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…

617 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