I have made (with my little knowledge of Bash and an extensive use of a search engine) a Bash script to reorder the pages of a big PDF file:
#!/bin/bash
file=originalfile.pdf;
newfile=$(basename $file .pdf)-2.pdf;
tmpfile=$(mktemp --suffix=.pdf);
blankfile=$(mktemp --suffix=.pdf);
cp -f $file $newfile;
cp -f $file $tmpfile;
numberofpages=`pdftk $file dump_data | grep "NumberOfPages" | sed 's:.*\([0-9][0-9*]\).*:\1:'`;
echo "" | ps2pdf -sPAPERSIZE=a4 - $blankfile;
while (( $numberofpages % 4 != 0 ));
do
((numberofpages++));
pdftk A=$newfile B=$blankfile cat A B output $tmpfile;
cp -f $tmpfile $newfile;
done;
neworder=`
for (( a=1, b=3, c=4, d=2 ;
a <=numberofpages ;
((a+=4)), ((b+=4)), ((c+=4)), ((d+=4))
));
do
echo -n "$a $b $c $d ";
done`;
pdftk $tmpfile cat $neworder output $newfile;
I wanted to make a Nautilus-Actions script out of it so it could be "installed" and used by a regular user. By regular user, I mean someone unable to type any command-line and unable to follow a few steps to copy the script at a specified place.
Unfortunately the script didn't work and I came up with this new script thanks to the help of people commenting below:
#!/bin/bash
file=originalfile.pdf;
newfile=$(basename $file .pdf)-2.pdf;
tmpfile=$(mktemp --suffix=.pdf);
blankfile=$(mktemp --suffix=.pdf);
cp -f $file $newfile;
cp -f $file $tmpfile;
numberofpages=`pdftk $file dump_data | grep "NumberOfPages" | sed 's:.*\([0-9][0-9*]\).*:\1:'`;
echo "" | ps2pdf -sPAPERSIZE=a4 - $blankfile;
while (( $numberofpages % 4 != 0 )); # NOTE: replace % by %% in Nautilus-Actions
do
((numberofpages++));
pdftk A=$newfile B=$blankfile cat A B output $tmpfile;
cp -f $tmpfile $newfile;
done;
a=0;
neworder=$(
while [ $a -lt $numberofpages ];
do
echo -n "$(($a + 1)) $(($a + 3)) $(($a + 4)) $(($a + 2)) ";
((a+=4));
done;
);
pdftk $tmpfile cat $neworder output $newfile;
I did paste everything in the Path
entry of Nautilus-Actions and it finally worked. The newly created Nautilus-action could then be exported in a .desktop
file (and therefore imported very easily by any user):
If I ask Nautilus-Actions to display the output, It seems that Nautilus-Actions execute the command line inside a /bin/sh -c 'myscript...'
command.
Could you explain to me why I had to change so many things in order to make it work ? Especially why I had to change the for
into a while
?
Note: I completely revamp the question since It was a mess.