Sg2.9
Working with the Console
Index RSS

When working with scripts and programming commands, there are unfortunately quite a few things that may go wrong, especially in the beginning when still learning how to program. But even the most experienced programmer might misspell a command or not consider a special case in the script, leading to unexpected results.

Programming languages are very specific about how programs are written, how commands are named and executed, values assigned, etc. If these rules are not followed, the program cannot be interpreted, causing the famous Syntax Error. There are also many things that Illustrator is specific about, for example one cannot create a path item if there is no open document to create it in.

The Scriptographer Console is the place where all such feedback of the system goes to. If an error occurs anywhere in a script, it is printed to the console, indicating the name of the script and mostly also the location of the error within the script.

// The bellow text is not proper JavaScript code and therefore
// will produce an error when executed.
Hey Illustrator, could you please create a path item
in the current document for me?
 

This message tells us to search for a missing ';' on line 3 of the script My Scripts/Test.js. Such error messages can often be a bit confusing, as Scriptographer is expecting proper JavaScript, not human English like in this example, but the line number mostly provides enough information to know where to look for the error.

Executing Code in the Console

As shown in the above image, there are two text fields in the console: The input field at the top and the output field at the bottom. The input field is where you can input short commands of code to quickly try them out without the usual steps of creating a new script, selecting it and then hitting the play button.

Entering a sequence of code and then hitting enter will automatically evaluate the code and print any results to the output field:

Any of the Scriptographer commands and objets can be used in the console. The code entered can therefore directly interact with the active document and selected items. For example, we can tell Scriptographer to scale the first selected item of the document by a factor of 2 (200%):

Printing to the Console

Scripts can also write to the console directly by using the global console.log() function. This is a very useful during the process of developing new scripts and in the quest for errors. If a script is not producing the expected result, putting console.log() commands in multiple places of the script is often the easiest and most pragmatic way to find issues and fix them. They allow us to watch the execution sequence of a script, print calculated values and ensure they are indeed what we expect them to be.

console.log() can handle a variable amount of arguments, which are then printed to the console in one line, separated by one white space each:

console.log('Hello', 'World');

Values other than strings can be passed to console.log() and are converted to strings on the fly:

for (var i = 1; i <= 10; i++) {
	console.log('We are looping', i, 'times');
}