<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<?xml-stylesheet href="_c74_vig.xsl" type="text/xsl"?>
<vignette name="Basic Javascript programming: Global Methods" package="Max">

<h2>Basic Javascript programming: Global Methods</h2>

<h4>The following methods are defined in the global Javascript context and can be used anywhere, including global code.</h4>
  	<jsmethod_list name="Universally Available">
  		<jsmethod name="cpost">
		    <arglist>
		    	<arg name="message" type="anything"/>
		    </arglist>
		    <description>
		      Prints a message to the system console window. See post() below for further details about arguments.
		    </description>
  		</jsmethod>
  		<jsmethod name="error">
		    <arglist>
		    	<arg name="message" type="anything"/>
		    </arglist>
		    <description>
		      Sends a red tinged message to the Max window.
		    </description>
  		</jsmethod>
			<code language="javascript">
			function bang()
			{
			error("What just happened?\n");
			}
			</code>
  		<jsmethod name="messnamed">
		    <arglist>
		    	<arg name="object name" type="symbol"/>
		    	<arg name="message name" type="string"/>
		    	<arg name="message arguments" type="string"/>
		    </arglist>
			<description>
				Sends a message to the named Max object. A named Max object is an object associated with a global symbol (not an object with a patcher-specific name). For example, Max <o>receive</o> objects are bound to global symbols. The following code would send the message bang to the named object flower.
				<p>
					<b>Example:</b>
				</p>
				<code language="javascript">
		  			messnamed("flower","bang”);
				</code>
		    </description>
  		</jsmethod>

  		<jsmethod name="post">
		    <arglist>
		    	<arg name="message" type="anything"/>
		    </arglist>
		    <description>
		    	Prints a representation of the arguments in the Max window. If post() has no arguments, it prints starting on the next line. Otherwise it prints the input on the current line separated by spaces. Arrays are unrolled to one level as with outlet.
		    	<p>
				<b>Example:</b>
				</p>
				<code language="javascript">
					a = new Array(900,1000,1100);
				  	post(1,2,3,"violet",a);
				  	post();
				  	post(4,5,6);
				</code>
				<p>
					These statements produce the following output in the Max window:
				</p>
				<p>
				1 2 3 violet 900 1000 1100
				4 5 6
				</p>
				<p>
					If you want a line break in the middle of a call to post() you can use "\n" within a string (this is now a general feature of Max). Also, post() begins a new line if the last person to write to the Max window was not the <o>js</o> object.
				</p>
		    </description>
  		</jsmethod>
	</jsmethod_list>

<h2>The jsthis Object</h2>
<p>
The <b>jsthis</b> object is the this within the context of any function you define that can be invoked
from Max as well as your global code. When you define functions, they become methods of your extension
of jsthis. When you use variables in your global code, they become its properties. The <b>jsthis</b> object
has certain built-in properties and methods that facilitate interacting with and controlling the
Max environment.
</p>
	<jsproperty_list name="jsthis">
		<jsproperty name="autowatch" get="1" set="1" type="Number">
			<description>
				Turns on the automatic file recompilation feature where a file is reloaded and recompiled if it changes. This is particularly useful during development of your Javascript code if you have several <o>js</o> instances using the same source file and you want them all to update when the source changes. It can also be used to facilitate the use of an external text editor. When the text editor saves the file, the <o>js</o> object will notice and recompile it. By default, the value of autowatch is 0 (off). If you want to turn on autowatch, it is best to do so in your global code.
        	</description>
    	</jsproperty>
    	<p>
			<b>Example:</b>
		</p>
		<code>
		  <b>box</b> (<i>Maxobj, get</i>)
		</code>
		<p>
			Returns the Maxobj containing the <o>js</o> object. This is most useful for the <o>jsui</o> object to obtain the rectangle of the object’s box. For more information on this object, see <link type="vignette" module="js" name="jsmaxobject">the Max Object</link>.
		</p>

		<jsproperty name="editfontsize" get="1" set="1" type="Number">
			<description>
				Controls the size of the font shown in the text editing window where you edit a script in points. By assigning the editfontsize property in your global code, you can override the default font size setting for text editing, which is the same size as the text shown in the Max window.
        	</description>
    	</jsproperty>

		<jsproperty name="inlet" get="1" set="1" type="Number">
			<description>
				During the execution of a function, the inlet property reports the inlet number that received the message that triggered the function, starting at 0 for the leftmost inlet. This property’s value is 0 within global code.
        	</description>
    	</jsproperty>

		<jsproperty name="inlets" get="1" set="1" type="Number">
			<description>
				Specifies how many inlets an instance should have. The inlets property must be set in the global code to have any effect. If it isn't set, an object with one inlet will be created.
			</description>
		</jsproperty>

		<jsproperty name="inspector" get="1" set="1" type="Number">
			<description>
					Specific to the <o>jsui</o> object. The inspector property, if set to 1, causes Max to look for an inspector patch specific to your script rather than the default jsui-insp.pat file. The name used will be the name of your script (without the .js extension) plus –insp.pat. For example, if your script is called foo.js, your inspector file should be named foo-insp.pat. Inspector patches can be placed anywhere in the Max search path.
      </description>
    </jsproperty>

		<jsproperty name="jsarguments" get="get" set="0" type="Array">
			<description>
				Allows access to the arguments typed into your object when it was instantiated. The filename is jsarguments[0], the first typed-in argument is jsarguments[1]. The number of arguments plus one is jsarguments.length. jsarguments[] is available in global code and any function. It never changes after an object is instantiated, unless the Max <o>js</o> object receives the jsargs message with new typed-in arguments.
				<p>
					Example:
				</p>
				<p>
				Creating an object with a variable number of outlets based on an argument typed into the <o>js</o> object:
				</p>
				<code language="javascript">
				  // set a test default value, protects against
				  // a bad arg or no args
				  outlets = 0;
				  if (jsarguments.length >= 2)
				    outlets = jsarguments[1];
				  if (!outlets)
				  outlets = 1; // default value
				</code>
        	</description>
    	</jsproperty>

		<jsproperty name="Max" get="get" type="Object">
			<description>
				Returns a Javascript representation of the "max" object (i.e., the recipient of ; max preempt 1 in a message box). Lets you send any message to the object that controls the Max application. In addition, the max object has <o>js</o>-specific properties listed in the section on <o>js</o> <link type="vignette" module="js" name="jsmaxobject" anchor="maxobjectproperties">Max Object Properties</link>.
				<p>
					Here is an example of sending the max object the preempt message to turn Overdrive on:
				</p>
				<code language="javascript">
				  max.preempt(1);
				</code>
        	</description>

    	</jsproperty>
		<jsproperty name="maxclass" get="get" type="String">
			<description>
				Returns "js" (the standard Javascript class property returns "jsthis”)
        	</description>
    	</jsproperty>

		<jsproperty name="messagename" get="get" type="String">
			<description>
				The name of the message to the <o>js</o> object that invoked the method currently running. In global code, this is a nil value. This is generally useful only from within an anything() function that will be called when no specific function name matches the message sent to the <o>js</o> object. Here is an example of an anything() function that adds a property to a variable declared in global code. Note the use of the tricky Javascript bracket notation to specify a variable property.

				<p>
					Example:
				</p>
				<code language="javascript">
				  var stuff;
				  function anything(val)
				  {
				    if (arguments.length) // were there any arguments?
				      stuff[messagename] = val;
				  }
				</code>
        	</description>
    	</jsproperty>

		<jsproperty name="patcher" get="get" type="Object">
			<description>
				Access to the patcher containing the <o>js</o> object. See <link type="vignette" module="js" name="jspatcherobject">the Patcher Object</link> for more information on this object.
        	</description>
    	</jsproperty>

		<jsproperty name="outlets" get="get/set" set="1" type="Number">
			<description>
				The number of inlets an object should have. The outlets property must be set in the global code to have any effect. If it isn’t set, and object with one outlet will be created.
        	</description>
    	</jsproperty>
    </jsproperty_list>

<jsmethod_list name="jsthis">
  		<jsmethod name="arrayfromargs">
		    <arglist>
		    	<arg name="message" type="string" />
		    	<arg name="arguments" type="list"/>
		    </arglist>
		    <description>
		    	A utility for writing functions that take a variable number of arguments, and/or those that can be called using various messages (such as an anything function). The Function object has an arguments property that can be numerically indexed like an Array but is not an instance of Array. This means that you cannot call Array functions such as sort() on the arguments property, or send the arguments property out an outlet as a list of values. The arrayfromargs() method will convert the arguments property to an Array, optionally with message as the zeroth element of the array. This message usage is useful for processing messages as though they are lists beginning with a symbol, as would be typical in your anything function. Here is an example of a function that allows its arguments to be sorted. Note that messagename is a property of the <b>jsthis</b> object that returns the name of the message that invoked the function.

		    <p>
				Example:
			</p>
			<code language="javascript">
			  function anything()
			  {
			    var a = arrayfromargs(messagename,arguments);

			    a.sort();
			    outlet(0,a);
			  }
			</code>
		    </description>
  		</jsmethod>

  		<jsmethod name="assist">
		    <arglist>
		    	<arg name="arguments" type="anything"/>
		    </arglist>
		    <description>
		    	Sets the patcher assist string for a designated inlet or outlet of a <o>js</o> object box. Designed to be called from the assistance function specified as an argument to the setinletassist() or setoutletassist() method (see example under setoutletassist() below).
		    </description>
  		</jsmethod>

  		<jsmethod name="declareattribute">
		    <arglist>
		    	<arg name="attributenamex" type="string"/>
		    	<arg name="gettername" type="string" />
		    	<arg name="settername" type="string" />
		    	<arg name="embed" type="number"/>
		    </arglist>
		    <description>
		    	Declare an attribute which can be set, queried, and optionally stored in the patcher file. The attributename, argument is required, but the following arguments are optional. If no getterr or setter methods are specified, default ones will be used. These attributes can also be referenced by pattr. A few example uses are below.

		    	<p>
					Example:
				</p>
				<code language="javascript">
				  // default getter/setter
				  var foo=2;
				  declareattribute("foo"); //simple

				  // default getter/setter and embed on save
				  declareattribute("foo",null,null,1);

				  // explicit getter/setter
				  declareattribute("foo","getfoo","setfoo");

				  function setfoo(v)
				  {
				    foo = v;
				  }

				  function getfoo()
				  {
				    return foo;
				  }

				  function bang()
				  {
				    outlet(0,foo);
				  }
				</code>
		    </description>
  		</jsmethod>

  		<jsmethod name="embedmessage">
		    <arglist>
		    	<arg name="method_name" type="string"/>
		    	<arg name="arguments" type="anything"/>
		    </arglist>
		    <description>
		    	The embedmessage method works only inside of your save() function. You use it to specify the name of a function you want to be called when the <o>js</o> object containing your script is recreated. The function name must be given as a string, followed by the arguments you wish to pass to the function. These arguments will typically be numbers, arrays, or strings (Javascript objects cannot be used in this context) and will reflect the current state of your object.

		    	<p>
					You may use the embedmessage method as many times as you want to specify multiple functions you wish to invoke to restore object state. Here is an example where functions we assume you’ve defined called numchairs(), numtables(), and roomname() are used in separate embedmessage statements within a save function.
				</p>
				<code language="javascript">
				  function save()
				  {
				    embedmessage("numchairs",20);
				    embedmessage("numtables",2);
				    embedmessage("roomname","diningroom");
				  }
				</code>
				<p>
					When  the <o>js</o> object containing this script is recreated, the function numchairs will be called with an argument of 20, followed by the numtables function with an argument of 2. Finally, the roomname function will be called with an argument of the String diningroom.
				</p>
		    </description>
  		</jsmethod>

  		<jsmethod name="notifyclients">
		    <description>
		    	Notifies any clients (such as the <o>pattr</o> family of objects), that the object’s current value has changed. Clients can then take appropriate action such as sending a <o>js</o> instance the message getvalueof to invoke the getvalueof() method (if defined – see the special function names listed above for more information). The notifyclients() method is useful for objects that define setvalueof() and getvalueof() functions for <o>pattr</o> compatibility.
		    </description>
  		</jsmethod>

  		<jsmethod name="outlet">
		    <arglist>
		    	<arg name="outlet_number" type="number" />
		    	<arg name="arguments" type="anything"/>
		    </arglist>
		    <description>
		    	Sends the data after the first argument out the outlet specified by the outlet_number. 0 refers to the leftmost outlet. If the outlet_number is greater than the number of outlets, no output occurs.
				<p>
					<b>Example:</b>
				</p>
				<code language="javascript">
				  	outlet(0,"bang");  // sends a bang out the left outlet

				  	outlet(1,4,5,6); // sends a list 4 5 6 out second-from-left
				</code>
				<p>
					If the argument to outlet() is a Javascript object, it is passed as the Max message <m>jsobject &lt;jsvalue&gt;</m>
				which is the address of the object. When jsobject followed by a number is sent to a <o>js</o> object, it is parsed
				and checked to see if the number specifies the address of a valid Javascript object. If so, the word jsobject disappears
				and the function sees only the Javascript object reference.
				</p>
				<p>
					If the argument to outlet is an array, it is unrolled (to one level only) and passed as a Max message or list (depending on whether the first element of the array is a number or string).
				</p>
		    </description>
  		</jsmethod>

  		<jsmethod name="setinletassist">
		    <arglist>
		    	<arg name="inlet number" type="number" />
		    	<arg name="object" type="string"/>
		    </arglist>
		    <description>
		    	Associates either a number, string, or function with the numbered inlet (starting at 0 for the left inlet). If -1 is passed as the inlet number, the object argument is used for all inlets. In order to produce any assistance text in the patcher window the assistance function needs to call the assist() method described above. See example at setoutletassist() below. The setinletassist() and setoutletassist() functions are best called in global code but can be called at any time. You can even replace the assistance function or string dynamically.
		    </description>
  		</jsmethod>
   		<jsmethod name="setoutletassist">
		    <arglist>
		    	<arg name="inlet number" type="number" />
		    	<arg name="object" type="string"/>
		    </arglist>
		    <description>
		    	Associates either a number, string, or function with the numbered outlet (starting at 0 for the left outlet). If -1 is passed as the outlet number, the object argument is used for all outlets. In order to produce any assistance in the patcher, the assistance function needs to call the assist() method described above.

		    	<p>
					<b>Example:</b>
				</p>
				<code language="javascript">
					// assistance function
				  	function describe_it(num)
				  	{
				   	 assist("this is outlet number",num);
				  	}
				  	// global code to set it up
				  	setoutletassist(-1,describe_it);
				</code>
		    </description>
  		</jsmethod>
	</jsmethod_list>

</vignette>