Solved

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

Posted on 2011-02-20
8
1,772 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Image Deployment Tool 9 76
Free Alternative to JIRA 4 91
People Counting Software 2 113
Macro Excel - Multiple If conditions 2 64
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…
All of the resources available today make learning a new digital media easier than ever-- if you know where to begin. This is a clear, simple guide to a few of the basic digital art mediums and how to begin learning them on your own.
This video shows how use content aware, what it’s used for, and when to use it over other tools.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

16 Experts available now in Live!

Get 1:1 Help Now