Jump to content

AutoItObject Pure AutoIt


genius257
 Share

Recommended Posts

This seems something very interesting.... and even more when support for adding methods will be ready.
...worth stay tuned.... :)
Thanks for sharing.

p.s.
this line in AutoItObject_Internal.au3 raises an error:

If $iID==-1 Then Return Return $DISP_E_UNKNOWNNAME

 

Edited by Chimp

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

7 hours ago, Chimp said:

This seems something very interesting.... and even more when support for adding methods will be ready.
...worth stay tuned.... :)
Thanks for sharing.

p.s.
this line in AutoItObject_Internal.au3 raises an error:

If $iID==-1 Then Return Return $DISP_E_UNKNOWNNAME

 

Glad you like it ;)

Sorry about the error. My SciTE did not warn me, but after installing "SciTE4AutoIt3.exe" i now see what you mean ^^

I'll fix and re-upload immediately

Link to comment
Share on other sites

version: 0.1.2

added basic support for methods via "__defineMethod" will update example.

 

Edit:

I been spending all this time looking into function reference, but it might be impossible, without knowing the structure Autoit uses. I feel like i got as close as i could, without just poking the bytes and hoping to get lucky. So I've added the above to try and get somewhat the same result.

Please inform me, if someone can help me with this.

Edited by genius257
Link to comment
Share on other sites

version: 1.0.0

Every type should now be supported with ease.

"__defineMethod" is now deprecated

"__unset" added

"__lock" added

Documentation will be above for most recent release, or on GitHub for older releases.

Edited by genius257
Link to comment
Share on other sites

Nice. Some suggestions to join knowledge

Check also clr thread where we are working on c# clr solution for .net objects integrating into autoit..

Check oope.au3

Search for running object table in forums by trancex as it registers object on the fly so you can reach it from for example vbscript or other com languages.

 

Link to comment
Share on other sites

version: 1.0.2

"__keys" added

"arguments" property added to AccessorObject.

see documentation above for more info.

 

Example to show one way to use the arguments property and to show it works with things such as ScriptControl:

#include "AutoItObject_Internal.au3"

#AutoIt3Wrapper_Run_Au3Check=N

$AutoItError = ObjEvent("AutoIt.Error", "ErrFunc") ; Install a custom error handler
Func ErrFunc($oError)
    ConsoleWrite("!>COM Error !"&@CRLF&"!>"&@TAB&"Number: "&Hex($oError.Number,8)&@CRLF&"!>"&@TAB&"Windescription: "&StringRegExpReplace($oError.windescription,"\R$","")&@CRLF&"!>"&@TAB&"Source: "&$oError.source&@CRLF&"!>"&@TAB&"Description: "&$oError.description&@CRLF&"!>"&@TAB&"Helpfile: "&$oError.helpfile&@CRLF&"!>"&@TAB&"Helpcontext: "&$oError.helpcontext&@CRLF&"!>"&@TAB&"Lastdllerror: "&$oError.lastdllerror&@CRLF&"!>"&@TAB&"Scriptline: "&$oError.scriptline&@CRLF)
EndFunc ;==>ErrFunc

Example01()
Example02()

Func Example01()
    Local $o=IDispatch()
    $o.__defineGetter("a", _MsgBox)
    Local $oSC=ObjCreate("ScriptControl")
    $oSC.language = "JScript"
    ($oSC.Eval("Function('e','return e.a;')"))($o)
EndFunc

Func Example02()
    Local $oWindow = IDispatch()
    $oWindow.hwnd = GUICreate("title", 700, 320)
    $oWindow.__defineGetter("Show", Wnd_Show)
    $oWindow.__defineGetter("Hide", Wnd_Hide)
    $oWindow.__defineGetter("bkColor", Wnd_bkColor)
    $oWindow.__defineGetter("width", Wnd_width)
    $oWindow.__defineGetter("height", Wnd_height)
    $oWindow.__defineGetter("title", Wnd_title)
    $oWindow.__defineGetter("onExit", Wnd_onExit)

    $oWindow.Show.width(300).bkColor(0xC2E34E).height(300).title("Example 2").onExit(_MyExit)

    While 1
        Sleep(10)
    WEnd
