I've written a simple script in Windows powershell to loop through some SVG files and make an adjustment to the page area before exporting. This works well but is relatively slow. Therefore I wanted to try using Inkscape's shell mode but I'm completely perplexed as to how to make this work. I've tried the approach used in this page https://inkscape.org/doc/inkscape-man.html but without luck. Here is my 'working but slow' script to show what I'm trying to do:
$files = Get-ChildItem "C:\temp\SVG Charts" -filter *.svg
# Starts a FOR loop which runs through each of the files from above
foreach ($f in $files){
# Prepare the name of the outputted file
# So you don't save over the originals
$outfile = $f.DirectoryName + "\Adjusted_" + $f.BaseName + ".svg"
# Run inkscape
# The export-area-drawing command below simply resets the print area of the SVG
inkscape $f.FullName --export-area-drawing --export-plain-svg $outfile
}
Below is my attempt at a shell version. I think I have the loop structured incorrectly and need to put inkscape --shell
outside of the loop, however then it just hangs, waiting for input. If I put it inside the loop, then I'm prompted to type quit for each iteration and am then presented with lots of red error text which I think is probably related to the next point... There seem to be several different ways of writing shell commands and I don't know if I'm even using the right approach! I'm struggling to address the last point because I don't know if I've even set the loop up in the correct way to use shell.
$files = Get-ChildItem "C:\temp\SVG Charts" -filter *.svg
# Starts a FOR loop which runs through each of the files from above
foreach ($f in $files){
# Prepare the name of the outputted file
# So you don't save over the originals
$outfile = $f.DirectoryName + "\Adjusted_" + $f.BaseName + ".svg"
# The export-area-drawing command below simply resets the print area of the SVG
inkscape --shell
file-open:$f.FullName; export-area-drawing; export-filename:$outfile; export-do
}
My question is: How should I structure this Powershell loop and what syntax should I be using to run shell commands?
I'm running a relatively old version of Inkscape which no doubt doesn't help: Inkscape 0.92.3
As Moini suggests, please upgrade your version of Inkscape. I was only able to make this work with the latest Inkscape version 1.0.1:
# Avoid specifying long file paths in Inkscape commands...
Set-Location -Path 'C:\Temp\SVG Charts'
# Build-up a list of actions to send to Inkscape...
[string]$actions = ''
$files = Get-ChildItem -Filter *.svg
foreach ($f in $files) {
$actions += "file-open:$($f.Name); export-area-drawing; export-filename:Adjusted_$($f.Name); export-do`r`n"
}
# Ensure to end your list of actions with 'quit', then pipe it to Inkscape...
"$actions`r`nquit" | & "C:\Program Files\Inkscape\bin\inkscape.com" --shell
I hope this short example is helpful.