Jump to content

WinTitleMatchMode and winmove behave differently when compiled.


Recommended Posts

I've noticed that WinTitleMatchMode seems to behave differently when a script is compiled.

Here is a little program to demo it.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=opttest.exe
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Array.au3>
#include <Misc.au3>
#include <WinAPI.au3>
$arg = StringSplit($CmdLineRaw, " ")
If StringInStr($arg[1], "ErrorStdOut") Then
    ConsoleWrite('Run from Scite' & @CRLF)
    While StringInStr($arg[1], 'adotool') = 0
        _ArrayDelete($arg, 1)
        $arg[0] = $arg[0] - 1
        ;ConsoleWrite($arg[1] & @CRLF)
    WEnd
    _ArrayDelete($arg, 1)
    $arg[0] = $arg[0] - 1
    ;_ArrayDisplay($arg)
Else
    ConsoleWrite('Run from Command Line' & @CRLF)
EndIf
$collectDouble = False
$collectSingle = False
$doubleString = ''
$singleString = ''
Dim $newArg[1]
_ArrayDelete($arg,0)
For $f In $arg
    If StringRegExp($f, '^["]') And Not $collectSingle Then
        ;ConsoleWrite("Collect Single TRUE" & @CRLF)
        $collectDouble = True
    EndIf
    If StringRegExp($f, "^[']") And Not $collectDouble Then
        ;ConsoleWrite("Collect double TRUE" & @CRLF)
        $collectSingle = True
    EndIf
    If $collectDouble Then
        $doubleString = $doubleString & $f
        If StringRegExp($f, '["]$') Then
            $collectDouble = False
            _ArrayAdd($newArg, $doubleString)
            ;ConsoleWrite("1 add dbl " & $doubleString & 'collectDouble ' & $collectDouble & @CRLF)
            $doubleString = ''
        Else
            $doubleString = $doubleString & ' '
            ContinueLoop
        EndIf
    ElseIf $collectSingle Then
        $singleString = $singleString & $f
        If StringRegExp($f, "[']$") Then
            $collectSingle = False
            _ArrayAdd($newArg, $singleString)
            ;ConsoleWrite("2 add sgl " & $singleString & ' collectSingle ' & $collectSingle & @CRLF)
            $singleString = ''
        Else
            $singleString = $singleString & ' '
            ContinueLoop
        EndIf
    Else
        If Not $f = "" Then
            _ArrayAdd($newArg, $f)
            ;ConsoleWrite("2 add term " & $f & @CRLF)
        EndIf
    EndIf
Next
Dim $pos[2]
$pos[0] = 100
$pos[1] = 100
$p = 0
For $i = 1 To UBound($newArg)
    Select
        Case StringInStr($newArg[$i], "/e") > 0
            AutoItSetOption("WinTitleMatchMode", 3)
            ConsoleWrite('Exact Match On' & @CRLF)
        Case StringInStr($newArg[$i], "/s") > 0
            AutoItSetOption("WinTitleMatchMode", 2)
            ConsoleWrite('Substring Match On' & @CRLF)
        Case StringIsDigit($newArg[$i])
            $pos[$p] = Int($newArg[$i])
            $p = $p + 1
            If $p > 1 Then
                ExitLoop
            EndIf
        Case Else
            $fc = StringLeft($newArg[$i], 1)
            If $fc = '"' Or $fc = "'" Then
                $title = StringMid($newArg[$i], 2, StringLen($newArg[$i]) - 2)
            Else
                $title = $newArg[$i]
            EndIf
    EndSelect
Next
ConsoleWrite('MouseMove( "' & $title & '","", ' & $pos[0] & ', ' & $pos[1] & ")" & @CRLF )
$handle= WinGetHandle($title)
ConsoleWrite('Moving "' & WinGetTitle($handle) & @CRLF)
WinMove($handle,"",$pos[0],$pos[1])
exit 0

Suppose you compile this as a CUI called opttest.exe and then opened a cmd.exe window then

and ran these commands:

notepad fred.txt
opttest.exe /s fred 100 100

Then the command line window will be moved although opttest claims it is moving "fred.txt - Notepad"

if you run this command next

opttest.exe /e "fred.txt - Notepad" 100 120

Then opttest will claim it is moving "fred.txt - Notepad" and the notepad window  will be moved.

However if this same script is Run from Scite using F5 then the Notepad window gets moved in both cases.

Why is this?

Edited by tbodine88

tbodine

Link to comment
Share on other sites

Actually, as written, that script will error when you run it because you're looking for the command line parameter adotool, and it will never find it because you don't pass it with that command line you posted.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

  • 2 weeks later...

 Oh I see I called it optest.exe I'm so stupid. I'm sorry. This was extracted from a longer program entitled adotool, a knockoff of the unix xdotool.

On my installation this code