EndFunc

Func _MsgBox()
    Return MsgBox(0, "_MsgBox", "test")
EndFunc

Func Wnd_Show($oSelf)
    GUISetState(@SW_SHOW, $oSelf.parent.hwnd)
    Return $oSelf.parent
EndFunc

Func Wnd_Hide($oSelf)
    GUISetState(@SW_HIDE, $oSelf.parent.hwnd)
    Return $oSelf.parent
EndFunc

Func Wnd_bkColor($oSelf)
    If Not ($oSelf.arguments.length==1) Then Return SetError(1, 1, $oSelf.parent
    GUISetBkColor($oSelf.arguments.values[0], $oSelf.parent.hwnd)
    Return $oSelf.parent
EndFunc

Func Wnd_width($oSelf)
    If Not ($oSelf.arguments.length==1) Then Return SetError(1, 1, $oSelf.parent
    Local $aPos = WinGetPos(ptr($oSelf.parent.hwnd), "")
    WinMove(ptr($oSelf.parent.hwnd), "", $aPos[0], $aPos[1], $oSelf.arguments.values[0], $aPos[3], 0)
    Return $oSelf.parent
EndFunc

Func Wnd_height($oSelf)
    If Not ($oSelf.arguments.length==1) Then Return SetError(1, 1, $oSelf.parent
    Local $aPos = WinGetPos(ptr($oSelf.parent.hwnd))
    WinMove(ptr($oSelf.parent.hwnd), "", $aPos[0], $aPos[1], $aPos[2], $oSelf.arguments.values[0], 0)
    Return $oSelf.parent
EndFunc

Func Wnd_title($oSelf)
    If Not ($oSelf.arguments.length==1) Then Return SetError(1, 1, $oSelf.parent)
    WinSetTitle(ptr($oSelf.parent.hwnd), "", $oSelf.arguments.values[0])
    Return $oSelf.parent
EndFunc

Func Wnd_onExit($oSelf)
    If Not ($oSelf.arguments.length==1) Then Return SetError(1, 1, $oSelf.parent)
    opt("GuiOnEventMode", 1)
    GUISetOnEvent(-3, $oSelf.arguments.values[0], ptr($oSelf.parent.hwnd))
EndFunc

Func _MyExit()
    Exit
EndFunc

 

Edited by genius257
Example added
Link to comment
Share on other sites

  • 9 months later...

Q1. How would I do this when I want both parameters to come from a string

$methodName="onJump"
$newMethod="myClass_onJump"

$myObject=iDispatch()
$myObject.__defineGetter($methodName, $newMethod)

func myClass_onJump()
  consolewrite("jump")
endfunc

So i do not want this I want a more dynamic way 

$myObject.__defineGetter("onJump",  myClass_OnJump)

Q2. How would I clone an object that is created?

 

Edited by junkew
Link to comment
Share on other sites

On 31/5/2018 at 1:51 AM, junkew said:

Q1. How would I do this when I want both parameters to come from a string

$methodName="onJump"
$newMethod="myClass_onJump"

$myObject=iDispatch()
$myObject.__defineGetter($methodName, $newMethod)

func myClass_onJump()
  consolewrite("jump")
endfunc

So i do not want this I want a more dynamic way 

$myObject.__defineGetter("onJump",  myClass_OnJump)

Q2. How would I clone an object that is created?

 

Hi @junkew, I'm glad to know there still is interest for my project ;)

A1. Hmmmm. currently, you would need to check for that yourself. See the code below for an example :) If you want, you are more than welcome to create an issue on github for it, and i will implement it :)

#include "AutoItObject_Internal.au3"

#AutoIt3Wrapper_Run_Au3Check=N

if Not IsDeclared("E_HANDLE") Then Global Const $E_HANDLE = 0x80070006
if Not IsDeclared("E_POINTER") Then Global Const $E_POINTER = 0x80004003
if Not IsDeclared("E_NOINTERFACE") Then Global Const $E_NOINTERFACE = 0x80004002
if Not IsDeclared("S_OK") Then Global Const $S_OK = 0x00000000
if Not IsDeclared("E_INVALIDARG") Then Global Const $E_INVALIDARG = 0x80070057
if Not IsDeclared("E_NOTIMPL") Then Global Const $E_NOTIMPL = 0x80004001

$methodName="onJump"
$newMethod="myClass_onJump"

$myObject=MyObject($methodName, $newMethod)

$myObject.onJump

func myClass_onJump()
  consolewrite("jump"&@CRLF)
endfunc

Func MyObject($methodName, $newMethod)
    Local $myObject = IDispatch()

    $newMethod = IsFunc($newMethod) ? $newMethod : Execute($newMethod)
    If Not IsFunc($newMethod) Then Return SetError(1, 0, 0)

    $myObject.__defineGetter($methodName, $newMethod)

    Return SetError(0, 0, $myObject)
EndFunc

A2. The current public version does not support cloning. It is a feature i have planned :) It will come out with the next version, where i also will take big inspiration from JS Object methods, such as freezevalues and for your interest assign. Currently the way to clone an objects property/properties would be to create a new empty object and fetch the desired properties from the structure of the original object, call VariantCopy on each and and assign them to the new object's Properties element in the $tagObject struct. They are in a linked list, so extending the list is easy enough :) Let me know if you want some code to illustrate this :)

