Chimp

how to get the wanted property of an object requesting it via a variable?

9 posts in this topic


Hi,

.... I think I'm missing something simple, but I'm stuk....

for example using the normal syntax to get the attribute is ok:

$oDoc.fileCreatedDate

how to achieve the same result within the GetAttribute() function? (of course passing the wanted attribute via a variable)

; Open a browser to the google Homepage, get a reference
; to the document object and display a document attribute

#include <IE.au3>
#include <MsgBoxConstants.au3>

Local $oIE = _IECreate("http://www.google.com")
Local $oDoc = _IEDocGetObj($oIE)
MsgBox($MB_SYSTEMMODAL, "Document Created Date", $oDoc.fileCreatedDate)

; How to get attribute in this way instead ??
Local $sWantedattribute = "fileCreatedDate"
Local $CreatedDate = GetAttribute($oDoc, $sWantedattribute)
MsgBox($MB_SYSTEMMODAL, "Testing", $CreatedDate)

Func GetAttribute($oObject, $sAttribute)

    Local $Peekattribute = $oObject.$sAttribute ;  < - - - - how To ?????
    ; the following even doesn't works on my system
    ; Local $Peekattribute = $oObject.getAttribute($sAttribute)
    ; Local $Peekattribute = $oObject.getAttribute($oObject.item(1))

    Return $Peekattribute
EndFunc   ;==>GetAttribute

thanks for any suggestion


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

Share this post


Link to post
Share on other sites



I once had the same discussion with some Devs when re-writing the Word UDF.
A was then asked what the advantage of such a function would be. You replace a one liner with a function, some error checking and no further logic.
So it was dropped.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
1 hour ago, water said:

....
So it was dropped.

:'(


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

Share this post


Link to post
Share on other sites

It doesn't make a big difference when you directly access the property

$sResult = $oObject.PropertyName

or call a function

$sResult = _GetObjectProperty($oObject, "PropertyName")

Func _GetObjctProperty($oObjcet, $sPropertyName)
    Return($oObject."PropertyName") ; Does not work
EndFunc

But it get's much more complex as soon as you need to check for a valid property name. You do not want your script to crash when a user passes no or an invalid property.
If I find the time I will dig for this old thread. IIRC there was a solution for your problem.

1 person likes this

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Thanks @water :)

 

p.s.

since I will (would) use it internally and for "well known" propertyes, I don't need to check for a valid property name.

Edited by Chimp

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

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

For some objects you can use:
https://msdn.microsoft.com/en-us/library/ms536430(v=vs.85).aspx

or try to us this:

Func GetAttribute($oObject, $sAttribute)

    Local $Peekattribute = $oObject.$sAttribute ;  < - - - - how To ?????
    ; the following even doesn't works on my system
    ; Local $Peekattribute = $oObject.getAttribute($sAttribute)
    ; Local $Peekattribute = $oObject.getAttribute($oObject.item(1))
    $Peekattribute = Execute("$oObject." & $sAttribute)

    Return $Peekattribute
EndFunc   ;==>GetAttribute

 

Try also:

https://msdn.microsoft.com/en-us/library/ff975196(v=vs.85).aspx

 

Edited by mLipok
1 person likes this

Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * 

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2017-06-04

Share this post


Link to post
Share on other sites

This works:

#include <Word.au3>
$oWord = _Word_Create()
ConsoleWrite("1> " & $oWord.Name & @CRLF)
ConsoleWrite("2> " & _PropertyGet($oWord, "Name") & @CRLF)
Exit

Func _PropertyGet($oObject, $sProperty)
    Return Execute("$oObject." & $sProperty)
EndFunc   ;==>_PropertyGet

 

1 person likes this

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

The discussion can be found here:

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Thanks a lot @mLipok and @water , I see that you both suggest the same solution of using Execute(), and since it works well for what I need then it's OK also for me :).

