Jump to content
genius257

Is it possible to throw an error in a ScriptControl object, via AutoIt

Recommended Posts

genius257

First of all I've been searching on the forum, and while there's been done something like this with: https://www.autoitscript.com/forum/topic/141004-comobject-proxy-seamless-windows-script-control-autoitobj/

I could not find any solution to my predicament.

 

Is it possible to throw an error in a ScriptControl object, via AutoIt?

I have a script, where AutoIt calls ScriptControl:JavaScript which in turn calls AutoIt.

However i would like to be able to make AutoIt invoke an exception within ScriptControl, if the called functionality fails.

Here's some code for reference:

#AutoIt3Wrapper_Run_AU3Check=n

#include-once
#include "AutoitObject.au3"
#include <WinAPIDiag.au3>

$oJS = ObjCreate("ScriptControl")
$oJS.Language = "JScript"
$oJS.TimeOut = 0; A value of 0 means that the ScriptControl will monitor the execution of the script and will trigger the Timeout event if it determines that the script is hung.
_AutoItObject_Startup()
$oAutoIt = _AutoItObject_Create()
_AutoItObject_AddMethod($oAutoIt, "Execute", "_Execute", False)
$oJS.AddObject("AutoIt", $oAutoIt, True)

OnAutoItExitRegister("_CleanUp")

$_AutoItError = ObjEvent("AutoIt.Error", "_AutoItError")

Func _CleanUp()
    ConsoleWrite("Cleaning up..."&@CRLF)
    $oJS = 0
    $oAutoIt = 0
    _AutoItObject_Shutdown()
EndFunc

Func _Execute($oSelf, $sString)
    $vReturn = Execute($sString)
    If @error<>0 Then Return $oJS.Eval("throw new SyntaxError();"); Does invoke exception, but not within the try/catch
    Return $vReturn
EndFunc

Func _AutoItError($oError)
    $oError2 = $oJS.Error
    ConsoleWrite( _
        "Column: " & $oError2.Column & @CRLF & _
        "Description: " & $oError2.Description & @CRLF & _
        "HelpContext: " & $oError2.HelpContext & @CRLF & _
        "HelpFile: " & $oError2.HelpFile & @CRLF & _
        "Line: " & $oError2.Line & @CRLF & _
        "Number: " & $oError2.Number & @CRLF & _
        "Source: " & $oError2.Source & @CRLF & _
        "Text: " & $oError2.Text & @CRLF _
    )
    $oError2.Clear()
EndFunc

$oJS.Eval("(function(){"& _
    "try{"& _
        "AutoIt.Execute('MsgBox(0,\'\', \'a\')');"& _
        "AutoIt.Execute('a.b');/*error should occur here*/"& _
        "AutoIt.Execute('MsgBox(0,\'\', \'b\')');"& _
    "}catch(e){"& _
        "AutoIt.Execute('ConsoleWrite(\'Error\'&@CRLF)');"& _
    "}"& _
"})()")

 

Edited by genius257

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

  • Similar Content

    • guinness
      By guinness
      Just trying out the latest version of AutoIt and thinking more functional
      #include <Array.au3> ; Example ; An example of filtering an array and reducing an array, using a function reference. ; This is similiar to how it would be done in the likes of JavaScript i.e. more functional (declarative) Local $aArray[] = [1, 2, 3, 50, 30, 40, 20, 30] Local $aFiltered = _ArrayFilter($aArray, GtrThan30) _ArrayDisplay($aFiltered, '_ArrayFilter::') Local $iSum = _ArrayReduce($aArray, SumValues) ConsoleWrite('_ArrayReduce::' & $iSum & @CRLF) Func SumValues($a, $b) Return $a + $b EndFunc ;==>SumValues Func GtrThan30($vValue) Return $vValue > 30 EndFunc ;==>GtrThan30 Func _ArrayFilter($aArray, $fFunc) If Not IsArray($aArray) Then ; Null is more appropriate than returning the likes of -1 or an empty array Return SetError(1, 0, Null) EndIf If Not IsFunc($fFunc) Then ; Null is more appropriate than returning the likes of -1 or an empty array Return SetError(2, 0, Null) EndIf Local Const $iLen = UBound($aArray) - 1 Local $aFiltered[$iLen] Local $iIndex = 0 For $i = 0 To $iLen Local $bIsFiltered = __ArrayCall($fFunc, $aArray[$i], $i) If @error Then Return SetError(@error, @extended, Null) ElseIf $bIsFiltered Then $aFiltered[$iIndex] = $aArray[$i] $iIndex += 1 EndIf Next ReDim $aFiltered[$iIndex] Return $aFiltered EndFunc ;==>_ArrayFilter Func _ArrayReduce($aArray, $fFunc, $vInitial = Default) If Not IsArray($aArray) Then ; Null is more appropriate than returning the likes of -1 or an empty array Return SetError(1, 0, Null) EndIf If Not IsFunc($fFunc) Then ; Null is more appropriate than returning the likes of -1 or an empty array Return SetError(2, 0, Null) EndIf Local $bHasInitial = @NumParams >= 3 For $i = 0 To UBound($aArray) - 1 If $bHasInitial Then $vInitial = __ArrayCall($fFunc, $vInitial, $aArray[$i], $i) If @error Then Return SetError(@error, @extended, Null) EndIf Else $bHasInitial = True $vInitial = $aArray[$i] EndIf Next Return $vInitial EndFunc ;==>_ArrayReduce Func __ArrayCall($fFunc, $vArg1 = Default, $vArg2 = Default, $vArg3 = Default) Local $vRetValue = Call($fFunc, $vArg1) If @error = 0xDEAD And @extended = 0xBEEF Then $vRetValue = Call($fFunc, $vArg1, $vArg2) If @error = 0xDEAD And @extended = 0xBEEF Then $vRetValue = Call($fFunc, $vArg1, $vArg2, $vArg3) If @error = 0xDEAD And @extended = 0xBEEF Then ; The function exists, but there is no appropriate function signature Return SetError(3, 0, Null) EndIf EndIf EndIf Return SetError(@error, @extended, $vRetValue) EndFunc ;==>__ArrayCall  
    • DynamicRookie
      By DynamicRookie
      Hey There!
       
      So, what i need to do is an app that can read text in a image (I.e. a png that has text saying "This is a png" and return the text to a variable)
      I'm pretty much a newbie on AutoIt, my purpose is doing that but i don't know any function that can

      Any help is much appreciated
    • santoshM
      By santoshM
      How can i exit from a procedure in auto
      Func test() if x=o then     return endif endFunc  
    • Valnurat
      By Valnurat
      Hi
      Small question.
      I trying to find all index in an array with value higher than 7.
      How can that be possible?
    • TrashBoat
      By TrashBoat
      Could someone help me create or give an idea of how to do a incrementing for loop that would do this: https://i.imgur.com/YFUt47H.gifv
      I'm having a hard time figuring it out :S
×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.