I hope my answer will satisfy your questions, and sorry for the delay with the answer :)

Link to comment
Share on other sites

I think in this forum are about a dozen people that understand the power of your library and only a handfull that can write it :). With your library and some others as mentioned before we could write in a more oo syntax then currently is possible. So keep up the great piece of work you made and offcourse credits to all people involved in AIO and objcreateinterface.

Link to comment
Share on other sites

Junkew is right about "AutoItObject_Internal.au3" way over my head but very cool.
I like Pure AutoIt stuff, keep it simple and use as much AutoIt internal as possible.
I see JScript was used in an example which got me thinking about OBJECTS.
JScript can create the objects, properties, methods in Au3.

JScript Example 1

Func ObjErrFunc()
    Msgbox(0,"*COM ERROR*",$ObjErr.windescription & @lf & $ObjErr.description & @LF & "Line=" & $ObjErr.scriptline)
EndFunc

Func jsc($IO)
    $jsCode &= $IO & @CRLF
EndFunc

global $ObjErr = ObjEvent("AutoIt.Error","ObjErrFunc")
global $sc     = ObjCreate("ScriptControl")
global $jsCode = ""
$sc.Language   = "JScript"

jsc('function myCar(){')
    jsc('this.make       = "";')
    jsc('this.model      = "";')
    jsc('this.year       = 0;')
    jsc('this.DisplayCar = function(){return "A Beautiful "+this.year+" "+this.make+" "+this.model;}};')

; MsgBox(0,"jScript",$jsCode)
$sc.AddCode($jsCode)

local $myCar = $sc.Eval("new myCar")
$myCar.make  = "Ford"
$myCar.model = "Mustang"
$myCar.year  = 1969

MsgBox(64,"JS Returns",$myCar.DisplayCar())

The second example was very interesting.
To keep it Au3 pure, I used "AutoItX3.Control" and AutoIt3.exe /AutoIt3ExecuteLine.

JScript Example 2

Func ObjErrFunc()
    Msgbox(0,"*COM ERROR*",$ObjErr.windescription & @lf & $ObjErr.description & @LF & "Line=" & $ObjErr.scriptline)
EndFunc

Func jsc($IO)
    $jsCode &= $IO & @CRLF
EndFunc

Func bkColor($hWnd,$iMsg,$wParam,$lParam)
    GUISetBkColor($wParam,$hWnd)
EndFunc

Func GuiLoop()
    While GUIGetMsg() <> -3
        Sleep(10)
    WEnd
EndFunc

global $ObjErr = ObjEvent("AutoIt.Error","ObjErrFunc")
global $sc     = ObjCreate("ScriptControl")
global $jsCode = ""
$sc.Language   = "JScript"

