tags:

views:

13

answers:

1

Hi, I have a function to compress my pdf file using pdftk:

(defun compresspdf (filename)
  (interactive)
  (let ((tmpfile (concat filename "~")))
    (start-process-shell-command "pdftk" nil
    (format "pdftk %s cat output %s compress dont_ask"
        filename tmpfile))
    (rename-file tmpfile filename t)))

It compresses the file and saves it as the same name with ~ appended. However, at the point where it's supposed to rename the file, it gives me an error: let: Renaming: No such file or directory, /pathtofile/mypdf.pdf~, /pathtofile/mypdf.pdf, though clearly, both of these files exist. I can separately evaluate rename-file afterwards and it works fine. Maybe it's trying to rename the ~ file before it's actually created? In that case, how can I make it wait until the process is finished? (and possibly check for errors?)

+2  A: 

As opposed to using 'start-process-shell-command, which just spawns the process, so the rename happens before the tmpfile is created. Try using 'shell-command, like so:

(defun compresspdf (filename)
  (interactive)
  (let ((tmpfile (concat filename "~")))
    (with-temp-buffer 
      (shell-command (format "pdftk %s cat output %s compress dont_ask"
                             filename tmpfile)
                     (current-buffer)
                     (current-buffer)))
    (rename-file tmpfile filename t)))

The call to 'shell-command will finish after the process is done (compression is complete).

Trey Jackson
Ah, so I can use the temp-buffer to check for errors too - great stuff!
Stephen