Skip to content

CLI: Selection not passed to extensions when run in headless mode

Summary:

If I run the CLI version of inkscape, and want to run an extension that performs an action on the currently selected objects, the non-GUI variant of the CLI version fails. Passing along --batch-mode works as intended, but this opens a GUI window as the action is performed. Other actions that work on selections (such as selection-top, delete-selection etc.) clearly work in CLI-only mode, only when an extension is used, no IDs of selected objects are passed along

Steps to reproduce:

consider this CLI, together with the pathops extension

does not work:

inkscape --actions=export-area-page;select-by-id:rect14;selection-top;select-all;de.vektorrascheln.path.intersect.noprefs;export-plain-svg;export-filename:test-exp.svg;export-do; input.svg

does work: inkscape --batch-process --actions=export-area-page;select-by-id:rect14;selection-top;select-all;de.vektorrascheln.path.intersect.noprefs;export-plain-svg;export-filename:test-exp.svg;export-do; input.svg

What happened?

The resulting image should be an intersection of all available objects with rect14, as performed by de.vektorrascheln.path.intersect.noprefs (from the pathops extension). But when the variant without --batch-process is called, no operation is taken. Debugging into the extension python script, I can see that the first case, the python script is called as follows:

['pathops.py', '--mode=inter', '--max_ops=500', '--recursive_sel=true', '--keep_top=true', '--dry_run=false', 'C:\Users\ar\AppData\Local\Temp\ink_ext_XXXXXX.svgR9CM82']

but when it is called with --batch-process, the python code is called as follows:

['pathops.py', '--id=rect9', '--id=rect8', '--id=rect10', '--id=rect12', '--id=rect13', '--id=rect11', '--id=rect1', '--id=rect2', '--id=rect3', '--id=rect4', '--id=rect5', '--id=path1', '--id=path2', '--id=path3', '--id=path4', '--id=path5', '--id=path6', '--id=ellipse1', '--id=rect6', '--id=rect7', '--id=rect14', '--mode=inter', '--max_ops=500', '--recursive_sel=true', '--keep_top=true', '--dry_run=false', 'C:\Users\ar\AppData\Local\Temp\ink_ext_XXXXXX.svg634M82']

I think that all can be fixed with 2 lines of code in inkscape/src/extension/implementation/script.cpp

change

/**
 * Pure document version for calling an extension from the command line
 */
void Script::effect(Inkscape::Extension::Effect *mod, SPDocument *document)
{
    std::list<std::string> params;
    _change_extension(mod, document, params, mod->ignore_stderr);
}

to

/**
 * Pure document version for calling an extension from the command line
 */
void Script::effect(Inkscape::Extension::Effect *mod, SPDocument *document)
{
    std::list<std::string> params;
    Inkscape::Selection * selection = document->getSelection();
    if (selection) {
        params = selection->params;
        selection->clear();
    }
    _change_extension(mod, document, params, mod->ignore_stderr);
}

Sorry for the non-patch format, I’m fairly new to this, and could not verify this yet, but am waiting if someone can confirm this.

Version info

Inkscape 1.4.2 Calling extensions without GUI in CLI mode started only working with 1.4, previously it broke, but was fixed by the fix for inkscape#3653 (closed)

Edited by Alexander Roalter
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information