jsc('var au3x   = new ActiveXObject("AutoItX3.Control");')
jsc('var au3exe = "c:/Program Files (x86)/AutoIt3/AutoIt3.exe /AutoIt3ExecuteLine ";')
jsc('var width = 0,height = 0;')

jsc('function Au3_Objects(){')
    jsc('this.hWnd    = 0;')
    jsc('this.Title   = function(t){au3x.WinSetTitle("","",t);return this;}')
    jsc('this.bkColor = function(c){au3x.Run(au3exe+"\"DllCall(''user32'',''lresult'',''SendMessage'',''hwnd'',"+this.hWnd+",''uint'',1024,''wparam'',"+c+",''lparam'',0)\"");return this;}')
    jsc('this.Width   = function(w){au3x.WinMove("","",('&@desktopwidth&'-w)/2,('&@desktopheight&'-height)/2,w,height);width = w;return this;}')
    jsc('this.Height  = function(h){au3x.WinMove("","",('&@desktopwidth&'-width)/2,('&@desktopheight&'-h)/2,width,h);height = h;return this;}')
    jsc('this.Show    = function(){au3x.Run(au3exe+"\"GUISetState()\"");return this;}}')

; MsgBox(0,"jScript",$jsCode)
$sc.AddCode($jsCode)

local $gui = $sc.eval("new Au3_Objects")

$gui.hWnd = GUICreate("Normal Au3 Gui 1")
GUISetBkColor(0xffff00)
GUIRegisterMsg(1024,"bkColor")
GUISetState()
GuiLoop()

$gui.Title("Objects Au3 Gui 2").bkColor(0xff00ff).Width(400).Height(600).Show()
GuiLoop()

With $gui
    .Title("Objects Au3 Gui 3")
    .bkColor(0x00ff00)
    .Width(800)
    .Height(300)
    .Show()
EndWith
GuiLoop()

 

Link to comment
Share on other sites

@genius257 

  • Any more magic in this Execute("string")?

so this Execute($newMethod) seems to do the trick but reading help and manual I am surprised on this (powerfull) behavior

$newMethod = IsFunc($newMethod) ? $newMethod : Execute($newMethod)

As above does not trigger the jump function to execute that happens later when you

$myObject.onJump

 

 

Some code

I played around with referencing variables to functions ($class, $endclass etc)  to see if with some syntactic sugar we could make it more OO syntax

  • using $<some oo syntax> looks nice to my feeling as some kind of annotator tags before method/property definition
  • No additional scanning of coding needed
  • Makes life a little easier then the initial syntax of iDispatch()
  • TODO:
    • Manage the objects/classes  (I feel with assign/eval/execute or in an overall array of class/objects)
    • get some feedback if people like below OO-syntax
    • no workaround I feel for Classname_methodName syntax with underscores

 

#include <AutoItConstants.au3>
#include <AutoItObject_Internal.au3>

if Not IsDeclared("E_HANDLE") Then Global Const $E_HANDLE = 0x80070006
if Not IsDeclared("E_POINTER") Then Global Const $E_POINTER = 0x80004003
if Not IsDeclared("E_NOINTERFACE") Then Global Const $E_NOINTERFACE = 0x80004002
if Not IsDeclared("S_OK") Then Global Const $S_OK = 0x00000000
if Not IsDeclared("E_INVALIDARG") Then Global Const $E_INVALIDARG = 0x80070057
if Not IsDeclared("E_NOTIMPL") Then Global Const $E_NOTIMPL = 0x80004001
    
;~ #AutoIt3Wrapper_Run_Au3Check=N

#REGION putInIncludeLater

;~ Create some function variables to create a nicer class syntax
global $g_lastClassReferenced
$class=createClass
$property=createProperty
$method=createMethod
$endClass=endClass

;~ Some initial implementations behind the nice function variables
func createClass($className)
    consolewrite("Creating class " & $className & @CRLF)
    $g_lastClassReferenced=IDispatch()
    $g_lastClassReferenced.className=$className
    consolewrite("Created class  " & $g_lastClassReferenced.className & @CRLF)
    $result=assign($className,$g_lastClassReferenced, $ASSIGN_FORCEGLOBAL)
