Although Xill IDE is a very complete tool, sometimes you might need to use a tool that is not available in a Xill package. For these cases, you can use System.exec() to execute commands on the command line. In this function you can chain commands. This function runs an application an returns the output.The result will contain a list of errors, a list of output and the runtime in milliseconds. All errors will also be logged in the console while running.


For one of our recent projects (a Web migration from Tridion to Adobe Experience Manager), we had to convert the extracted html from Tridion to predefined html components in AEM. To clean up the html, we like to use the Tidy tool. This is possible with only a small piece of code. This code writes the given html to a temporary file before Tidy picks it up. I have downloaded the tool and put in in the C:/cltools directory. I have created some badly written html to test the robot. 

So let's start to recreate the robot I used. First, I created a variable that holds the badly formatted html. 

var bad_code = "
<p>lasjdflkjsadflkf<span>this was the span</p></span>
<li>listitem 1
<li>listitem 2

Next, we will put the location of the tidy tool into a variable. Adjust the location to where you have saved the tool. Please keep in mind that characters need to be escaped. While we are at it, let's create a variable that will hold the location of where the temporary files will be saved.  

Now we will create a function to do the actual execution of the tidy tool. The function needs one parameter, which holds the 'dirty' html. The first step the function executes is the saving of the dirty html to a temporary file. 

function tidy(text) { :: "htmltidy-in.html", text);

As you can see, we used the function to save the content to the htmltidy-in.html file. Because we used this function (which comes from the File package, we need to import the File package to the robot. So at the top of the robot, add the following code.

use File;

If you call the function with the bad_code variable, than the text inside the bad_code variable is saved to the location C:\tmp\htmltidy-in.html. Try it!

Now comes the exciting piece of code. The actual execution of the tidy tool. We will use the System.exec() function from the System package. So first, at the top, add the following code. 

use System;

Inside the tidy function, add the following code. 

 var result = System.exec([
"-f", "htmltidy-errors.txt", 
"-o", "htmltidy-out.html", 

This is a confusing piece of code, so I will explain it step-by-step. The function expects two parameters, an array of commands and an optional parameter of the working directory of where you would like the commands to be executed. To be able to use the tidy tool, we provide the variable 'tidylocation'. Next we want to provide a location where tidy will write the errors to. To do this, we will provide the System.exec function with the parameter that tidy uses ("-f") and the name of the file where the errors are written to ("htmltidy-errors.txt"). 

Now we will provide the System.exec function with an output file where the result of the tidy tool will be saved to. To do this, we provide the System.exec function with the parameter that tidy used ("-o") and the location of the output file ("htmtidy-out.html"). 

Finally we will provide the System.exec function with the input file, which holds the badly formatted html code and was created inside the tidy() function we are writing.  

In this case we use the optional parameter. Because we are saving the temporary files to the directory provided in the 'temp_path' variable, we want the tidy tool to look there for the input file and write the possible errors and the output file to. 

We want the function to return the nicely formatted code that the tidy tools returns. To do that, we will read the outputted file form the hard drive and return that value with the following piece of code: 

var ret = File.getText(temp_path :: "htmltidy-out.html");

If you would execute the tool via the command line, the calling of the tool would look like this: 

C:\cltools\tidy.exe -f C:\tmp\htmltidy-errors.txt -o C:\tmp\htmltidy-out.html C:\tmp\htmltidy-in.html

Now, let's call the function we created to fetch the nicely formatted text. Do that with the following code: 

To compare the input and the output of the function, let's print the bad_code and good_code variables to the console: 

Now the functionality is complete. We used the System.exec function to execute an external tool from within Xill IDE. The complete code we created will be attached to this article, named 'systemexec.xill'. Click the link to download it. For your convenience, I also attached the tidy.exe tool.