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
Solved

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

Posted on 2011-02-20
8
1,808 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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

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.

Question has a verified solution.

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

Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to set up a document for the web and print and the recommended PPI for printing.
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.

839 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