EndFunc

func endClass($dummy=0)
    consolewrite("Ending class definition " & $g_lastClassReferenced.className & @CRLF)
EndFunc

func createProperty($propName)
    $newMethod=$g_lastClassReferenced.className & "_set_" & $propName
    $newMethod = IsFunc($newMethod) ? $newMethod : Execute($newMethod)
;~     'If there is a setter function defined
    If IsFunc($newMethod) Then 
        $g_lastClassReferenced.__defineSetter($propName, $newMethod)
    EndIf

    $newMethod=$g_lastClassReferenced.className & "_get_" & $propName
    $newMethod = IsFunc($newMethod) ? $newMethod : Execute($newMethod)
;~     'If there is a setter function defined
    If IsFunc($newMethod) Then 
        $g_lastClassReferenced.__defineGetter($propName, $newMethod)
    EndIf
    
;~  assign($newProp,"",$ASSIGN_FORCEGLOBAL)
    consolewrite("This could work " & $propName & @CRLF)
EndFunc

func defaultGet($oSelf)
    consolewrite("Default get with paramcount "& $oSelf.arguments.length & @CRLF)
EndFunc

func createMethod($methodName)
    $newMethod=$g_lastClassReferenced.className & "_" & $methodName
    $newMethod = IsFunc($newMethod) ? $newMethod : Execute($newMethod)
    If Not IsFunc($newMethod) Then Return SetError(1, 0, 0)

    $g_lastClassReferenced.__defineGetter($methodName, $newMethod)

;~  assign($newMethod,"",$ASSIGN_FORCEGLOBAL)
    consolewrite("This could work " & $newMethod & @CRLF)
EndFunc

func new($className)
    consolewrite("Instantiate object " & $className & @CRLF)
;~ TODO: Now we get the classObject whereas we should actually make a clone
    return eval($className)
EndFunc
#EndRegion
;~ Each class could be in its own include file, REGION there only for testing 
#REGION class Foo
$class("Foo")
    $property("Property1")
    func Foo_set_Property1()
        consolewrite("Hmmm, looks funny Foo_set_Property1" & @CRLF)
    EndFunc
    func Foo_get_Property1()
        consolewrite("Hmmm, looks funny Foo_get_Property1" & @CRLF)
    EndFunc 

    $property("Property2")
    func Foo_set_Property2()
        consolewrite("Hmmm, looks funny Foo_get_Property2" & @CRLF)
    EndFunc
    func Foo_get_Property2()
        consolewrite("Hmmm, looks funny Foo_get_Property2" & @CRLF)
    EndFunc 
    $method("method1")
    func Foo_method1()
        consolewrite("Hmmm, looks funny Foo_method1" & @CRLF)
    EndFunc
    $method("method2")
    func Foo_method2()
        consolewrite("Hmmm, looks funny Foo_method2" & @CRLF)
    EndFunc
    $method("method3")
    func Foo_method3()
        consolewrite("Hmmm, looks funny 3" & @CRLF)
    EndFunc
$endClass("Foo")
#EndRegion

#REGION class bar
$class("Bar")
    $property("Property1")
    func Bar_set_Property1()
        consolewrite("Hmmm, looks funny Bar_set_Property1" & @CRLF)
    EndFunc
    func Bar_get_Property1()
        consolewrite("Hmmm, looks funny Bar_get_Property1" & @CRLF)
    EndFunc 

    $property("Property2")
    func Bar_set_Property2()
        consolewrite("Hmmm, looks funny Bar_get_Property2" & @CRLF)
    EndFunc
    func Bar_get_Property2()
        consolewrite("Hmmm, looks funny Bar_get_Property2" & @CRLF)
    EndFunc 
    $method("method1")
    func Bar_method1()
        consolewrite("Hmmm, looks funny Bar_method1" & @CRLF)
    EndFunc
    $method("method2")
    func Bar_method2()
        consolewrite("Hmmm, looks funny Bar_method2" & @CRLF)
    EndFunc
    $method("method3")
    func Bar_method3()
        consolewrite("Hmmm, looks funny 3" & @CRLF)
    EndFunc
