1

I would like to design a simple Automator service that would delete last page from PDF passed via finder. My proposed syntax for the Shell Script is:

for f in "$@"
do
    pages=`pdfcount "$f"`
    pagescut=`$pages - 1`
    /usr/local/bin/pdfsplit "$f" - $pagescut > "$f".tmp
    rm "$f"
    mv "$f".tmp "$f"
done

My Automator workflow looks like that: Automator screenshot The syntax to delete first page: /usr/local/bin/pdfsplit "$f" 2- > "$f".tmp, works like a charm so the error must be with doing the arithmetic on page numbers. I tried to modify the syntax:

for f in "$@"
do
    pages=`pdfcount "$f"`
    pagescut=`$(( ${pages} - 1 ))`
    /usr/local/bin/pdfsplit "$f" - $pagescut > "$f".tmp
    rm "$f"
    mv "$f".tmp "$f"
done

but I'm not getting the desired results. How to construct this service properly?

  • In general, it's easer to get the shell script part correct directly in Terminal first (and integrate it into an Automator workflow afterwards), primarily because Automator doesn't give all error details back if the shell script fails. – nohillside Feb 22 '15 at 14:58
1

You have a couple of errors on your syntax. Replace the following lines:

pages=`pdfcount "$f"`
pagescut=`$(( ${pages} - 1 ))`

With this ones:

pages=$(pdfcount "$f")
pagescut=$(($pages - 1))
  • At least the assignment to pagescut must be changed, the first line is correct in both parts. – nohillside Feb 22 '15 at 14:56
  • I tried that but my original syntax with the amendments applied left only one page from the PDF. I tried modifying the syntax: /usr/local/bin/pdfsplit "$f" $pagescut - $ > "$f".tmp but it appears like it's not removing any pages. – Konrad Feb 22 '15 at 17:16
  • Try this way: /usr/local/bin/pdfsplit 1-$pagescut "$f" > "$f".tmp – jherran Feb 22 '15 at 17:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .