Chimp

Question about /AutoIt3ExecuteLine

12 posts in this topic

Hi

Is it possible to execute the following snippet using the Run() command with a syntax like this:

Run(@AutoItExe & ' /AutoIt3ExecuteLine ' & $sCommand)

where the $sCommand variable contains the statements stacked in a single line?
In the following test script I get an odd error.
Any advise is welcome.
Thanks

; Testing the /AutoIt3ExecuteLine parameter
;
; the following line works
Local $sMsg = "Hello", $Dummy = MsgBox(0, "Debug", $sMsg)
MsgBox(0, 0, "Pause")
;
; the following line doesn't instead. Why?
Local $sCommand = 'Local $sMsg = "Hello", $Dummy = MsgBox(0,"Debug", $sMsg)'
Run(@AutoItExe & ' /AutoIt3ExecuteLine ' & $sCommand)
MsgBox(0, 0, "Pause")

 


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



Try:

Local $sCommand = '"Local $sMsg = ""Hello"", $Dummy = MsgBox(0,""Debug"", $sMsg)"'
Run(@AutoItExe & ' /AutoIt3ExecuteLine ' & $sCommand)

Jos

2 people like this

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

:) that way it works.

I have a quite long $sCommand string I'm working on, I will try to use quotation marks as you showed here.

... Thank Mr. Jos

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

It is not too difficult just do the following: :)

Local $sCommand = 'Local $sMsg = "Hello", $Dummy = MsgBox(0,"Debug", $sMsg)'
$sCommand = Stringreplace($sCommand,'"', '""')
$sCommand = '"' & $sCommand & '"'
Run(@AutoItExe & ' /AutoIt3ExecuteLine ' & $sCommand)

Jos

2 people like this

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

interesting.

Local $sCommand = _
        'Local ' & _
        '$Dummy1 = MsgBox(0,"Debug", ""), ' & _
        '$Dummy2 = Sleep(500), ' & _
        '$Dummy3 = WinWaitActive("Debug",""), ' & _
        '$Dummy4 = ControlClick("Debug","","[CLASS:Button; TEXT:OK]"), ' & _
        '$Dummy5 = Sleep(3000), ' & _
        '$Dummy6 = MsgBox(0,"Debug2", "")'

$sCommand = '"' & StringReplace($sCommand, '"', '""') & '"'
ConsoleWrite($sCommand & @CRLF)
Run(@AutoItExe & ' /AutoIt3ExecuteLine ' & $sCommand)
MsgBox(0, '', 333)

This can be used for a workaround for some JavaScript Dialog box popuped with IE.au3 (was discused recently here on the forum)

 


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

thanks @Jos for the tip, very comfortable indeed.
the line that I am trying to run is as follows

Local $sCommandLine = 'local $ObjShell = ObjCreate("Shell.Application"), $ObjShellWindows = $ObjShell.Windows(), $ObjIE = $ObjShellWindows.item(' & $aRefs[1] & '), $oDocument = $ObjIE.Document, $Dummy = Execute("$oDocument"' & $sMethod & ')'

But it causes to carsh AutoIt .... :blink: (running on AutoIt v. 3.3.14.2 Win7 x64)
Perhaps there is some subtle bug I have to find in my script ...
What I'm trying to do is to perform a click on a button of the browser completely from an external script (but the whole script should be in the $sCommandLine variable instead that in an external file).

Hi @mLipok, thanks for your answer,

in the snippet you posted, you are dealing with an MsgBox, and so there is no need to pass Objcts references to the external script, while what I'm trying to do is to pass reference of our Browser obj and reference of our Element Obj (the button) to the external script so that it (the external script) can have references to what it has to manage. (sending a click in this case)
Here is my script which I am experiencing. The crash occurs in _CallFromExternal() function.

Spoiler
#include <ARRAY.AU3>

#include <IE.au3>
; Open Microsoft's demo page
$sURL = "http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/showModalDialogLaunch.htm"
$oIE = _IECreate($sURL)
$hIE = _IEPropertyGet($oIE, "hwnd")

; Click button to open modal popup
$oButton = _IETagNameGetCollection($oIE, "Button", 0)

; the following 2 lines works --- this is OK ---
; If @error = 0 And IsObj($oButton) Then _IEAction($oButton, "focus")
; ControlSend($hIE, "", "", "{ENTER}")
; ------------------------------------------------------------------
;
; _IEAction($oButton, "click") ; <--- this statement doesn't work
;
; ===== EXPERIMENTAL =====
Local $aReturn = _GetReferences($oIE, $oButton)
If Not @error Then
    ; _ArrayDisplay($aReturn)
    _CallFromExternal($aReturn)
    Sleep(1000)
Else
    MsgBox(0, "Debug", "!Error")
    Exit
EndIf
; Exit
ConsoleWrite("now script has reached this point" & @CRLF)
; -----------------
$timeout = TimerInit()
Do
    $oPopup = _IEAttach("Text Selection in a Modal Dialog --", "DialogBox")
    If TimerDiff($timeout) > 5000 Then
        MsgBox(0, "Timeout", "Popup not found" & @CRLF & "it was closed")
        Exit
    EndIf
Until IsObj($oPopup)

$TextArea = _IEGetObjById($oPopup, "output")
_IEFormElementSetValue($TextArea, "Hey! This works!")


; pass the $oIE reference to the browser and the $oNode reference to the element
Func _GetReferences(ByRef $oIE, ByRef $oElement)
    Local $hoIE = HWnd($oIE.HWnd()) ; get the handle of the browser
    Local $ObjShell = ObjCreate("Shell.Application")
    Local $ObjShellWindows = $ObjShell.Windows(); collection of all ShellWindows (IE and File Explorer)
    Local $aRefs[3] ; [0] HWND of browser; [1] item nr within Shell.Application.Windows; [0] xPath of Element
    Local $ObjTempIE ; temp variable
    For $item = 0 To ($ObjShellWindows.count) - 1
        ;
        $ObjTempIE = $ObjShellWindows.item($item) ; scann all Windows in the collection
        If HWnd($ObjTempIE.HWnd()) = $hoIE Then ; Found reference to hour browser
            $aRefs[0] = $hoIE ; HWND
            $aRefs[1] = $item ; item
            $aRefs[2] = _GetPathTo($oElement) ; Path
            $ObjTempIE = ""
            Return SetError(0, 0, $aRefs)
        EndIf
        $ObjTempIE = ""
    Next
    Return SetError(1, 0, -1)
EndFunc   ;==>_GetReferences

; 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

Func _CallFromExternal(ByRef $aRefs)
    ; replacing the $aRefs[*] with it's values whe should has
    Local $sMethod = StringReplace(StringReplace(StringReplace($aRefs[2], '/', '.getElementsByTagname("'), '[', '").item('), ']', ')') & ".click()"
    Local $sCommandLine = 'local $ObjShell = ObjCreate("Shell.Application"), $ObjShellWindows = $ObjShell.Windows(), $ObjIE = $ObjShellWindows.item(' & $aRefs[1] & '), $oDocument = $ObjIE.Document, $Dummy = Execute("$oDocument"' & $sMethod & ')'
    $sCommandLine = StringReplace($sCommandLine, '"', '""')
    $sCommandLine = '"' & $sCommandLine & '"'
    ; and now we can run it from 'external'
    ; MsgBox(0, "Debug", $sCommandLine)
    Run(@AutoItExe & ' /AutoIt3ExecuteLine ' & $sCommandLine)
    MsgBox(0, "Debug", $sCommandLine)
EndFunc   ;==>_CallFromExternal

 

 


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

#7 ·  Posted (edited)

That's is not a run-able line for me as I am missing the variables. so can't really test as you also haven't defined what you mean with "crash".

Jos

Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

@Chimp please change your example by using my "Syntax" concept of writing "code" for this "one liner" :

Local $sCommand = _
        'Local ' & _
        '$Dummy1 = MsgBox(0,"Debug", ""), ' & _
        '$Dummy2 = Sleep(500), ' & _
        '$Dummy3 = WinWaitActive("Debug",""), ' & _
        '$Dummy4 = ControlClick("Debug","","[CLASS:Button; TEXT:OK]"), ' & _
        '$Dummy5 = Sleep(3000), ' & _
        '$Dummy6 = MsgBox(0,"Debug2", "")'

$sCommand = '"' & StringReplace($sCommand, '"', '""') & '"'

This way is much easier to read.

Edited by mLipok

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

sorry for the messy code
here is the _CallFromExternal() function with some comments

Func _CallFromExternal(ByRef $aRefs)
    ; idea from here:
    ; https://www.autoitscript.com/forum/topic/11823-attaching-to-an-existing-ie-instance-one-method/#comment-81693
    ; ----- expanded form -----
    ; in input we have references to the objects in this array:
    ;
    ;            $aRefs[0] = $hoIE                  ; HWND of the passed browser
    ;            $aRefs[1] = $item                  ; item nr, of the browser Obj within the Shell.Application.Windows collection
    ;            $aRefs[2] = _GetPathTo($oElement)  ; xPath of the Element we need to manage within the browser
    ;
    ; 1) Instantiate the shell obj
    ; Local $ObjShell = ObjCreate("Shell.Application")
    ;
    ; 2) get collection of all ShellWindows (IE and File Explorer)
    ; Local $ObjShellWindows = $ObjShell.Windows()
    ;
    ; 3) Point to the targhet browser using the received item nr.
    ;    something like _IEAttach() ....
    ;    so we retrieve an Obj
    ; Local $ObjIE = $ObjShellWindows.item($aRefs[1])
    ;
    ; 4) Check if obtained obj has the same HWND of our targhet browser (it should)
    ;  .... to do
    ;
    ; 5) Point to the .Document obj
    ; Local $oDocument = $ObjIE.Document
    ;
    ; 6)  Construct the method from the given xPath
    ;     xPath for example is something like this:
    ;     /HTML[0]/BODY[0]/BUTTON[0]
    ;     and afther the StringReplaces we have something like this:
    ;     .getElementsByTagname("HTML").item(0).getElementsByTagname("BODY").item(0).getElementsByTagname("BUTTON").item(0).click()
    ;
    ; Local $sMethod = StringReplace(StringReplace(StringReplace($aRefs[2], '/', '.getElementsByTagname("'), '[', '").item('), ']', ')') & ".click()"
    ;
    ; 7) call the wanted method ageinst the element
    ; $Dummy = Execute("$oDocument" & $sMethod)
    ; ------------------------------------------------------------------------------
    ; the complete list of above Commands stacked on a single line should be like this
    ; Local $ObjShell = ObjCreate("Shell.Application"), $ObjShellWindows = $ObjShell.Windows(), $ObjIE = $ObjShellWindows.item($aRefs[1]), $oDocument = $ObjIE.Document, $Dummy = Execute("$oDocument" & $sMethod)
    ; replacing the $aRefs[*] and the $sMethod with it's values whe should has
    Local $sMethod = StringReplace(StringReplace(StringReplace($aRefs[2], '/', '.getElementsByTagname("'), '[', '").item('), ']', ')') & ".click()"
    Local $sCommandLine = 'local $ObjShell = ObjCreate("Shell.Application"), $ObjShellWindows = $ObjShell.Windows(), $ObjIE = $ObjShellWindows.item(' & $aRefs[1] & '), $oDocument = $ObjIE.Document, $Dummy = Execute("$oDocument"' & $sMethod & ')'
    $sCommandLine = StringReplace($sCommandLine, '"', '""')
    $sCommandLine = '"' & $sCommandLine & '"'
    ; and now we can run it from 'external' ...??  maybe...
    ; MsgBox(0, "Debug", $sCommandLine)
    Run(@AutoItExe & ' /AutoIt3ExecuteLine ' & $sCommandLine)
    MsgBox(0, "Debug: CommadLine", $sCommandLine) ; this is the resulting Command Line
EndFunc   ;==>_CallFromExternal

 


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
1 hour ago, Jos said:

That's is not a run-able line for me as I am missing the variables. so can't really test as you also haven't defined what you mean with "crash".

Jos

The script from Post #6 (within spoiler) is runnable and I get an "APPCRASH" window running it:

2lcurex.jpg


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

just for the record
the APPCRASH was caused by the presence of the Execute() statement within the $sCommandLine string.
by replacing this part   $Dummy = Execute("$oDocument"' & $sMethod & ')'
with this part   $Dummy = $oDocument' & $sMethod  solved the problem.


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
19 hours ago, mLipok said:

This can be used for a workaround for some JavaScript Dialog box popuped with IE.au3 (was discused recently here on the forum)

Here is example how this can be used with IE.au3 :

 

 


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

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