$endClass("Bar")
#EndRegion

;~ ##### Actual program ######
Consolewrite ("##### Actual program ######")
$oFoo1=new("Foo")
$oFoo2=new("Foo")
$oBar1=new("BAR")
$oBar2=new("BAR")

consolewrite("is object " & isobj($oFoo1) & @CRLF)
consolewrite("is object " & $oFoo1.Property1 & @CRLF)
$oFoo1.method1()
$oFoo2.method1()
$oBar1.method1()
$oBar2.method1()

 

Link to comment
Share on other sites

@junkew

On 2/6/2018 at 11:49 PM, junkew said:

I think in this forum are about a dozen people that understand the power of your library and only a handfull that can write it :). With your library and some others as mentioned before we could write in a more oo syntax then currently is possible. So keep up the great piece of work you made and offcourse credits to all people involved in AIO and objcreateinterface.

Thank you for the kind words.

 

22 hours ago, junkew said:

Any more magic in this Execute("string")?

Lots, i would say :)

22 hours ago, junkew said:

so this Execute($newMethod) seems to do the trick but reading help and manual I am surprised on this (powerfull) behavior

$newMethod = IsFunc($newMethod) ? $newMethod : Execute($newMethod)

As above does not trigger the jump function to execute that happens later when you

$myObject.onJump

 

Yeah Execute runs the litteral string as AutoIt script, so if it's a function name, we get a UserFunction or Function type variable returned.

$myObject.jump triggers the getter accessor (if it is exists) and within your getter function, you can return computed values.

 

22 hours ago, junkew said:

Some code

I do like your idea, but i try to use as few global variables as possible on purpose :)

The less chance to confict with other user functions/variables the better :)

This could be a nice fork of my project, giving people even more choices :)

 

I do understand the issue you have with the IDispatch approach. The truth is this library is more akin to JS objects, as to classes.

My personal approach to classes so far have been like this:

#Region Actual script
#include <Array.au3>
#include "AutoItObject_Internal.au3"

if Not IsDeclared("E_HANDLE") Then Global Const $E_HANDLE = 0x80070006
if Not IsDeclared("E_POINTER") Then Global Const $E_POINTER = 0x80004003
if Not IsDeclared("E_NOINTERFACE") Then Global Const $E_NOINTERFACE = 0x80004002
if Not IsDeclared("S_OK") Then Global Const $S_OK = 0x00000000
if Not IsDeclared("E_INVALIDARG") Then Global Const $E_INVALIDARG = 0x80070057
if Not IsDeclared("E_NOTIMPL") Then Global Const $E_NOTIMPL = 0x80004001

#AutoIt3Wrapper_Run_Au3Check=N

$oFoo = Foo()

$oFoo.Property1 = "I love AutoIt"
$oFoo.Property2 = "I love AutoIt"
$oFoo.Property3 = "I love AutoIt"
$oFoo.Property4 = "really"

For $i=0 To 9
    ConsoleWrite( _
        $oFoo.Property1 & @CRLF & _
        $oFoo.Property2 & @CRLF & _
        $oFoo.Property3 & @CRLF & _
        $oFoo.Property4 & @CRLF & _
        @CRLF _
    )
Next

#cs
# Chained calls
#ce
$oFoo.Write($oFoo.Property1).Write($oFoo.Property2).Write($oFoo.Property3).Write($oFoo.Property4)

#cs
# Dynamic number of parameters
#ce
$oFoo.Write($oFoo.Property1, $oFoo.Property2, $oFoo.Property3, $oFoo.Property4)
#EndRegion Actual script

#Region Foo.au3
    Func Foo()
        Local $fooClassInstance = IDispatch()

        $fooClassInstance.Property1 = "string value";normal variable

        $fooClassInstance.Property2 = "";"computed" variable out
        $fooClassInstance.__defineGetter("Property2", Foo_Property2_Get)

        $fooClassInstance.Property3 = "";"computed" variable in
        $fooClassInstance.__defineSetter("Property3", Foo_Property3_Set)

        $fooClassInstance.Property4 = "";"computed" variable in & out
        $fooClassInstance.__defineGetter("Property4", Foo_Property4_Get)
        $fooClassInstance.__defineSetter("Property4", Foo_Property4_Set)

        $fooClassInstance.Write = ConsoleWrite;internal property used as a setter that returns relf for continues calls
        $fooClassInstance.__defineGetter("Write", Foo_Write_Get)

        $fooClassInstance.__lock();prevent additional properties to be created

        Return $fooClassInstance
    EndFunc

    #cs
    # Reverses the value of Property2 and returns it.
    # @param $oThis - IDispatch internal accessor object
    #ce
    Func Foo_Property2_Get($oThis)
        Local $value = $oThis.val
        If Not IsString($value) Then Return $value
        $value = StringReverse($value)
        $oThis.val = $value
        Return $value
    EndFunc

    #cs
    # Shuffles the provided string value and sets it as
    # @param $oThis - IDispatch internal accessor object
    #ce
    Func Foo_Property3_Set($oThis)
        Local $value = $oThis.ret
        If Not IsString($value) Then Return SetError(2, 0, 0)
        $value = StringSplit($value, "", 2)
        _ArrayShuffle($value)
        $oThis.val = _ArrayToString($value, "")
    EndFunc

    #cs
    # Randomly sets each char in Property4 value to upper or lower case and returns it
    # @param $oThis - IDispatch internal accessor object
    #ce
    Func Foo_Property4_Get($oThis)
        Local $value = $oThis.val
        $value = StringSplit($value, "", 2)
        For $i=0 To UBound($value)-1
            $value[$i] = Random(0, 1, 1) ? StringUpper($value[$i]) : StringLower($value[$i])
        Next
        $value = _ArrayToString($value, "")
        Return $value
    EndFunc

    #cs
    # Adds provided string as an adverb in a proclamation for AutoIt love and sets it as Property4 value
    #ce
    Func Foo_Property4_Set($oThis)
        $oThis.val = StringFormat("I %s love AutoIt", $oThis.ret)
    EndFunc

    Func Foo_Write_Get($oThis)
        Local $i
        Local $self = $oThis.parent
        Local $length = $oThis.arguments.length
        Local $values = $oThis.arguments.values
        For $i=0 To $length-1
            ConsoleWrite($values[$i] & @CRLF)
        Next
        ConsoleWrite(@CRLF)
        Return $self
    EndFunc
#EndRegion Foo.au3

 

Edit:

Here is an example for nested objects used with accessors

Edited by genius257
Link to comment
Share on other sites

On 3/6/2018 at 4:12 PM, xroot said:

Junkew is right about "AutoItObject_Internal.au3" way over my head but very cool.
I like Pure AutoIt stuff, keep it simple and use as much AutoIt internal as possible.
I see JScript was used in an example which got me thinking about OBJECTS.
JScript can create the objects, properties, methods in Au3.

JScript Example 1

Func ObjErrFunc()
    Msgbox(0,"*COM ERROR*",$ObjErr.windescription & @lf & $ObjErr.description & @LF & "Line=" & $ObjErr.scriptline)
EndFunc

Func jsc($IO)
    $jsCode &= $IO & @CRLF
EndFunc

global $ObjErr = ObjEvent("AutoIt.Error","ObjErrFunc")
global $sc     = ObjCreate("ScriptControl")
global $jsCode = ""
$sc.Language   = "JScript"

jsc('function myCar(){')
    jsc('this.make       = "";')
    jsc('this.model      = "";')
    jsc('this.year       = 0;')
    jsc('this.DisplayCar = function(){return "A Beautiful "+this.year+" "+this.make+" "+this.model;}};')

; MsgBox(0,"jScript",$jsCode)
$sc.AddCode($jsCode)

local $myCar = $sc.Eval("new myCar")
$myCar.make  = "Ford"
$myCar.model = "Mustang"
$myCar.year  = 1969

