Search the Community
Showing results for tags 'JScript'.
-
Since W8.1 (actually IE 11 with W7 will work also) microsoft has a Javascript runtime that can be called from scripting languages As shown in this example from C++: https://iobservable.net/blog/2013/11/12/introduction-to-jsrt/ By loading either JSCRIPT9.DLL or CHAKRA.DLL you can embed ECMA JavaScript in your application And implemented here in AutoHotKey https://autohotkey.com/boards/viewtopic.php?f=6&t=5739 Triggered by other threads: https://www.autoitscript.com/forum/topic/185883-accessing-autoit-variables/ https://iobservable.net/blog/2013/11/12/introduction-to-jsrt/ https://www.autoitscript.com/forum/topic/184824-chakracore-udf-executing-javascript-in-autoit/ http://eclipsesource.com/blogs/2016/04/06/getting-started-with-microsoft-chakracore/ This code is almost working but unfortunately not with the expected output. Someone seeing what is wrong? It runs completely after fixes of DanyFirex ;~ https://iobservable.net/blog/2013/11/ ;~ Should work on all windows versions with IE11 #AutoIt3Wrapper_UseX64=N #Region enum JsErrorCode : unsigned int Enum $JsNoError = 0, _ $JsErrorCategoryUsage = 0x10000, _ $JsErrorInvalidArgument, _ $JsErrorNullArgument, _ $JsErrorNoCurrentContext, _ $JsErrorInExceptionState, _ $JsErrorNotImplemented, _ $JsErrorWrongThread, _ $JsErrorRuntimeInUse, _ $JsErrorBadSerializedScript, _ $JsErrorInDisabledState, _ $JsErrorCannotDisableExecution, _ $JsErrorHeapEnumInProgress, _ $JsErrorArgumentNotObject, _ $JsErrorInProfileCallback, _ $JsErrorInThreadServiceCallback, _ $JsErrorCannotSerializeDebugScript, _ $JsErrorAlreadyDebuggingContext, _ $JsErrorAlreadyProfilingContext, _ $JsErrorIdleNotEnabled, _ $JsErrorCategoryEngine = 0x20000, _ $JsErrorOutOfMemory, _ $JsErrorCategoryScript = 0x30000, _ $JsErrorScriptException, _ $JsErrorScriptCompile, _ $JsErrorScriptTerminated, _ $JsErrorScriptEvalDisabled, _ $JsErrorCategoryFatal = 0x40000, _ $JsErrorFatal ;~ }JsErrorCode; #EndRegion enum JsErrorCode : unsigned int #Region typedef enum JsRuntimeVersion Enum $JsRuntimeVersion10 = 0, $JsRuntimeVersion11 = 1 #EndRegion typedef enum JsRuntimeVersion ; Create callback function. Local $hPrintf = DllCallbackRegister("_printf", "long", "ptr;bool;ptr;ushort;ptr") Local $hJSRuntime = DllOpen("c:\windows\system32\jscript9.dll") $JsRuntimeAttributeNone = 0x00000000 ;~ #RequireAdmin Example() Func Example() Local $script = "native.printf('hello world')" ;~ Local $script = "native.printf('number=%#x string=%s\n', 255, 'test')" ;~ Local $script = "(()=>{return 'Hello world!';})()" ;~ Local $script = "var x='helloworld'; return x;" ;~ Local $script = "var x=10;" & @CRLF & "var y=11;" ;~ Local $script = "(42);" ;~ Local $script = "(" & @CRLF ;~ $script=$script & "host.echo(JSON.stringify({foo:'bar'}));)" & @CRLF ;~ $script=$script & "42 // The script's result :) "& @CRLF ;~ $script=$script & ")"& @CRLF ;~ $script=$script & "class Host {"& @CRLF ;~ $script=$script & " echo(s) {"& @CRLF ;~ $script=$script & " MsgBox %s%"& @CRLF ;~ $script=$script & " }"& @CRLF ;~ $script=$script & "}"& @CRLF Local $aResult Local $runtime Local $context ;~ // Create a runtime ;~ JsCreateRuntime(JsRuntimeAttributeNone, nullptr, &runtime); $runtime = _JsCreateRuntime($JsRuntimeAttributeNone, 0, $runtime) ; ;~ // Create an execution context. ;~ JsCreateContext(runtime, &context); $context = _JsCreateContext($runtime, $context) ;~ // Now set the current execution context. ;~ JsSetCurrentContext(context); _JsSetCurrentContext($context) ; ; Get the Global object for adding stuff ;~ JsGetGlobalObject(&global); Local $global $global = _JsGetGlobalObject($global) ;~ JsPropertyIdRef nativeProp; ;~ JsGetPropertyIdFromName(L"native", &nativeProp); Local $nativeProp $nativeProp = _JsGetPropertyIdFromName("native", $nativeProp) ;~ JsValueRef nativeObj; ;~ JsCreateObject(&nativeObj); Local $nativeObj $nativeObj = _JsCreateObject($nativeObj) ;~ JsPropertyIdRef printfProp; ;~ JsGetPropertyIdFromName(L"printf", &printfProp); Local $printfProp $printfProp = _JsGetPropertyIdFromName("printf", $printfProp) ;~ JsValueRef printfFunc; ;~ JsCreateFunction(PrintFormat, nullptr, &printfFunc); Local $printfFunc $printfFunc = _JsCreateFunction(DllCallbackGetPtr($hPrintf), 0, $printfFunc) ;~ JsSetProperty(nativeObj, printfProp, printfFunc, true); _JsSetProperty($nativeObj, $printfProp, $printfFunc, True) ;~ JsSetProperty(global, nativeProp, nativeObj, true); _JsSetProperty($global, $nativeProp, $nativeObj, True) ;~ STDAPI_(JsErrorCode) JsCreateFunction( ;~ _In_ JsNativeFunction nativeFunction, ;~ _In_opt_ void *callbackState, ;~ _Out_ JsValueRef *function ;~ ); ; Get a JsValueRef for our Host object ;~ hostRef := ToJsValue(Host) ;~ $aResult=DllCall($hJSRuntime, "int","JsVariantToValue", "ptr", &variant, "ptr*", valref) ; Pass our Host object to the script engine ;~ DllCall("jscript9\JsSetProperty", "ptr", globalObject, "ptr", hostPropertyId, "ptr", hostRef, "int", true) ;~ // Run the script. ;~ STDAPI_(JsErrorCode) JsRunScript( ;~ _In_z_ const wchar_t *script, ;~ _In_ JsSourceContext sourceContext, ;~ _In_z_ const wchar_t *sourceUrl, ;~ _Out_ JsValueRef *result ;~ ); Local $currentSourceContext = 1 ;~ JsRunScript(script.c_str(), currentSourceContext++, L"", &result); Local $result = "" Local $sourceURL = "" $aResult = DllCall($hJSRuntime, "int", "JsRunScript", "WSTR", $script, "UINT*", $currentSourceContext, "wstr", "dummysource.js", "WSTR*", $result) ConsoleWrite("Error 11: " & @error & @CRLF) If @error = 0 Then ConsoleWrite($aResult[0] & ";" & $aResult[1] & ";" & $aResult[2] & ";" & $aResult[3] & ";" & $aResult[4] & @CRLF) EndIf ;~ // Convert your script result to String in JavaScript redundant if your script returns a String ;~ JsValueRef resultJSString; ;~ STDAPI_(JsErrorCode) JsConvertValueToString( ;~ _In_ JsValueRef value, ;~ _Out_ JsValueRef *stringValue ;~ ); ;~ JsConvertValueToString(result, &resultJSString); ;~ $aResult=DllCall($hJSRuntime, "int", "JsConvertValueToString", "WSTR", $script, "long", 1, "WSTR*",0, "WSTR*", $result) ;~ consolewrite("Error 4: " & @error & @CRLF) ;~ if @error=0 Then ;~ consolewrite($aResult[0] & ";" & $aResult[1] & ";" & $aResult[2] & ";" & $aResult[3] & ";" & $aResult[4] & @CRLF) ;~ EndIf ;~ ;~ // Project script result back to C++. ;~ const wchar_t *resultWC; ;~ size_t stringLength; ;~ JsStringToPointer(resultJSString, &resultWC, &stringLength); ;~ wstring resultW(resultWC); ;~ cout << string(resultW.begin(), resultW.end()) << endl; ;~ system("pause"); ;~ // Dispose runtime ;~ JsSetCurrentContext(JS_INVALID_REFERENCE); ;~ JsDisposeRuntime(runtime); Return 0 ; EndFunc ;==>Example ; Create callback function. ;~ JsValueRef CALLBACK PrintFormat(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState) ;~ { ;~ const wchar_t *format; ;~ size_t length; ;~ JsStringToPointer(arguments[1], &format, &length); ;~ VARIANT variant; ;~ JsValueToVariant(arguments[2], &variant); ;~ const wchar_t *str; ;~ JsStringToPointer(arguments[3], &str, &length); ;~ wprintf(format, variant.intVal, str); ;~ return JS_INVALID_REFERENCE; ;~ } ;~ typedef _Ret_maybenull_ JsValueRef (CALLBACK * JsNativeFunction)( ;~ _In_ JsValueRef callee, ;~ _In_ bool isConstructCall, ;~ _In_ JsValueRef *arguments, ;~ _In_ unsigned short argumentCount ;~ ); Func _printf($callee, $isConstructCall, $arguments, $argumentCount, $callbackState) ConsoleWrite("there we are with " & $argumentCount & " arguments" & @CRLF) Local $tArgs = DllStructCreate("ptr JsValues[" & $argumentCount & "]", $arguments) ;~ STDAPI_(JsErrorCode) JsConvertValueToString( ;~ _In_ JsValueRef value, ;~ _Out_ JsValueRef *stringValue ;~ ); ;~ JsConvertValueToString(result, &resultJSString); local $tresult $aResult=DllCall($hJSRuntime, "int", "JsConvertValueToString", "ptr", DllStructGetData($tArgs,1,2), "ptr*", $tresult) consolewrite("Error 4: " & @error & @CRLF) if @error=0 Then consolewrite( $aResult[0] & ";" & $aResult[1] & ";" & $aResult[2] & @CRLF) $tresult=$aresult[2] EndIf ;~ STDAPI_(JsErrorCode) JsValueToVariant( ;~ _In_ JsValueRef object, ;~ _Out_ VARIANT *variant ;~ ); ;~ Local $aResult = DllCall($hJSRuntime, "dword", "JsValueToVariant", "ptr", $arguments[0], "ptr*", 0) ;~ If @error Then Return SetError(3, 0, @error) ;~ If $aResult[0] <> 0 Then Return SetError(4, 0, $aResult[0]) ;~ consolewrite($aResult[2]) Local $aResult = DllCall($hJSRuntime, "dword", "JsStringToPointer", "ptr", $tResult, "ptr*", 0, "int*", 0) If @error Then Return SetError(3, 0, @error) If $aResult[0] <> 0 Then Return SetError(4, 0, $aResult[0]) Local $tString = DllStructCreate("wchar string["&$aResult[3]&"]", $aResult[2]) consolewrite("+Parameter: " & $tString.string & @CRLF) Return 0 EndFunc ;==>_printf ;~ typedef void *JsRuntimeHandle; ;~ // Create a runtime. ;~ Edge mode signature STDAPI_(JsErrorCode) JsCreateRuntime( ;~ _In_ JsRuntimeAttributes attributes, ;~ _In_opt_ JsThreadServiceCallback threadService, ;~ _Out_ JsRuntimeHandle *runtime); ;~ Legacy mode signature ;~ STDAPI_(JsErrorCode) JsCreateRuntime( ;~ _In_ JsRuntimeAttributes attributes, ;~ _In_ JsRuntimeVersion version, ;~ _In_opt_ JsThreadServiceCallback threadService, ;~ _Out_ JsRuntimeHandle *runtime ;~ ); ;~ $runtime=_JsCreateRuntime(JsRuntimeAttributeNone, 0, $runtime); Func _JsCreateRuntime($JsRuntimeAttributeNone, $JSRuntimeVersion, $runtime) $aResult = DllCall($hJSRuntime, "int", "JsCreateRuntime", "int", $JsRuntimeAttributeNone, "int", $JSRuntimeVersion, "ptr", 0, "ptr*", $runtime) If @error Then Return SetError(1, @error, 0) Return $aResult[4] EndFunc ;==>_JsCreateRuntime ;~ // Edge mode signature ;~ STDAPI_(JsErrorCode) JsCreateContext( ;~ _In_ JsRuntimeHandle runtime, ;~ _Out_ JsContextRef *newContext); ;~ // Legacy mode signature ;~ STDAPI_(JsErrorCode) JsCreateContext( ;~ _In_ JsRuntimeHandle runtime, ;~ _In_ IDebugApplication *debugApplication, ;~ _Out_ JsContextRef *newContext ;~ ); ;~ JsCreateContext(runtime, &context); Func _JsCreateContext($runtime, $context) $aResult = DllCall($hJSRuntime, "int", "JsCreateContext", "ptr", $runtime, "ptr", 0, "ptr*", $context) If @error Then Return SetError(1, @error, 0) Return $aResult[3] EndFunc ;==>_JsCreateContext ;~ STDAPI_(JsErrorCode) JsSetCurrentContext( ;~ _In_ JsContextRef context ;~ ); ;~ JsSetCurrentContext(context); Func _JsSetCurrentContext($context) $aResult = DllCall($hJSRuntime, "int", "JsSetCurrentContext", "ptr", $context) If @error Then Return SetError(1, @error, 0) Return $JsNoError EndFunc ;==>_JsSetCurrentContext ; Get the Global object for adding stuff ;~ STDAPI_(JsErrorCode) JsGetGlobalObject( ;~ _Out_ JsValueRef *globalObject ;~ ); ;~ JsValueRef global; ;~ JsGetGlobalObject(&global); Func _JsGetGlobalObject($global) $aResult = DllCall($hJSRuntime, "int", "JsGetGlobalObject", "ptr*", $global) If @error Then Return SetError(1, @error, 0) Return $aResult[1] EndFunc ;==>_JsGetGlobalObject ; Get a property ID for the name "host" ;~ STDAPI_(JsErrorCode) JsGetPropertyIdFromName( ;~ _In_z_ const wchar_t *name, ;~ _Out_ JsPropertyIdRef *propertyId ;~ ); ;~ DllCall("jscript9\JsGetPropertyIdFromName", "wstr", "host", "ptr*", hostPropertyId) ;~ local $hostPropertyID ;~ $aResult=DllCall($hJSRuntime, "int","JsGetPropertyIdFromName", "wstr", "host", "ptr*", $hostPropertyId) ;~ consolewrite("Error 5: " & @error & @CRLF) ;~ if @error=0 Then ;~ consolewrite($aResult[0] & ";" & $aResult[1] & ";" & $aResult[2] & @CRLF) ;~ EndIf ;~ JsPropertyIdRef nativeProp; ;~ JsGetPropertyIdFromName(L"native", &nativeProp); Func _JsGetPropertyIdFromName($propname, $nativeProp) $aResult = DllCall($hJSRuntime, "int", "JsGetPropertyIdFromName", "wstr", $propname, "ptr*", $nativeProp) If @error Then Return SetError(1, @error, 0) Return $aResult[2] EndFunc ;==>_JsGetPropertyIdFromName ;~ STDAPI_(JsErrorCode) JsCreateObject( ;~ _Out_ JsValueRef *object ;~ ); ;~ JsValueRef nativeObj; ;~ JsCreateObject(&nativeObj); Local $nativeObj Func _JsCreateObject($JSRTobject) $aResult = DllCall($hJSRuntime, "int", "JsCreateObject", "ptr*", $JSRTobject) If @error Then Return SetError(1, @error, 0) Return $aResult[1] EndFunc ;==>_JsCreateObject ;~ STDAPI_(JsErrorCode) JsCreateFunction( ;~ _In_ JsNativeFunction nativeFunction, ;~ _In_opt_ void *callbackState, ;~ _Out_ JsValueRef *function ;~ ); Func _JsCreateFunction($fncCallBackPtr, $callbackState, $fncVar) $aResult = DllCall($hJSRuntime, "int", "JsCreateFunction", "ptr", $fncCallBackPtr, "ptr*", $callbackState, "ptr*", $fncVar) If @error Then Return SetError(1, @error, 0) Return $aResult[3] EndFunc ;==>_JsCreateFunction ;~ JsSetProperty(nativeObj, printfProp, printfFunc, true); ;~ STDAPI_(JsErrorCode) JsSetProperty( ;~ _In_ JsValueRef object, ;~ _In_ JsPropertyIdRef propertyId, ;~ _In_ JsValueRef value, ;~ _In_ bool useStrictRules ;~ ); Func _JsSetProperty($Obj, $Prop, $Func, $val) ; $aResult = DllCall($hJSRuntime, "int", "JsSetProperty", "ptr", $Obj, "ptr", $Prop, "ptr", $Func, "int", $val) If @error Then Return SetError(1, @error, 0) EndFunc ;==>_JsSetProperty
-
I've been thinking about this for the last couple of weeks, and I've finally got around to putting together a proof of concept. As far as OOP goes, Javascript is a pretty damn good language -- very flexible, but lets look at some code. Here is a two button form for AutoIt written in Javascript. var $=this; var hgui; this.MsgHandler_=function (m){ if (m==$.AutoIt.GUI_EVENT_CLOSE) { $.AutoIt.GUIDelete(hgui); return true; }else if (m==button1) { $.AutoIt.Run("notepad.exe"); }else if (m==button2) { alert('Thanks from a javascript alert box!'); } return false; } var button1,button2; function Example1(){ var msg; hgui=$.AutoIt.GUICreate("My Gui",400,500); button1=$.AutoIt.GUICreateButton("Run Notepad",10,10,200,30); button2=$.AutoIt.GUICreateButton("Press Me",10,40,200,30); $.AutoIt.GUISetState($.AutoIt.SW_SHOW,hgui); } Example1(); This code would reside in an external file and be called by AutoIt. I've created a framework in AutoIt using AutoItObject that is just big enough to make this code work. Basically I am creating an AutoItObject, tacking on some core AutoIt functions and constants, and sending it off to play in Javascript land. There is much more to be tacked on. Other than this AutoItObject being a proxy for AutoIt functionality, there is a Javascript COM object into which the javascript file is loaded, and a message loop that calls back into the javascript. The framework is 300 lines of code. Before I extend this, I want to know if AutoIt ALREADY has a COM object I that I can use like this. But I am thinking that even if it does, this hand-coded way would be better because you can make the calls pass objects instead of parameters. Here is the framework: #include <AutoItObject.au3> #include <GUIConstants.au3> Global $thisfile=@ScriptFullPath; Global $logg=$thisfile&".log.txt"; Global $jsfile=$thisfile&".js"; ;e C:\batch\borg\TestJsaio.au3.js ;fret not, you do not need, will be a no-op if not exist Global Const $snarl="C:\batch\Snarl_CMD.exe"; ; need script exit for AIO shutdown OnAutoItExitRegister("EvtScriptExit") _AutoItObject_StartUp() ; error handler Global $oError $oError = ObjEvent("AutoIt.Error", "_ErrFunc") ; JScript Com globals Global $_ComObj_proxy Global $_com Global $_Js JScriptInit() ;;;;;;;;;;;;;;;;;;;;;;;;;;done inits ;;;;;;;;;;; begin framework ; set up a mini framework of AutoIt Functions/constants and stick them on an object Global $AutoItForCom=_AutoItObject_Create(); ;_AutoItObject_AddMethod(ByRef $oObject, $sName, $sAutoItFunc, $fPrivate = False) _AutoItObject_AddMethod($AutoItForCom,"GUICreate","GUICreate_AI") _AutoItObject_AddMethod($AutoItForCom,"GUISetState","GUISetState_AI") _AutoItObject_AddMethod($AutoItForCom,"GUIGetMsg","GUIGetMsg_AI") _AutoItObject_AddMethod($AutoItForCom,"GUIDelete","GUIDelete_AI") _AutoItObject_AddMethod($AutoItForCom,"MsgBox","MsgBox_AI") _AutoItObject_AddMethod($AutoItForCom,"GuiCreateButton","GuiCreateButton_AI") _AutoItObject_AddMethod($AutoItForCom,"Run","Run_AI") ;_AutoItObject_AddProperty(ByRef $oObject, $sName, $iFlags = $ELSCOPE_PUBLIC, $vData = "") _AutoItObject_AddProperty($AutoItForCom, "SW_SHOW", $ELSCOPE_PUBLIC+$ELSCOPE_READONLY, @SW_SHOW) _AutoItObject_AddProperty($AutoItForCom, "GUI_EVENT_CLOSE", $ELSCOPE_PUBLIC+$ELSCOPE_READONLY, $GUI_EVENT_CLOSE) ;;; here are the tie-ins that the object will call Func GUICreate_AI($me,$title,$width,$height) #forceref $me ;GUICreate ( "title" [, width [, height [, left [, top [, style [, exStyle [, parent]]]]]]] ) Local $rv=GUICreate($title,$width,$height) Return $rv EndFunc Func GUISetState_AI($me,$flag,$hwnd) #forceref $me ;GUISetState ( [flag [, winhandle]] ) Local $rv=GUISetState($flag,HWnd($hwnd));need to covert Int32 Return $rv EndFunc Func GUIGetMsg_AI($me,$advanced) #forceref $me Local $rv= GUIGetMsg ($advanced) Return $rv EndFunc Func GUIDelete_AI($me,$winhandle) #forceref $me Local $rv=GUIDelete($winhandle) Return $rv EndFunc Func MsgBox_AI($me,$flag,$title,$text) #forceref $me ;MsgBox ( flag, "title", "text" [, timeout [, hwnd]] ) Local $rv=MsgBox($flag,$title,$text) Return $rv EndFunc Func GUICreateButton_AI($me,$text,$left,$top,$width=Default,$height=Default,$style=Default,$exstyle=Default) #forceref $me ;GUICtrlCreateButton ( "text", left, top [, width [, height [, style [, exStyle]]]] ) Local $rv=GUICtrlCreateButton ( $text, $left, $top, $width, $height, $style, $exstyle) Return $rv EndFunc Func Run_AI($me,$program,$wkdir=Default,$show_flag=Default,$opt_flag=Default) #forceref $me ;Msg2("run",$program) Local $rv; If False Then ElseIf @NumParams==2 Then $rv=Run($program) ElseIf @NumParams==3 Then $rv=Run($program,$wkdir) ElseIf @NumParams==4 Then $rv=Run($program,$wkdir,$show_flag) ElseIf @NumParams==5 Then $rv=Run($program,$wkdir,$show_flag,$opt_flag) EndIf ;Run ( "program" [, "workingdir" [, show_flag [, opt_flag]]] ) ;Local $rv=Run($program,$wkdir,$show_flag,$opt_flag) Return $rv EndFunc ;;;;;;;;;;;;;;;; end of tie-ins ;;;;;;;;;; setup JScript COM obj Global $jso=NewComObj() ; import our AutoIt Proxy into the jso $jso.set("AutoIt", $AutoItForCom) ; read our javascript file Global $ftext=FileRead($jsfile) ; set the file text up as a Main function $jso.set_function("Main", "", $ftext) ; need to run our message loop in autoit code, but pass it back to JS land here $jso.set_function("MsgHandler", "m", "return eval('this.MsgHandler_(m)');") ; hold on to your britches... ; this call will run through the js file and the file must do a setup and have a callback MsgHandler_ $jso.Main() ;; Message Loop Local $end_if_true Local $m While True $m=GUIGetMsg(0) $end_if_true=$jso.MsgHandler($m) ;Msg($end_if_true); If $end_if_true Then ExitLoop EndIf WEnd Exit ;; the only thing worth looking at below this point is the _ComObj_init and friends ;;;;;;;;;;;;;;;functions Func EvtScriptExit() ;Msg("EvtScriptExit") _AutoItObject_Shutdown() ;_logline('EvtScriptExit') ;If IsObj($MidiMgr) Then ; $MidiMgr.Terminate() ;EndIf EndFunc Func Msg($s) MsgBox(0,$thisfile,$s) EndFunc Func Msg2($t,$s) MsgBox(0,$t,$s) EndFunc Func logclear() FileDelete($logg) EndFunc Func _logline($line) logline($line) EndFunc Func logline($line) Local $fh1=FileOpen($logg,1); If $fh1<>-1 Then FileWriteLine($fh1,$line) FileClose($fh1) EndIf EndFunc Func logsnarl($line) logerr($line) snarl(10,'Fatal Error',$line) EndFunc Func snarl($i,$t,$s) If Not FileExists($snarl) Then Return EndIf $s1=StringReplace($s,'"',"'") $t1=StringReplace($t,'"',"'") $cmd=$snarl&' snShowMessage '&$i&' "'&$t1&'" "'&$s1&'"'; Run($cmd) EndFunc ;;;;js utils ; #FUNCTION# ================================================================== ; Name : _ComObj_init ; Description : Creates MS Windows Script control and deploy it as proxy for ; AutoIt COM object binding. ; Syntax : _ComObj_init([$VBScriptSupport = false]) ; Parameter : $VBScriptSupport ; By default JScript doesn't have alert() function, it is provided ; by browser's window object. ; We can emulate this using VBScript's MsgBox function, which is ; performance hog because we need another ScriptControl instance. ; Other advantage is to be able to execute other VBScript's methods ; within function via vb.Eval() method. ; This feature is disabled by default. ; ============================================================================= Func _ComObj_init ($VBScriptSupport = false) Local $_Script $_Script = ObjCreate("ScriptControl") $_Script.Language = "JScript" $_Script.Timeout = 60000 ;$_Script.AddCode("var $=this,arrays=[],vb;function set_vbscript($) {vb=$;vb.Language='VBScript';vb.AllowUI=true;}function alert(s){if(vb)vb.Eval('MsgBox(Unescape(""'+s+'""), 0, ""Autoit"")')}function get_proxy(){return $}function new_object($){$=new Function('return{'+$+'}')();$.set=function(k, v){$[k]=v};$.unset=function(k){delete $[k]};$.set_object=function(k,$){this.set(k,new_object($))};$.set_array=function(){var v=[];for(var i=0;i<arguments.length;i++)v[i]=arguments[i];$.set(v.shift(),new_array.apply(this,v))};$.set_function=function(k,p,$){this.set(k,new_function(p,$))};return $}function new_array(){var v=[];for(var i=0;i<arguments.length;i++)v[i]=arguments[i];return v}function array_get($,k){return $[k]}function array_set($,k,v){return $[k]=v}var new_function=(function(){function F(r) {return Function.apply(this,r)}F.prototype = Function.prototype;return function(p,$){p=p.split(/\s*,\s*/);p.push($);return new F(p)}})()") ;a[a.length]=i; ;function new_array(){var v=[];for(var i=0;i<arguments.length;i++)v[i]=arguments[i];return v} Local $more= _ "$.get=function(k){return $[k]};" & _ "$.has=function(n){" & _ " for (var i in this){" & _ " if (i!=n){" & _ " continue;" & _ " }" & _ " if (!this.hasOwnProperty(i)){" & _ " continue;" & _ " }" & _ " if (typeof(this[i])=='function'){" & _ " continue;" & _ " }" & _ " return true;" & _ " };" & _ " return false;" & _ "};" & _ "$.keys=function(){" & _ " var a=[];" & _ " for (var i in this){" & _ " if (this.hasOwnProperty(i)){" & _ " if (typeof(this[i])!='function'){" & _ " a[a.length]=i;" & _ " }" & _ " }" & _ " };" & _ " return a;" & _ "};" & _ "$.set2=function(){" & _ " for(var x=0;x<arguments.length-1;x+=2){" & _ " this[arguments[x]]=arguments[x+1]" & _ " }" & _ "};" & _ "Array.prototype.item=function(n){" & _ " return this[n];" & _ "};" & _ "Array.prototype.has=function(v){" & _ " for(x=0;x<this.length;x++){" & _ " if(this[x]==v){return true;}" & _ " }" & _ " return false;" & _ "};" & _ "Array.prototype.asString=function(d){" & _ " var s='';" & _ " for(x=0;x<this.length;x++){" & _ " if(s==''){s=this[x];}else{s+=d+this[x];}" & _ " }" & _ " return s;" & _ "};" $_Script.AddCode("var $=this,arrays=[],vb;function set_vbscript($) {vb=$;vb.Language='VBScript';vb.AllowUI=true;}function alert(s){if(vb)vb.Eval('MsgBox(Unescape(""'+s+'""), 0, ""Autoit"")')}function get_proxy(){return $}function new_object($){$=new Function('return{'+$+'}')();$.set=function(k, v){$[k]=v};"&$more&"$.unset=function(k){delete $[k]};$.set_object=function(k,$){this.set(k,new_object($))};$.set_array=function(){var v=[];for(var i=0;i<arguments.length;i++)v[i]=arguments[i];$.set(v.shift(),new_array.apply(this,v))};$.set_function=function(k,p,$){this.set(k,new_function(p,$))};return $}function new_array(){var v=[];for(var i=0;i<arguments.length;i++)v[i]=arguments[i];return v}function array_get($,k){return $[k]}function array_set($,k,v){return $[k]=v}var new_function=(function(){function F(r) {return Function.apply(this,r)}F.prototype = Function.prototype;return function(p,$){p=p.split(/\s*,\s*/);p.push($);return new F(p)}})()") If $VBScriptSupport = true Then $_Script.Run("set_vbscript", ObjCreate("ScriptControl")) Return $_Script EndFunc ; ============================================================================= Func NewComObj() Local $com While True $com=$_com.new_object("") Sleep(30) If IsObj($com) Then ExitLoop EndIf WEnd Sleep(30) Return $com EndFunc Func JScriptInit() $_ComObj_proxy = _ComObj_init(true) $_com = $_ComObj_proxy.Run("get_proxy") $_Js = $_com.new_object("") $_Js.set_function("AIOSetProperty","o,n,v","o[n]=v;") EndFunc Func JS_SetProp() $_Js = $_com.new_object("") $_Js.set_function("AIOSetProperty","o,n,v","o[n]=v;") Return $_Js; EndFunc My main questions are... Why doesn't this already exist, and where is it? and... Why doesn't this already exist -- what wall am I headed for?
-
I want to assign to an AutoItObject property without hardcoding the property name. In Javascript it would look like: obj['propertyName']=103; I can read access a property using Execute: Global $ppt_val=Execute('$obj.propertyName') But I cannot assign with: Assign('obj.propertyName',103,4); doesnt work, nor... Assign('$obj.propertyName',103,4) Maybe AutoItObjects have a sooper-secret $obj.__set($name,$val) function? I am looking for a workaround no matter how convoluted. Was thinking about maybe this might be feasible with a JScript COM helper, since I have built a AutoItObject ClassFactory anyhow. I would be able to create dynamic helper functions in JScript COM, that would stop me from writing: Func Set__WinStyle($me,$name,$bool) If False Then ElseIf $name=='Border' Then $me.Border=$bool ElseIf $name=='Popup' Then $me.Popup=$bool ElseIf $name=='Caption' Then $me.Caption=$bool ;etc,etc,etc JScript COM: '?do=embed' frameborder='0' data-embedContent>>
- 2 replies
-
- AutoItObject
- JScript
-
(and 1 more)
Tagged with: