Scripting

Skala documents are plain XML files, with an open specification, which allows fast and direct manipulation of documents and projects. Automation and scripting can be done without Skala running, or even without Skala installed. Working this way can also process many documents at once.

If you’re handy with Terminal, Grep and Perl can be used to count things, batch rename layers, and replace colors. With a little more code, tools could be built for linting, generating grids of shapes, and so much more.

Skala will also live reload edited documents, provided no changes have been made inside Skala since the document was last saved or opened. When a document is reloaded, Skala shows a “Document updated” message.

The examples below only work with uncompressed Skala documents. Documents saved with the Compression option set to Xz (Smaller Files) use the .skalaxz extension, and can’t be read by text tools. To convert one, open it in Skala, choose File › Save As, and set Compression to None (Best for Git).

Further reading: Version control

How documents are structured #

Every layer in a Skala document is a <layer> element, with a <name>, and a <content> element that says what kind of layer it is. Here’s a rectangle, and a component that uses component.skala.

<layer id="881407B4D3E749EF">
   <name>Background</name>
   <content xsi:type="Data.PageRectangleShape"/>
</layer>
<layer id="353CE272FAB5DB3D">
   <name>Button</name>
   <content xsi:type="Data.PageSkalaInclude">
      <path>component.skala</path>
   </content>
</layer>

Constants are stored in a <constantList> near the top of the document. Each constant has a <name>, and its value is stored in <value> (number and boolean constants), <scale> (unit constants), <string> (string constants), <vector> (vector constants), or <color> (color constants).

Counting things #

The number of rectangles in a document can be counted using the Grep Terminal command below.

grep -c 'xsi:type="Data.PageRectangleShape"' document.skala

Or, the number of component instances in a document.

grep -c 'xsi:type="Data.PageSkalaInclude"' document.skala

The path to a component is on the line after the component’s content type, so the number of instances for a specific component can be counted by chaining two Grep commands together. The example below counts how many times component.skala is used in document.skala.

grep -A1 'xsi:type="Data.PageSkalaInclude"' document.skala | grep -c 'component.skala</path>'

If you want to check an entire folder of Skala documents to see where a component has been used, that’s also possible. The example below shows where component.skala is used in all the Skala documents in folder.

grep -A1 'xsi:type="Data.PageSkalaInclude"' folder/*.skala | grep 'component.skala</path>'

Component paths are stored relative to the document, and spaces in file names are written as %20.

Batch renaming layers #

With great power comes great responsibility. Please, please only perform destructive actions on copies of your Skala documents. We’re not responsible if you break stuff. If you promise to only work on copies of your documents, you can read on.

The Perl command below replaces layers named Old Name with New Name in document.skala.

perl -i -0pe 's/(<layer id="[^"]*">\s*<name>)Old Name(<\/name>)/$1New Name$2/g' document.skala

If you’d rather not use Terminal, Skala’s refactor sheet can batch rename layers, materials, pages, constants, and more, with a preview of every change.

Further reading: Refactor and batch rename

Batch replacing color constants #

The Perl command below changes the color constant named blue to the color 0 0 1 1 in document.skala. The four numbers are red, green, blue, and alpha, in the color profile stored with the constant.

perl -i -0pe 's/(<name>blue<\/name>\s*<color>.*?<components>)[^<]*(<\/components>)/${1}0 0 1 1$2/s' document.skala

Doing that isn’t too handy, because it’s just changing a single color constant, which you could do quickly from Skala’s interface. But, what if you wanted to change an entire folder full of documents, updating all their color constants?

perl -i -0pe 's/(<name>blue<\/name>\s*<color>.*?<components>)[^<]*(<\/components>)/${1}0 0 1 1$2/s' folder/*.skala

Once again, we urge you to take extreme caution when batch editing documents, and only work with copies. We also recommend using version control for your Skala projects.

Batch replacing number constants #

On the same tip, what if you wanted to change a number constant across lots of documents? You could use something similar to the command below, which changes the number constant called a to 8 for all the Skala documents in a folder.

perl -i -0pe 's/(<name>a<\/name>\s*<value>)[^<]*(<\/value>)/${1}8$2/' folder/*.skala

Batch replacing string constants #

The command below changes the string constant called buttonText to Hello for all the Skala documents in a folder. Maybe this could help you switch localisations for your designs? È una buona idea, amico mio.

perl -i -0pe 's/(<name>buttonText<\/name>\s*<string>)[^<]*(<\/string>)/${1}Hello$2/' folder/*.skala

Automated builds #

Once your documents have been updated, SkalaCLI can build them without opening Skala, which is handy for scripts and continuous integration.

Further reading: Command line

What’s missing? #

Skala already does a lot, and things like shearing do not require a plugin, but maybe we’re missing a feature you’d like? Maybe the feature can not be done via the methods mentioned above? Please get in contact if there’s something new you’d like Skala to do.