MsgBox(64,"JS Returns",$myCar.DisplayCar())

The second example was very interesting.
To keep it Au3 pure, I used "AutoItX3.Control" and AutoIt3.exe /AutoIt3ExecuteLine.

JScript Example 2

Func ObjErrFunc()
    Msgbox(0,"*COM ERROR*",$ObjErr.windescription & @lf & $ObjErr.description & @LF & "Line=" & $ObjErr.scriptline)
EndFunc

Func jsc($IO)
    $jsCode &= $IO & @CRLF
EndFunc

Func bkColor($hWnd,$iMsg,$wParam,$lParam)
    GUISetBkColor($wParam,$hWnd)
EndFunc

Func GuiLoop()
    While GUIGetMsg() <> -3
        Sleep(10)
    WEnd
EndFunc

global $ObjErr = ObjEvent("AutoIt.Error","ObjErrFunc")
global $sc     = ObjCreate("ScriptControl")
global $jsCode = ""
$sc.Language   = "JScript"

jsc('var au3x   = new ActiveXObject("AutoItX3.Control");')
jsc('var au3exe = "c:/Program Files (x86)/AutoIt3/AutoIt3.exe /AutoIt3ExecuteLine ";')
jsc('var width = 0,height = 0;')

jsc('function Au3_Objects(){')
    jsc('this.hWnd    = 0;')
    jsc('this.Title   = function(t){au3x.WinSetTitle("","",t);return this;}')
    jsc('this.bkColor = function(c){au3x.Run(au3exe+"\"DllCall(''user32'',''lresult'',''SendMessage'',''hwnd'',"+this.hWnd+",''uint'',1024,''wparam'',"+c+",''lparam'',0)\"");return this;}')
    jsc('this.Width   = function(w){au3x.WinMove("","",('&@desktopwidth&'-w)/2,('&@desktopheight&'-height)/2,w,height);width = w;return this;}')
    jsc('this.Height  = function(h){au3x.WinMove("","",('&@desktopwidth&'-width)/2,('&@desktopheight&'-h)/2,width,h);height = h;return this;}')
    jsc('this.Show    = function(){au3x.Run(au3exe+"\"GUISetState()\"");return this;}}')

; MsgBox(0,"jScript",$jsCode)
$sc.AddCode($jsCode)

local $gui = $sc.eval("new Au3_Objects")

$gui.hWnd = GUICreate("Normal Au3 Gui 1")
GUISetBkColor(0xffff00)
GUIRegisterMsg(1024,"bkColor")
GUISetState()
GuiLoop()

$gui.Title("Objects Au3 Gui 2").bkColor(0xff00ff).Width(400).Height(600).Show()
GuiLoop()

With $gui
    .Title("Objects Au3 Gui 3")
    .bkColor(0x00ff00)
    .Width(800)
    .Height(300)
    .Show()
EndWith
GuiLoop()

 

Hi @xroot

Yeah i used to do a lot of JS objects with AutoIt, however there are problems, sutch as trying to execute AutoIt methods in JS or vice versa with internal variable types, or nasty memory leaks, that resulted in my script crashing after 2 minutes.

But yeah it depends on preference :) Everything has its strengths and weaknesses i guess ^^

Link to comment
Share on other sites

  • 2 weeks later...
  • Whats the idea (naming thoughts) of parent property when you are in the setter/getter?
    I expected more like $This.string2 without .parent. in front of it
  • Offcourse I made already a mistake on infinite recursing with using $this.parent.string ;-)

Example I played with

#include <AutoItObject_Internal.au3>

$o=idispatch()
$o.string = "123";optional to define property first
$o.string2 ="just another string"

$o.__defineSetter("string", setter1)
$o.__defineGetter("string", getter1)

$o.string = "456";so what happens if I set it again
func setter1($this) 
    consolewrite($this.parent.string2 & @CRLF)
    $this.val = $this.ret & $this.ret
EndFunc

func getter1($this)
;~  $this.ret=$this.val 'Does not make sense here
    return $this.val & $this.val
EndFunc

 

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

×
×
  • Create New...