genius257

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

1 post in this topic

#1 ·  Posted (edited)

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

    • fosil
      By fosil
      Hi everyone.

      I'm currently working a program that constantly prints out log files through "consolewrite" and the "#AutoIt3Wrapper_Change2CUI=y" wrapper.

      Part of this program requires me to run a batch script.

      My issue is the batch script launches from the same window as consolewrite. I need the batch file to be launched through a different window as currently this causes an issue with the logs (which need to be very precise) but also causes the batch file to produce some funny behavior...

      Does anyone know how I can force the file to run on a second DOS window?

      Thanks in advance!!!
       
      Edit: Im using the "run" command if that helps. I tried "shellexecute" but that seemed to not launch the batch scripts at all.
    • nacerbaaziz
      By nacerbaaziz
      Hello guys
      I have an inquiry regarding bass.dll
      I wanted to record an audio file and did not know the way
      As well as I want to save the file after applying the effects
      can you help me?
      am sorry for my english
      With my greetings and best wishes
    • Chimp
      By Chimp
      Hi all.
      I'm playing a bit with the ScriptControl object using as base this example by @genius257.
      here is a very simple try, ... but it fails. what I'm doing wrong?
      Local $oSC = ObjCreate("ScriptControl") $oSC.Language = "JScript" $oSC.ExecuteStatement('alert("I am a javascript alert box!");') MsgBox(0,'AutoIt','Pause') Thanks
    • kenny4896
      By kenny4896
      Hi All,
      I tried my best to looking a solution from this forum or others, but I have no luck. The imagesearch dll working fine in my 64 bits machine, whenever I move the scripts to Win 7 (32 bits), it crashes. ImageSearchDLL.dll, ImageSearch.au3 and my simple scripts are in the same working directory. For win 7 (32 bits) machine, I'm using 32 bits ImageSearchdll.dll, the simple scripts as below:
      #include <ImageSearch.au3>
      Global $y = 0, $x = 0
      Local $search = _ImageSearch("C:\Program Files\AutoIt3\Examples\Images\mypc.PNG", 0, $x, $y, 0)
      if $search = 1 then
         MsgBox(0,"Found","Found my target here..." & $x & "," & $y)
         MouseMove($x, $y, 10)
      Else
         msgbox(0, "Not found","")
      Endif
      However, I received error message below when running the script. Anyone here got clue? 
      Problem Event Name:    APPCRASH
        Application Name:    AutoIt3.exe
        Application Version:    3.3.14.2
        Application Timestamp:    55fc1979
        Fault Module Name:    AutoIt3.exe
        Fault Module Version:    3.3.14.2
        Fault Module Timestamp:    55fc1979
        Exception Code:    c0000005
        Exception Offset:    00020e42
        OS Version:    6.1.7601.2.1.0.256.4
        Locale ID:    17417
        Additional Information 1:    0a9e
        Additional Information 2:    0a9e372d3b4ad19135b953a78882e789
        Additional Information 3:    0a9e
        Additional Information 4:    0a9e372d3b4ad19135b953a78882e789