What I'm playing with is a way to pass obj of the browser from a script to another: since you can't pass an object from one script to another as a parameter, I needed a way to retrieve an obj from an xPath so that I can pass the xPath instead of the obj (it's a simple string and can be passed) and then retrieve again the obj from the xPath from the receiving script. This should work for browser objects.
In short, in the following script I have 2 functions:
getPathTo() a recursive function that gets the xPath of the passed obj
getElementByXpath() retrieves the obj from the xPath created by the previous. (this simple function works ONLY with the specific xPaths created by  getPathTo(). It is not intended for generic xPaths)

here I use those 2 functions within the same script just for testing, but I will use getPathTo() in one script and getElementByXpath() in another script. in the following test script I've set up a loop that pass any object behind the mouse while hovering the browser (some objects still not detected, but I think is a problem of iframes).
I was stuck on the getElementByXpath() function, but using Execute() now i can go ahead...
Thanks a lot again mLipok and Water.

#include <IE.au3>
#include <WinAPI.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <array.au3>

Local $oIE = _IECreate("www.google.com")
Local $oDoc = _IEDocGetObj($oIE)
Local $oIE_hwnd = _IEPropertyGet($oIE, "hwnd")
Local $oElement, $oElement_save, $aMousePos
Local $aBrowserPos[6] ; x , y , width , height , x right , y bottom
Local $aOverlayPos[4] ; coordinates of the overlay frame
; Create a Phantom window with a visible frame
Local $aParentPos, $AlphaKey = 0xABABAB
Local $iBorderColor = 0x00FF00, $iBorderThickness = 3
$hOverlay = GUICreate("", -50, -50, 10, 10, $WS_POPUPWINDOW, $WS_EX_LAYERED + $WS_EX_TOPMOST) ; transparent
$Panel = GUICreate("", 48, 48, 10, 10, $WS_CHILD + $WS_VISIBLE, -1, $hOverlay) ; this new window becomes a child of $hOverlay window
GUISetBkColor($iBorderColor, $hOverlay)
GUISetBkColor($AlphaKey, $Panel)
_WinAPI_SetLayeredWindowAttributes($hOverlay, $AlphaKey, 0, $LWA_COLORKEY)
GUISetState(@SW_SHOW, $hOverlay)

While WinExists($oIE_hwnd)
    $aMousePos = MouseGetPos()
    ; Now we get reference to the DOM object behind the mouse pointer
    ; If IsObj($oDoc) Then
        $oElement = $oDoc.elementFromPoint($aMousePos[0] - $oIE.document.parentwindow.screenLeft, $aMousePos[1] - $oIE.document.parentwindow.screenTop)
        If IsObj($oElement) And $oElement <> $oElement_save Then
            $oElement_save = $oElement
            $aBrowserPos[0] = Int($oIE.document.parentwindow.screenLeft) ;      X position on the desktop of the left edge of the browser
            $aBrowserPos[1] = Int($oIE.document.parentwindow.screenTop) ;       Y position on the desktop of the top border of the browser
            $aBrowserPos[2] = Int($oIE.document.documentElement.clientWidth) ;  Width of browser's client area
            $aBrowserPos[3] = Int($oIE.document.documentElement.clientHeight) ; Height of browser's client area
            $aBrowserPos[4] = $aBrowserPos[0] + $aBrowserPos[2] ;               X position on the desktop of the right border of the browser
            $aBrowserPos[5] = $aBrowserPos[1] + $aBrowserPos[3] ;               Y position on the desktop of the bottom border of the browser
            ;
            $oRect = $oElement.getBoundingClientRect() ; coordinates of the DOM object within the client area of the browser
            ;
            $aOverlayPos[0] = Int($oRect.left) + $aBrowserPos[0] ; X position of the left overlay border (absolute desktop coordinates)
            If $aOverlayPos[0] < $aBrowserPos[0] Then $aOverlayPos[0] = $aBrowserPos[0] ; if left border of the DOM obj is out of browser, overlay border stays anyway within the screen
            $aOverlayPos[1] = Int($oRect.Top) + $aBrowserPos[1] ; Y  position of the top overlay border
            If $aOverlayPos[1] < $aBrowserPos[1] Then $aOverlayPos[1] = $aBrowserPos[1] ; if top border of obj is out of browser, overlay border stays anyway within the screen
            $aOverlayPos[2] = Int($oRect.right - $oRect.left) ; width of the obj
            If $aOverlayPos[0] + $aOverlayPos[2] > $aBrowserPos[4] Then $aOverlayPos[2] = $aBrowserPos[4] - $aOverlayPos[0] ; if obj's width is out of bounds, force the edge within the browser
            $aOverlayPos[3] = Int($oRect.bottom - $oRect.top) ; Height of the obj
            If $aOverlayPos[1] + $aOverlayPos[3] > $aBrowserPos[5] Then $aOverlayPos[3] = $aBrowserPos[5] - $aOverlayPos[1] ; stay within browser's bounds
            ;
            WinMove($hOverlay, "", $aOverlayPos[0], $aOverlayPos[1], $aOverlayPos[2], $aOverlayPos[3])
            $aParentPos = WinGetPos($hOverlay) ; x, y, width, height
            WinMove($Panel, "", $iBorderThickness - 1, $iBorderThickness - 1, Int($aParentPos[2] - $iBorderThickness * 2), Int($aParentPos[3] - $iBorderThickness * 2))

            ; ----- test of functions -----
            $sxPath = getPathTo($oElement)
            ConsoleWrite($sxPath & @CRLF)
            $TargetObject = getElementByXpath($oIE.document, $sxPath)
            If IsObj($TargetObject) Then
                ConsoleWrite(IsObj($TargetObject) & @CRLF & $TargetObject.tagName & @TAB & $TargetObject.OuterText & @CRLF & "--------------" & @CRLF)
            EndIf
            ; -----------------------------
        EndIf
    ; EndIf
WEnd

; get xPath of an element
; http://stackoverflow.com/questions/2631820/im-storing-click-coordinates-in-my-db-and-then-reloading-them-later-and-showing/2631931#2631931
; answer by Scott Izu
Func getPathTo($oElement)
    If $oElement.tagName = 'HTML' Then Return '/HTML[0]'
    If $oElement = $oElement.document.body Then Return '/HTML[0]/BODY[0]'
    Local $ix = 0, $sibling
    Local $siblings = $oElement.parentNode.childNodes;
    For $sibling In $siblings
        If ($sibling = $oElement) Then
            Return getPathTo($oElement.parentNode) & '/' & $oElement.tagName & '[' & ($ix) & ']'
        EndIf
        If ($sibling.nodeType = 1 And $sibling.tagName = $oElement.tagName) Then $ix += 1
    Next
EndFunc   ;==>getPathTo

; returns the obj from the xPath created by the previous getPathTo() function
; https://www.autoitscript.com/forum/topic/107337-ieau3-can-i-get-a-specific-object-using-an-xpath/?do=findComment&comment=757505
; syntax by DaleHohm
Func getElementByXpath($oDocument, $sxPath)
    Local $array = StringSplit($sxPath, "/") ; xPath to array
    ; _ArrayDisplay($array)
    Local $sPath, $iPos, $sTag, $iTagNdx
    For $i = 2 To $array[0]
        $iPos = StringInStr($array[$i], "[")
        $sTag = StringLeft($array[$i], $iPos - 1)
        $iTagNdx = Number(StringMid($array[$i], $iPos + 1, StringLen($array[$i]) - $iPos - 1))
        $sPath &= '.getElementsByTagname("' & $sTag & '").item(' & $iTagNdx & ')'
    Next
    $0bj = Execute("$oDocument" & $sPath) ; <--- Thanks to mLipok and WATER
    ; ConsoleWrite($sPath & @TAB & IsObj($0bj) & @CRLF)
    Return $0bj
EndFunc   ;==>getElementByXpath

 

Edited by Chimp
1 person likes this

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

Share this post


Link to post
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