A couple of months back at the GTALUG Graphics session, someone asked if Inkscape – the 2D vector graphics workhorse that everyone seems to use – could be scripted. We pretty much said that it couldn’t. Recently, I found out that it does support a limited form of scripting, and wish to pass this on.
The key to it is understanding Inkscape’s command verbs. These can be listed using:
These verbs map to Inkscape commands, and often have names linked to the menu they live in (such as “FileQuit” doing what you’d expect).
I had a task I had to repeat on many files: convert all the stroked lines to filled paths. You’d need to do this if you are laser engraving a simple drawing, but there are other applications for this too. Here’s a command that would do this for all objects in a drawing, and overwrite the input file:
inkscape --verb EditSelectAll --verb SelectionUnGroup \ --verb EditSelectAll --verb SelectionUnGroup \ --verb EditSelectAll --verb SelectionUnGroup \ --verb EditSelectAll --verb ObjectToPath \ --verb EditSelectAll --verb SelectionCombine \ --verb EditSelectAll --verb StrokeToPath \ --verb FileSave --verb FileClose \ --verb FileQuit input.svg
What this does:
- Selects everything, and ungroups all objects (×3, to break up most nested groups);
- Selects everything, and converts all objects to paths (so text, circles, polygons, spirals become paths, the lowest-level SVG object);
- Selects everything, and combines everything into one path;
- Selects everything, and converts all strokes to filled paths (so a two node straight line 1 mm wide would become a four node filled rectangle 1 mm thick);
- Overwrite the input file, close it, and quit.
The process has some disadvantages:
- It opens a window every time. You can’t execute verbs without the GUI opening.
- You can’t have another copy of Inkscape open while you do this.
- Realistically, you can’t really do anything at your computer until this is done, as it’s popping up windows and shifting focus like crazy. (ssh types can say “heh!” in a smug manner now)
- You can’t set parameters to verbs.
- It will overwrite the input file.
- It clogs up your “File / Recent” menu with all of the files you scripted.