Inkscape.org
Beyond the Basics Passing a batch file to Inkscape in shell mode
  1. #1
    feedme feedme @feedme

    Hello everyone! Glad there is a chat here where we can ask questions about this great software. 
    I have a question about --shell mode on Windows.

    I'm trying to convert a large list of SVG files to PNG. 
    It seems that on Linux it is possible to redirect a file containing shell commands like this:     `inkscape --shell < commands.txt`
    Where commands.txt contains this sort of thing:
    file-open:frames000000.svg;export-filename:frames000000.png;export-do;file-open:frames000001.svg;export-filename:frames000001.png;export-do;  etc.

    But on Windows I get an error '> Unable to find: file-open' and it fails to convert my list of files. 
    I can only get it working if, instead of redirecting a file, I start inkscape in interactive --shell mode and then I can copy and paste the contents of commands.txt here, which works.      

    `inkscape --shell`
    `> file-open:frames000000.svg;export-filename:frames000000.png;export-do;file-open:frames000001.svg;export-filename:frames000001.png;export-do;`

    The problem is that I can only paste the commands for about 50 files, due to limits with pasting text into cmd or powershell, and I have hundreds or thousands to convert! 

    I can call Inkscape with a single file to convert each time, but the overhead of reopening inkscape for each file makes converting all of them very slow. 

    I would love to hear back from you if I have made a mistake here. Or is there a known issue with using --shell mode with Windows and an input file? 
    Perhaps the issue is with my attempt to redirect the file using `<` ?  
    Maybe a later version of Inkscape will accept a file with syntax like   `inkscape --shell --batch commands.txt`  ?  

    Thank you!

    I'm using Inkscape 1.0.2 on Windows 10.

    #shell

  2. #2
    inklinea inklinea @inklinea⛰️

    I don't know how to use Windows powershell ---- but can you give this a go ? 

    type ./command.txt | inkscape --shell

  3. #3
    inklinea inklinea @inklinea⛰️

    Well I had another go, messing with powershell. 

    This works for me : 

    Create a new script file called inkloop.ps1 and put the following code inside : 

     $files = Get-ChildItem -Name ./*.svg
     for ($i=0; $i -lt $files.Count; $i++) {
         
     $inkCmd = 'file-open:' + $files[$i] + ';' + 'export-filename:' + 'frames' + ([string]$i).PadLeft(5,'0') + '.png' + ';' + 'export-do'
         
        $inkCmds = $inkCmds +  "`r`n" + $inkCmd
         }
     
     #echo $inkCmds
     
     $randomNumber = Get-Random
     
     $randomCommandName = 'commandLine' + $randomNumber + '.txt'
     
     $inkCmds | Out-File $randomCommandName
     
     $inkscapeCommandLine = 'type ' + $randomCommandName + ' | inkscape --shell'
     
     echo $inkscapeCommandLine

    This will create a file list for all of the .svg files in the current folder.

    To run the script you have to use this line ( because windows by default has security against running powershell scripts )

     powershell -ExecutionPolicy Bypass -File .\inkLoop.ps1

    It creates the file list with the commands you wanted and also echos out the command line you need to pipe to inkscape at the end so you can copy and paste it back into the powershell.

    You should have a file which contains

    file-open:blobTest.svg;export-filename:frames00000.png;export-do
    file-open:checkerBoardSquiggle.svg;export-filename:frames00001.png;export-do
    file-open:circles.svg;export-filename:frames00002.png;export-do
    file-open:classTest.svg;export-filename:frames00003.png;export-do  ............. etc etc

    and a command line echoed out 

    type commandLine1645646307.txt | inkscape --shell

    which can be pasted and will start the shell batch conversion.

    I've only tried it with 25 .svgs but worked fine :)

     

     

  4. #4
    feedme feedme @feedme

    Hi @inklinea

    Thanks so much for taking the time to answer my question. In actual fact I already had a script that was generating the commands file, but your inkLoop.ps1 could come in handy for someone else, or for myself in future. 

    The bit I was really missing was (a) confirming whether Inkscape can accept a command file like this on Windows and (b) knowing what the syntax is in powershell to do that. You answered both (a) and (b) perfectly, and I'm very happy because I just tested this with 500 .svg files and it worked.... super fast!  I haven't timed it but I would guess that it's at least 10 times faster than my old method that re-opened Inkscape each time. 

    This will save me many hours. Wow. 

    Thanks again! 

Inkscape Inkscape.org Inkscape Forum Beyond the Basics Passing a batch file to Inkscape in shell mode