JScript (XSISDK)

JScript is a simple, free language with C++ like syntax. It is automatically installed as part of any Windows installation, but on Linux it normally only runs in the context of XSI.

Historically JScript is the Microsoft implementation of JavaScript, and is the main scripting language used inside Internet Explorer (even if the script tag says "JavaScript"). Because the languages are almost identical most documentation about JavaScript also applies to JScript.

On Windows XSI uses version 5.6 of JScript (JScript.Net is not supported). On Linux the version is slightly older, which means that a few features are not available.

Currently it is the “recommended” language for the XSI SDK, meaning that when writing new script examples a focus is being placed on JScript.

JScript can be learned easily, it is recommended that all SDK users spend a few hours mastering its syntax, even if it won’t be the primary scripting language that you use. This will make it easier to read and understand any JScript examples.

One way to learn JScript is to read the help file Script56.chm that is part of the Script Debugger download http://msdn.Microsoft.com/scripting. Other JScript information is freely available on-line.

Table of contents

JScript Advantages

  • C++ like syntax makes it easy to read for anyone familiar with C or C++, and it is a little easier to port JScript into C++. It also has syntax similarities with MelScript.
  • Includes many advanced language features like creation of Dynamic objects, Exception handling (try/catch). It can be used to write very large scripts.
  • More stable than Python
  • Built in support for accessing a COM based API like the XSI
  • Good language choice if you want to manipulate HTML and XML, e.g. Net View content. JScript and JavaScript are extremely widely used in webpages, and with the popularity of AJAX its usage for more and more sophisticated usage is growing.

JScript Disadvantages

  • Some people consider the syntax (with all the funny characters like “{ [ ; ++”) hard to learn for novice programmers
  • Garbage collection is a little too lazy, some times objects remain in memory long after expected.
  • Doesn’t support output arguments in XSI Commands (Only vbscript does and those commands are being phased out)
  • JScript Array is not the standard COM SAFEARRAY and is only 1-dimensional, so it is necessary to sometimes use the VBArray() object.
  • Doesn’t have the large free libraries available to Python
  • Language is not being developed or improved by Microsoft (JScript.Net is the successor of JScript but it is not suitable for usage with XSI)

Tips and “Gotchas”

Arrays

Arrays in JScript are so important that they have their own page: Arrays in JScript (XSISDK)



Globals

Variables do not need to be defined in JScript. Any new variable name because global as soon as it is defined, unless the “var” statement is used. This can lead to unexpected behaviour:

 for ( i = 0 ; i < 5 ; i++ ) {
    DoSomething() ;
 }

 function DoSomething() {
   for ( i = 0 ; i < 100 ; i++ ) {
       …
   }
 }

In this example DoSomething() will only be called once rather than 5 times, because they both share the same “i” variable. The fix is to use “var”

 for ( var i = 0 ; i < 5 ; i++ ) {
    DoSomething() ;
 }

 function DoSomething() {
   for ( var i = 0 ; i < 100 ; i++ ) {
       …
   }
 }

So using “var” is a very good habit to get into.

Random Numbers

JScript does not offer ability to set the seed for its random number generation.

However the hr$mersennetwister.js class found at http://www.henk-reints.nl/jscript/ provides a suitable alternative Random number generator.


WScript.Shell

This object is not implemented by Softimage and it is documented inside Script56.chm. It is available on any Windows machine and can be used to read values from the registry, find out the current directory and some other useful method.

File System Access

General File System Access information can be found at File System Access (XSISDK), and this section contains JScript-specific additional comments.

Windows Path Separator Character

A common mistake is the put "C:\temp" in a script instead of "C:\\temp". In the first case JScript interprets the string as having a tab character, e.g. "C: emp"!

Determine File Extension

// Give a file determine the file extension
// Leading "." character is included in result
// returns empty string if no extension
function GetExt( in_file )
{
	pos = in_file.search( /(\..*)$/ )
	if ( pos == -1 )
		return "" ;
		
	return RegExp.$1 ;
}

// Test code:
Logmessage( GetExt( "foo" ) ); 
Logmessage( GetExt( "c:\\temp\\foo.txt" ) ); 
Logmessage( GetExt( "/var/usr/foo.txt.bak" ) ); 
//Expect:
//INFO : 
//INFO : .txt
//INFO : .txt.bak

JScript On Linux

JScript on Linux is currently at JScript version 5.0, which means it is missing a few features added in the version 5.5 and later.

All Windows machines suitable for running XSI should have 5.6 installed by default.

According to the Script56.chm file, 5.0 is missing these methods:

  • Array.push
  • Array.pop
  • Array.unshift
  • Array.splice
  • Array.shift
  • String.CharCodeAt
  • RegExp.global
  • RegExp.ignoreCase
  • RegExp.rightContext (and .leftContext)
  • RegExp.multiline
  • RegExp.lastMatch
  • RegExp.lastParen
  • Number.toPrecision
  • Number.toFixed
  • Number.toExponential
  • Object.hasOwnProperty
  • Object.isPrototypeOf
  • Object.propertyIsEnumerable
  • lastMatch
  • undefined Property
  • Date.toTimeString (also toDateString, toLocaleTimeString and toLocaleDateString)
  • String.toLocaleUppercase (and toLocaleLowercase)
  • String.localCompare
  • Error.name
  • Error.message
  • Arguments.length
  • Function.Apply method
  • Function.Call
  • Function.Callee
  • Global.DecodeURI
  • Global.DecodeURIComponent
  • Global.EncodeURI
  • Global.EncodeURIComponent

Most of these methods can be easily avoided, for example the array operations can be implemented via the existing methods.

The Regular Expression support is more primitive in 5.0, not only are some methods missing from RegExp but some regular expressions that work on Windows may appear as a syntax error on Linux. Simple Regular expressions will work.

Array.Push

If you want to use Array.Push on a script that will run on linux just add this code.

// Add a push method to the JScript Array Object
// @cc_on
// @if (@_jscript_version < 5.5)
var push = function(){
	for( var i = 0; arguments[ i ] != null; i++ )
		this[this.length++] = arguments[ i ];
	return( this );
	}
Array.prototype.push = push;
// @end


This page was last modified 18:40, 30 Aug 2007.
This page has been accessed 31814 times.

© Copyright 2009 Autodesk Inc. All Rights Reserved. Privacy Policy | Legal Notices and Trademarks | Report Piracy