<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="_c74_vig.xsl" type="text/xsl"?>
<vignette name="The LiveAPI Object" package="Max for Live">
	<h1>The LiveAPI Object</h1>
	<p>
		The LiveAPI object provides a means of communicating with the Live API functions from JavaScript. For background information on this functionality, please see the <link module="core" name="live_api_overview" type="vignette">Live API Overview</link> and <link module="core" name="live_object_model" type="vignette">Live Object Model</link> documents, as well as the Reference pages for <o>live.path</o>, <o>live.object</o> and <o>live.observer</o> objects, which provide the same basic functionality as the LiveAPI object, but from the Max patcher.
	</p>
	<h2>LiveAPI Constructor</h2>
	<code language="javascript">api = new LiveAPI([callback], [path]/[id]);</code>
	<p>
		<m>callback</m> is an optional JavaScript function. This function will be called when the LiveAPI object refers to a new object in Live (if the LiveAPI object's path change, for instance),  or when an observed property changes. <m>path</m> refers to the object in Live "pointed to" by the LiveAPI object (e.g. "live_set tracks 0 devices 0"). Alternately, a valid <m>id</m> can be used to refer a LiveAPI object to an object in Live.
	</p>
	<bluebox>
		<p>
			Technical note: you cannot use the LiveAPI object in JavaScript global code. Use the <o>live.thisdevice</o> object to determine when your Max Device has completely loaded (the object sends a <m>bang</m> from its left outlet when the Device is fully initialized, including the Live API).
		</p>
	</bluebox>
	<br/>
	<bluebox>
		<p>
			Legacy note: previous versions of the LiveAPI object required the jsthis object's this.patcher property as the first argument. For backward-compatibility, this first argument is still supported, but is no longer necessary.
		</p>
	</bluebox>
	<br/>
	<jsproperty_list name="LiveAPI">
		<jsproperty name="id" get="1" set="1" type="Number">
			<description>
				The id of the Live object referred to by the LiveAPI object. These ids are dynamic and awarded in realtime from the Live application, so should not be stored and used over multiple runs of Max for Live.
        	</description>
    	</jsproperty>
		<jsproperty name="path" get="1" set="1" type="String">
			<description>
				The path to the Live object referred to by the LiveAPI object. These paths are dependent on the currently open Set in Live, but are otherwise stable: <i>live_set tracks 0 devices 0</i> will always refer to the first device of the first track of the open Live Set.
        	</description>
    	</jsproperty>
		<jsproperty name="unquotedpath" get="1" set="1" type="String">
			<description>
				The path to the Live object referred to by the LiveAPI object, without any quoting (the <m>path</m> property contains a quoted path). These paths are dependent on the currently open Set in Live, but are otherwise stable: <i>live_set tracks 0 devices 0</i> will always refer to the first device of the first track of the open Live Set.
        	</description>
    	</jsproperty>
		<jsproperty name="children" get="get" type="Array">
			<description>
				An array of children of the object at the current path.
        	</description>
    	</jsproperty>
		<jsproperty name="mode" get="1" set="1" type="Number">
			<description>
				The follow mode of the LiveAPI object. 0 (default) means that LiveAPI follows the object referred to by the <m>path</m>, even if it is moved in the Live user interface. For instance, consider a Live Set with two tracks, "Track 1" and "Track 2", left and right respectively. If your LiveAPI object's path is <i>live_set tracks 0</i>, the left-most track, it will refer to "Track 1". Should the position of "Track 1" change, such that it is now to the right of "Track 2", the LiveAPI object continues to refer to "Track 1". A <m>mode</m> of 1 means that LiveAPI updates the followed object based on its location in the Live user interface. In the above example, the LiveAPI object would always refer to the left-most track, updating its <m>id</m> when the object at that position in the user interface changes.
        	</description>
    	</jsproperty>
		<jsproperty name="type" get="1" set="0" type="String">
			<description>
				The type of the object at the current path. Please see the <link module="core" name="live_api_overview" type="vignette">Live API Overview</link> and <link module="core" name="live_object_model" type="vignette">Live Object Model</link> documents for more information.
        	</description>
    	</jsproperty>
		<jsproperty name="info" get="1" set="0" type="String">
			<description>
				A description of the object at the current path, including id, type, children, properties and functions.
        	</description>
    	</jsproperty>

		<jsproperty name="property" get="1" set="1" type="String">
			<description>
				The observed property, child or child-list of the object at the current path, if desired. For instance, if the LiveAPI object refers to "live_set tracks 1", setting the <m>property</m> to "mute" would cause changes to the "mute" property of the 2nd track to be reported to the callback function defined in the LiveAPI Constructor.
        	</description>
    	</jsproperty>
		<jsproperty name="proptype" get="1" set="0" type="String">
			<description>
				The type of the currently observed property or child. The types of the properties and children are given in the <link module="core" name="live_object_model" type="vignette">Live Object Model</link>.
        	</description>
    	</jsproperty>
		<jsproperty name="patcher" get="1" set="0" type="Object">
			<description>
				The patcher of the LiveAPI object, as passed into the Constructor.
        	</description>
    	</jsproperty>
    </jsproperty_list>

	<jsmethod_list name="LiveAPI">
  		<jsmethod name="getcount">
		    <arglist>
		    	<arg name="child" type="string"/>
		    </arglist>
		    <description>
		    	The count of children of the object at the current path, as specified by the <m>child</m> argument.
		    </description>
  		</jsmethod>

  		<jsmethod name="goto">
		    <arglist>
		    	<arg name="path" type="string"/>
		    </arglist>
		    <description>
		    	Navigates to the <m>path</m> and causes the id of the object at that path out be sent to the callback function defined in the Constructor. If there is no object at the path, id 0 is sent.
		    </description>
  		</jsmethod>

  		<jsmethod name="get">
		    <arglist>
		    	<arg name="property" type="string"/>
		    </arglist>
		    <description>
		    	Returns the value or list of values of the specified <m>property</m> of the current object.
		    </description>
  		</jsmethod>

  		<jsmethod name="getstring">
		    <arglist>
		    	<arg name="property" type="string"/>
		    </arglist>
		    <description>
		    	  Returns the value or list of values of the specified <m>property</m> of the current object as a String object.
		    </description>
  		</jsmethod>

  		<jsmethod name="set">
		    <arglist>
		    	<arg name="property" type="string" />
		    	<arg name="value" type="anything"/>
		    </arglist>
		    <description>
		    	Sets the value or list of values of the specified <m>property</m> of the current object.
		    </description>
  		</jsmethod>

  		<jsmethod name="call">
		    <arglist>
		    	<arg name="function" type="string" />
		    	<arg name="arguments" type="anything"/>
		    </arglist>
		    <description>
		    	Calls the given <m>function</m> of the current object, optionally with a list of <m>arguments</m>.
		    </description>
  		</jsmethod>
	</jsmethod_list>

	<h2>Sample Code</h2>
	<code language="javascript">
		var api = new LiveAPI(sample_callback, "live_set tracks 0");
		if (!api) {
		post("no api object\n");
		return;
		}
		post("api.mode", api.mode ? "follows path" : "follows object", "\n");
		post("api.id is", api.id, "\n");
		post("api.path is", api.path, "\n");
		post("api.children are", api.children, "\n");
		post("api.getcount(\"devices\")", api.getcount("devices"), "\n");

		api.property = "mute";
		post("api.property is", api.property, "\n");
		post("type of", api.property, "is", api.proptype, "\n");

		function sample_callback(args)
		{
		post("callback called with arguments:", args, "\n");
		}
	</code>

	<h2>The LiveAPI Object and the Scheduler</h2>
	<p>
		Beginning with release 6.0 of Max, it is no longer possible to configure JavaScript functions to run in the high-priority thread of Max's scheduler. The LiveAPI object <i>cannot be created or used</i> in the high-priority thread, so users should be sure to use the <o>defer</o> or <o>deferlow</o> objects to re-queue messages to the <o>js</o> object.
	</p>
</vignette>
