2

I have a directory ~/foo/ with hundreds of org documents among other types of files. I want to export each org file to ASCII. The only way I know how to do this is to follow the following procedure

  1. open each file with emacs and issue C-c C-e t a
  2. answer yes to the question Evaluate this emacs-lisp code block on your system?

Can this procedure be automated from the command line?

Update: with the help of @zck's answer, my script now looks like

#!/bin/bash

for file in ~/foo/*.org; do
    emacs --batch -l ~/.emacs \
        --eval "(require 'org)" \
        --insert "${file}" \
        --eval "(org-ascii-export-as-ascii nil nil nil nil '(:ascii-charset ascii))" \
        --eval "(write-file \"${file}.report\")" \
        --kill
done

This does the job but... I have to answer "yes" to the question Evaluate this emacs-lisp code block on your system? for each file. Is there a way to automatically answer yes to this question?

Brian Fitzpatrick
  • 2,265
  • 1
  • 17
  • 40

1 Answers1

1

The immediate problem is that you haven't imported org. This imports it:

  emacs -Q --batch --eval "(require 'org)" \
    --insert "${file}" -f org-export-dispatch \
    --eval "(write-file \"${file}.report\")" --kill

But it ends up prompting you with Export command: This is because org-export-dispatch calls org-export--dispatch-ui, which calls org-export--dispatch-action, which has the following docstring (emphasis added):

Read a character from command input and act accordingly.

So if we call org-export-dispatch, it's going to try to read from input. We need to find the function that ends up being called, and call that directly.

The ASCII options displayed in the Org Export Dispatcher are created in ox-ascii.el in org. (I found this by grepping for "As ASCII buffer"). The function we want to call is org-ascii-export-as-ascii, and it's called with five arguments. The last is a literal, and three of the other four are explained in org-export-as -- we can set all of SUBTREEP, VISIBLE-ONLY, and BODY-ONLY to nil. I'm assuming that we can also set the ASYNC argument to nil.

So we end up with this long function:

emacs -Q --batch \
  --eval "(require 'org)" \
  --insert "$FILE" \
  --eval "(org-ascii-export-as-ascii nil nil nil nil '(:ascii-charset ascii))" \
  --eval "(write-file \"$FILE.report\")" \
  --kill

This exports the file as expected.

zck
  • 8,984
  • 2
  • 31
  • 65
  • Hmm... This script is now returning `Symbol's function definition is void: org-ascii-export-as-ascii` Maybe there is a problem with importing org? – Brian Fitzpatrick Sep 21 '15 at 23:02
  • @BrianFitzpatrick Do you use a git checkout of Org, or the version shipped with Emacs? If you use the git version, maybe it is not loaded properly? – suvayu Sep 21 '15 at 23:12
  • Replacing `emacs -Q --batch` with `emacs --batch -l ~/.emacs` seems to do the trick. Now the problem is I have to answer `yes` to the question `Evaluate this emacs-lisp code block on your system?` for each file. Any idea how to automatically answer `yes` to this question each time? – Brian Fitzpatrick Sep 22 '15 at 02:07
  • @BrianFitzpatrick can you give the complete command you're using? I'm not sure what that error is, but it might be hard to reproduce if you're using your .emacs. Also, I'm not sure why you were not finding `org-ascii-export-as-ascii`. You were definitely importing org? – zck Sep 22 '15 at 04:39
  • @zck I've updated my question with the complete command I'm using now. To be honest, I think I'm having problems with my installation of org-mode (org-mode has been doing a few other weird things today). I believe the problem is that I stupidly tried to install both the git and ELPA versions. – Brian Fitzpatrick Sep 22 '15 at 04:42
  • It does seem like it might be an org problem; the command you're trying in the question works for me. Can you try opening `emacs -Q`, evaling `(load 'org)`, and then seeing if `org-ascii-export-as-ascii` is a function? – zck Sep 22 '15 at 05:34