$arg = StringSplit($CmdLineRaw, " ")
If StringInStr($arg[1], "ErrorStdOut") Then
    ConsoleWrite('Run from Scite' & @CRLF)
    While StringInStr($arg[1], 'adotool') = 0
        _ArrayDelete($arg, 1)
        $arg[0] = $arg[0] - 1
        ;ConsoleWrite($arg[1] & @CRLF)
    WEnd
    _ArrayDelete($arg, 1)
    $arg[0] = $arg[0] - 1
Else
    ConsoleWrite('Run from Command Line' & @CRLF)
EndIf

Distiquishes between whether the program is an executable or a interpreted and run from Scite.

In the latter case it removes everything up to and including the program name 'adotool', in the former case it just notifies the user that the program is aware it is an executable and has been run from the command line.

So really if I had written the example correctly it would be this

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=opttest.exe
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Array.au3>
#include <Misc.au3>
#include <WinAPI.au3>
$arg = StringSplit($CmdLineRaw, " ")
If StringInStr($arg[1], "ErrorStdOut") Then
    ConsoleWrite('Run from Scite' & @CRLF)
    While StringInStr($arg[1], 'opttest') = 0 ; <------ Note this change
        _ArrayDelete($arg, 1)
        $arg[0] = $arg[0] - 1
        ;ConsoleWrite($arg[1] & @CRLF)
    WEnd
    _ArrayDelete($arg, 1)
    $arg[0] = $arg[0] - 1
    ;_ArrayDisplay($arg)
Else
    ConsoleWrite('Run from Command Line' & @CRLF)
EndIf
$collectDouble = False
$collectSingle = False
$doubleString = ''
$singleString = ''
Dim $newArg[1]
_ArrayDelete($arg,0)
For $f In $arg
    If StringRegExp($f, '^["]') And Not $collectSingle Then
        ;ConsoleWrite("Collect Single TRUE" & @CRLF)
        $collectDouble = True
    EndIf
    If StringRegExp($f, "^[']") And Not $collectDouble Then
        ;ConsoleWrite("Collect double TRUE" & @CRLF)
        $collectSingle = True
    EndIf
    If $collectDouble Then
        $doubleString = $doubleString & $f
        If StringRegExp($f, '["]$') Then
            $collectDouble = False
            _ArrayAdd($newArg, $doubleString)
            ;ConsoleWrite("1 add dbl " & $doubleString & 'collectDouble ' & $collectDouble & @CRLF)
            $doubleString = ''
        Else
            $doubleString = $doubleString & ' '
            ContinueLoop
        EndIf
    ElseIf $collectSingle Then
        $singleString = $singleString & $f
        If StringRegExp($f, "[']$") Then
            $collectSingle = False
            _ArrayAdd($newArg, $singleString)
            ;ConsoleWrite("2 add sgl " & $singleString & ' collectSingle ' & $collectSingle & @CRLF)
            $singleString = ''
        Else
            $singleString = $singleString & ' '
            ContinueLoop
        EndIf
    Else
        If Not $f = "" Then
            _ArrayAdd($newArg, $f)
            ;ConsoleWrite("2 add term " & $f & @CRLF)
        EndIf
    EndIf
Next
Dim $pos[2]
$pos[0] = 100
$pos[1] = 100
$p = 0
For $i = 1 To UBound($newArg)
    Select
        Case StringInStr($newArg[$i], "/e") > 0
            AutoItSetOption("WinTitleMatchMode", 3)
            ConsoleWrite('Exact Match On' & @CRLF)
        Case StringInStr($newArg[$i], "/s") > 0
            AutoItSetOption("WinTitleMatchMode", 2)
            ConsoleWrite('Substring Match On' & @CRLF)
        Case StringIsDigit($newArg[$i])
            $pos[$p] = Int($newArg[$i])
            $p = $p + 1
            If $p > 1 Then
                ExitLoop
            EndIf
        Case Else
            $fc = StringLeft($newArg[$i], 1)
            If $fc = '"' Or $fc = "'" Then
                $title = StringMid($newArg[$i], 2, StringLen($newArg[$i]) - 2)
            Else
                $title = $newArg[$i]
            EndIf
    EndSelect
Next
ConsoleWrite('MouseMove( "' & $title & '","", ' & $pos[0] & ', ' & $pos[1] & ")" & @CRLF )
$handle= WinGetHandle($title)
ConsoleWrite('Moving "' & WinGetTitle($handle) & @CRLF)
WinMove($handle,"",$pos[0],$pos[1])
exit 0

I have run this program and it works as I expect except for moving the wrong window when given partial window titles.

If the /s option is chosen then the window with the focus is moved when the executable is run, this is usually the command tool, but the appropriate window will be moved when run from Scite.

Thanks for your help.

Edited by tbodine88

tbodine

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...