User Tools

Site Tools


Sub Script

Summary

In build 2.2.2.17, Daz Studio was given the ability to pass an Array of arguments into a DzScript object. This allowed a script to expose remote operation of itself to other scripts, or even plugins, while also allowing the source of that script to remain protected. The type of objects that can be passed in via the arguments Array are limited to basic types; String, Number, Boolean, Array, Object, etc. Below you will find sample code for two scripts; one for a script that remotely executes another script, and one for the script being called.

API Areas of Interest

Example (Caller)

Caller_Script.dsa
// Define an anonymous function;
// serves as our main loop,
// limits the scope of variables
(function(){
 
	/*********************************************************************/
	// String : A function for retrieving a translation if one exists
	function text( sText )
	{
		// If the version of the application supports qsTr()
		if( typeof( qsTr ) != "undefined" ){
			// Return the translated (if any) text
			return qsTr( sText );
		}
 
		// Return the original text
		return sText;
	};
 
	/*********************************************************************/
	// Declare working variables
	var sTitle, sMessage;
 
	// Define common strings
	var sButton = text("&OK");
 
	// Define the base path of the script we'll call; without the file extension
	var sBasePath = String( "%1/support/MyName/MyScriptName" ).arg( App.getScriptsPath() );
 
	// Create a script object
	var oScript = new DzScript();
 
	// Attempt to find our script; doing it this way, we can debug with an
	// ascii file and distribute a binary [encrypted] file with the same name...
	// without having to update the contents of the script or manually handle
	// the file extensions; requires 3.0.1.5 or newer
	var sScriptPath = oScript.getScriptFile( sBasePath );
 
	// If a script is found
	if( !sScriptPath.isEmpty() ){
		// If the script loads
		if( oScript.loadFromFile( sScriptPath ) ){
			// Execute the script; pass in an array of arguments;
			// passing in arguments requires 2.2.2.17 or newer
			oScript.execute( [ App.version, sScriptPath, true ] );
		// If the script doesn't load
		} else {
			// Define text variables for the message
			sTitle = text( "Read Error" );
			sMessage = text( "The '%1' file could not be loaded." ).arg( sScriptPath );
			// Inform the user
			MessageBox.information( sMessage, sTitle, sButton );
		}
	// If a script is not found
	} else {
		// Define text variables for the message
		sTitle = text( "File Not Found" );
		sMessage = text( "A '%1.ds(a|b|e)' file could not be found." ).arg( sBasePath );
		// Inform the user
		MessageBox.information( sMessage, sTitle, sButton );
	}
 
// Finalize the function and invoke
})();

Example (Callee)

Callee_Script.dsa
// Define an anonymous function;
// serves as our main loop,
// limits the scope of variables
(function( aArgs ){
 
	/*********************************************************************/
	// String : A function for retrieving a translation if one exists
	function text( sText )
	{
		// If the version of the application supports qsTr()
		if( typeof( qsTr ) != "undefined" ){
			// Return the translated (if any) text
			return qsTr( sText );
		}
 
		// Return the original text
		return sText;
	};
 
	/*********************************************************************/
	// Declare working variables
	var vArg;
	var sType;
 
	// Initialize
	var aLines = new Array( aArgs.length );	
 
	// Iterate over the arguments passed to the script
	for( var i = 0, nArgs = aArgs.length; i < nArgs; i += 1 ){
		// Get the 'current' argument
		vArg = aArgs[ i ];
 
		// Get the 'current' type
		sType = typeof( vArg );
 
		// Stringify based on the type of the argument
		switch( sType ){
			case "object":
				aLines[ i ] = String("%1 : %2")
						.arg( sType )
						.arg( JSON.stringify( vArg ) );
				break;
			default:
				aLines[ i ] = String("%1 : %2")
						.arg( sType )
						.arg( vArg );
				break;
		}
	}
 
	// Define text variables for the message
	var sButton = text( "&OK" );
	var sTitle = text( "Arguments" );
	var sMessage = aLines.join( "\n" );
 
	// Display the message
	MessageBox.information( sMessage, sTitle, sButton );
 
// Finalize the function and invoke
})( getArguments() );