Output (library)

Categories:Text

Overview

Summary

The Output library provides logging functionality, object introspection, and a variety of printing and formatting methods. You will typically use the library in development, to inspect variables, or perhaps to save data to file for reporting or logging tasks.

Contents

API

props(obj, output)

Converts a typical [object Object] string into an [object Class value="1" property="2"] string

Parameters:

  • obj Object Any object that can be converted to a String
  • output Boolean An optional Boolean idicating to output the result to the Output panel

Returns:

  • String A String representation of the object and its properties

Debug the contents of an object and output to the Output panel:

var obj = {a:null, b:1, c:'Hello'};
trace(Output.props(obj));
[object Object a=null b=1 c="Hello"]

list(arr, properties, label, output)

Lists the properties of an object in a human-readable format

Parameters:

  • arr Array An Array of objects with selected properties you wish to list
  • arr Object An object whose properties you wish to list
  • properties String An optional property to list, defaults to 'name'
  • properties Array An optional Array of properties
  • label String An optional String label, defaults to "List"
  • output Boolean An optional boolean to indicate output type: true=debug, false=return, undefined=output

Returns:

  • String A String list of the object's properties

The following example lists the contents of an arbitrary Array:

list(['Dave', 'Steve', 'John']);
List: Array (depth:1, objects:0, values:3, time:0.0 seconds)
--------------------------------------------------------------------------------
array => Array
	 0: "Dave"
	 1: "Steve"
	 2: "John"

Here's the same result with a selection:

list($selection);
List: Array (depth:1, objects:0, values:5, time:0.0 seconds)
--------------------------------------------------------------------------------
array => Array
	 0: "[object SymbolInstance]"
	 1: "[object SymbolInstance]"
	 2: "[object SymbolInstance]"
	 3: "[object SymbolInstance]"
	 4: "[object SymbolInstance]"

As you can see, that's not that helpful, so list() allows you to pass in further parameters to customise the output. The following example lists the names of the objects in the selection:

list($selection, 'name');
List: Array (depth:1, objects:0, values:5, time:0.0 seconds)
--------------------------------------------------------------------------------
array => Array
	 0: "item_01"
	 1: "item_02"
	 2: "item_03"
	 3: "item_04"
	 4: "item_05"

Alternatively, you can pass in an Array of property names, and the output is output hierarchically:

list($selection, ['name', 'x']);
List: Array (depth:2, objects:5, values:10, time:0.0 seconds)
--------------------------------------------------------------------------------
array => Array
	[0] => Array
		 0: "item_01"
		 1: 30
	[1] => Array
		 0: "item_02"
		 1: 85
	[2] => Array
		 0: "item_03"
		 1: 140
	[3] => Array
		 0: "item_04"
		 1: 195
	[4] => Array
		 0: "item_05"
		 1: 250

Note that Output.list() has also been aliased as a global function, list():

list($selection);

inspect(obj, $depth, $label, $debug, $filters, $callback)

Output object to Output panel in hierarchical format (note that $ arguments are optional, and can be passed in any order)

Parameters:

  • obj Object Any Object or value
  • $depth Number An optional max depth to recurse to (defaults to 3)
  • $label String An optional String label (defaults to "Inspect")
  • $functions Boolean An optional boolean to process functions. Defaults to false
  • $options Object An optional options object. Keys may be:
    • ignore: A String or Array of object types to ignore. Valid values are 'object', 'string', 'array', 'number', 'xml', 'object', 'boolean', 'function', 'undefined', 'null'.
    • print: A Boolean to instruct the outputter to print or not print the values to the Output panel. Defaults to true.
    • debug: A Boolean indicating to trace the output as it happens. Defaults to false.
    • private: A Boolean indicating to process or skip private (_underscored) properties. Defaults to false.
  • $callback Function An optional output function in case you want to do any custom processing of the data

Returns:

  •   String A String hierarchy of the object's properties

The $ arguments of this method are derived using parameter type-shifting, that is, each argument is a different datatype, so they can be supplied in any order. This leaves you flexible to apply some or many properties as you see fit, without having to enter default values.

The following example Inspects the current library's items, with a custom label, and limits the inspection depth to 3 levels:

