Thursday, January 8, 2009

Script Code: One More for the Road (Kix with Jscript)

Someone asked about the example I posted for requesting a return value from WSH/VBscript using KiXtart, but wanted to see an example of JavaScript instead.  Keep in mind that WSH provides “JScript”, not actually “ECMAScript/JavaScript”, so it’s close but not identical in every respect.   However, the only thing to be mindful of is that function/method/object calls within JScript are case sensitive.  So calling “Math.Cos(45)” is not the same as “Math.cos(45)”.  The latter is correct.  The former is erroneous, fwiw.

    Function Cosine($numValue)
        $sc = CreateObject("ScriptControl")
        $sc.Language = "jscript"
        $result = $sc.Eval("Math.cos("+$numValue+")")
        $sc = 0
        $Cosine = $result
    EndFunction

    $testvalue = 45
    $test = Cosine($testvalue)

    ? "cosine of $testvalue is: "+$test

If you run this, it will display the result:

    cosine of 45 is: 0.52532198881773

I didn't really need to define $testvalue first and then pass it into the Cosine() function that way.  I could have entered the explicit value as the argument as well, such as Cosine(45).  I hope this helps.

No comments: