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.