cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Want the oppurtunity to discuss enhancements to PTC products? Join a working group! X

Managing windows with AutoIt.

dschenken
21-Topaz I

Managing windows with AutoIt.

Ever have windows that are always in the way or hide? The following AutoIt program is a basis for creating a custom solution to your problem. You can start it up and leave it running while using Pro/E or Creo or any other programs you like. It isn't very smart, but it doesn't do very much, except be convenient.

 

It has three functions, based on window name: Move a window to a certain spot, ask the window to close, and activate the window (which should bring it out of hiding)

 

It has a variable, $delay, that sets how fast it runs through a list you create of window titles. By default it is once per second.

It has the ability to select whether you mean windows with a specific name or those based on a partial name

It can either just do the action once for each time you run the winmover program or to keep at it

 

I think it is commented well enough; others may differ on that, so ask questions if you like.

ConsoleWrite is a diagnostic. Those lines can be taken out if you don't need them, but leave them in to see how it works. Comment them if there's a chance of needing them in the future.

 

The ";", semicolon, is a comment introducer. From there to the end of the line is a comment.

 

AutoIT is a zero-based array language. Dimension an array with 3 elements and they are numbered 0, 1, and 2.

 

Note that there can be many windows that meet a poor match choice. It's best not to use the window-close function until you have some practice doing this, particularly when using 'repeat'.

 

To test it, copy and run it from AutoIt or open the attached file**. Then open Notepad; it should move the window a bit. Then save the Notepad file as winmover_test.txt. The program should close it.

 

** The attached file can be edited with Notepad. The AU3 suffix tells the AutoIt customized SciTE editor the file type.

 

If you hammer on this program hard enough, there's a chance it will fail trying to move or close a window that you manage to close first. It does check to see if the window exists at the start of the loop, but if the program is slower than you are, you could beat it. Nothing bad happens. AutoIT will stop the program and give an error message. The program could be three or four times longer to make it more bullet-resistant. Do not use it where loss of life is a concern. Do not base your business on it. It is provided as-is for amusement and educational purposes. Do not attempt high-wire acts involving rabid wild animals or fire.

 

******************************Start of program, does not include this line***********************************

 

 

;Takes a list of window names

;and matching list of coordinates and options

;and waits for windows

 

$delay = 1000 ;the amount of time to wait for the next sweep

$entries = 3

$winactive = 1 ;flag in win_select_opts to indicate option applies only when active

$i = 0

 

dim $win_names[$entries]

dim $win_select_opts[$entries][3]

;first opt is title match -exact names or partial, always, or only active?

;second is if it applies only when active

;third is action: 1 = move, 2 = close, 3 = bring to front

 

dim $win_coords[$entries][2] ; X, Y

dim $win_repeat[$entries] ; Should the window action repeat?

dim $win_done[$entries] ; if true and win_repeat is false, then don't do it again.

 

;If you want to write a file open/close/validate interface, then go ahead

;Otherwise you'll just have to enter all the data here.

;Besides, you should know what the code does before you use it.

 

$entry = 0

$win_names[$entry] = "winmover_test.txt - Notepad"

$win_select_opts[$entry][0] = 3; match entire title

$win_select_opts[$entry][1] = $winactive; when active

$win_select_opts[$entry][2] = 2; close the window

$win_coords[$entry][0] = 300

$win_coords[$entry][1] = 500

$win_repeat[$entry] = True

$win_done[$entry] = False

 

$entry = 1

$win_names[$entry] = "winmover_test.txt - Notepad"

$win_select_opts[$entry][0] = 3; match entire title

$win_select_opts[$entry][1] = 0; active or not active

$win_select_opts[$entry][2] = 1; move the window

$win_coords[$entry][0] = 300

$win_coords[$entry][1] = 500

$win_repeat[$entry] = False

$win_done[$entry] = False

 

$entry = 2

$win_names[$entry] = "Notepad"

$win_select_opts[$entry][0] = 2; match part of the title

$win_select_opts[$entry][1] = 0; active or not active

$win_select_opts[$entry][2] = 1; move the window

$win_coords[$entry][0] = 300

$win_coords[$entry][1] = 100

$win_repeat[$entry] = true

$win_done[$entry] = False

 

while 1=1

    for $i = 0 to $entries-1

        Opt("WinTitleMatchMode", $win_select_opts[$i][0])

        ;See help for more advanced match type 4

        ;1 = Match the title from the start (default)

        ;2 = Match any substring in the title

        ;3 = Exact title match

ConsoleWrite($win_names[$i]&@crlf)

        if (false = $win_repeat[$i] and true = $win_done[$i]) then

            ;this action is not supposed to repeat, and have already done this once.

ConsoleWrite("Skipped"&@crlf)

            ContinueLoop

        EndIf

 

        if WinExists($win_names[$i]) then

ConsoleWrite("Found it " & $i &" "&WinGetTitle($win_names[$i]) &@crlf)

            if $win_select_opts[$i][1] = $winactive Then ; only if the window is active

                if WinActive($win_names[$i]) Then

Consolewrite("It must be active and is"&@crlf)

                    Select

                        case 1 = $win_select_opts[$i][2] ; Move the window

                            WinMove($win_names[$i],"",$win_coords[$i][0],$win_coords[$i][1])

                        case 2 = $win_select_opts[$i][2] ; Close the window

                            WinClose($win_names[$i])

                        case 3 = $win_select_opts[$i][2] ;Bring window to front

                            WinActivate($win_names[$i])

                    endselect

                endif

            else ;don't care if window is active

Consolewrite("It's not active"&@crlf)

                Select

                    case 1 = $win_select_opts[$i][2] ; Move the window

                        WinMove($win_names[$i],"",$win_coords[$i][0],$win_coords[$i][1])

ConsoleWrite("Tried to move the window"&@crlf)

                    case 2 = $win_select_opts[$i][2] ; Close the window

                        WinClose($win_names[$i])

                    case 3 = $win_select_opts[$i][2] ;Bring window to front

                        WinActivate($win_names[$i])

                endselect

            endif

            if (false = $win_repeat[$i]) then

                $win_done[$i] = true

            EndIf

        endif

 

    Next

    sleep($delay)

wend


This thread is inactive and closed by the PTC Community Management Team. If you would like to provide a reply and re-open this thread, please notify the moderator and reference the thread. You may also use "Start a topic" button to ask a new question. Please be sure to include what version of the PTC product you are using so another community member knowledgeable about your version may be able to assist.
0 REPLIES 0
Top Tags