Solved

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

Posted on 2011-02-20
8
1,838 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

In this article, you will read about the trends across the human resources departments for the upcoming year. Some of them include improving employee experience, adopting new technologies, using HR software to its full extent, and integrating artifi…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This video demonstrates basic masking and how to edit the mask to reveal the desired image.
The viewer will learn how to set up a document for the web and print and the recommended PPI for printing.

685 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