Output.inspect($library.items, 'Library items', 3);
Library items: Array (depth:4, objects:39, values:120, time:0.0 seconds)
--------------------------------------------------------------------------------
array => Array
	[0] => SymbolItem
		[timeline] => Timeline
			 name: "star graphic"
			[layers] => Array
				[0] => Layer
			 frameCount: 1
			 currentFrame: 0
			 layerCount: 1
			 currentLayer: 0
			[bindings] => Array
		 symbolType: "movie clip"
		 sourceFilePath: ""
		 sourceLibraryName: undefined
		 sourceAutoUpdate: undefined
		 scalingGrid: false
		[scalingGridRect] => Object
			 left: -2147483.648
			 top: -2147483.648
			 right: -2147483.648
			 bottom: -2147483.648
		 itemType: "movie clip"
		 name: "star graphic"
		 linkageExportForAS: false
		 linkageExportForRS: false
		 linkageImportForRS: false
		 linkageExportInFirstFrame: undefined

		 // additional properties truncated

Note that Output.inspect() has also been aliased as a global function, inspect():

inspect($selection);

Note that functions are ignored by default when inspecting objects or classes, so you need to pass true if you want to see a class' methods:

inspect(new Template());
inspect(new Template(), true);
Inspect: Template (depth:4, objects:0, values:6, time:0.0 seconds)
--------------------------------------------------------------------------------
object => Template
	 uri: ""
	 input: ""
	 output: ""
	 data: null
	 options: null
	 _indent: ""
Inspect: Template (depth:4, objects:0, values:13, time:0.0 seconds)
--------------------------------------------------------------------------------
object => Template
	 uri: ""
	 input: ""
	 output: ""
	 data: null
	 options: null
	 _indent: ""
	 load: function (pathOrURI)
	 save: function (pathOrURI, overwrite)
	 set: function (data, value, append)
	 indent: function (indent)
	 render: function (_stack)
	 clone: function ()
	 toString: function ()

Finally, know that if a property is parsed that causes an error, there will be no output, as the method stores the output until the end and does one, single trace (for performance reasons).

If you get JavaScript errors (but no output) when tracing objects, it's likely that a single property is causing an error, so you should pass an options parameter with the debug property set as true, so you can see which property is causing the error:

inspect(window);
inspect(window, {debug:true});
The following JavaScript error(s) occurred:

Open a Flash document (FLA) before running this script.
Debug: Window
--------------------------------------------------------------------------------
	object => Window
		[flash] => Flash
			 appName: "Flash MX2"
			 appVersion: ""
			 systemScript: "en"
			 tools: [ SKIPPING! ]
			[drawingLayer] => drawingLayer
				 sampleProperty: "foo"
			[Math] => mathObject

			...other properties omitted...

					 MP3MediumStereo: 1
					 MP3MediumMono: 2
					 MP3BestStereo: 3
					 MP3BestMono: 4
				[hardwareAccelleration] => Object
					 none: 0
					 direct: 1
					 cpu: 2
			[utils] => Object
			[prototype] => PublishProfile
				[file] => Object
The following JavaScript error(s) occurred:

At line 225 of file "PublishProfile.jsfl":
TypeError: this.parent is not a function

In this case here, it looks as if I need to add a check in the PublishProfile class for any getters (which are parsed as properties, but they are actually functions, so execute code and can throw errors) that call this.parent(). I did that, corrected the issue, and the output completed like so:


			...other properties omitted...

					[XUL] => Class
					[Source] => Class
					[Timer] => Class
			[modules] => Object
			[ui] => Object
				[dialogs] => Array
			[events] => Events
				[handlers] => Object
					[documentPublished] => Object
					[documentSaved] => Object
					[documentNew] => Object
					[documentOpened] => Object
					[documentClosed] => Object
					[documentChanged] => Object
					[layerChanged] => Object
					[frameChanged] => Object
					[mouseMove] => Object

 (depth:4, objects:1256, values:2262, time:5.4 seconds) 

folder(value, depth, output)

View the hierarchy of a folder

Parameters:

  • value String A valid folder path
  • value Folder An existing folder object
  • depth Number An optional max depth to recurse to (defaults to 3)
  • output Boolean An optional boolean to indicate outputting or not (defaults to true)

Returns:

  • String The String hierarchy of the folder

Output.folder() is actually a wrapper around Utils.walkFolder(), with a callback that outputs the file and folder names as a textual tree structure.

The following example outputs the xJSFL user folder to the default 3 levels deep (note the // shortcut to the xJSFL root folder, as outlined in URI Juggling):

Output.folder('//user/');
/user
	/assets
		/readme.txt
	/config
		/data
			/readme.txt
		/settings
			/readme.txt
			/test.xml
			/user.xml
			/xjsfl.xml
		/templates
			/readme.txt
	/jsfl
		/bootstrap.jsfl
		/libraries
			/readme.txt
		/snippets
			/Demo
			/Development
			/Instances
			/Library
			/Stage
	/temp
		/dom.txt
		/readme.txt
	/ui
		/readme.txt

Comments are closed.