Org-QL
Query Org files with Emacs in batch mode using org-ql. Use when you need a structured view of projects, tasks, or org metadata.
Setup
Use the agent batch init so org-ql and your org config load correctly:
- Init file:
/home/ben/.emacs.d/org-agent-init.el - Org files come from
org-agenda-filesin~/.emacs.d/settings.el
Process
- Run
emacs --batchwith the agent init file. - Use
org-ql-selectwith an org-ql query. - Print results with
princ(plain text) or JSON for easy parsing.
Examples
List all projects (TODO=PROJ)
emacs --batch -l /home/ben/.emacs.d/org-agent-init.el --eval "(progn
(dolist (entry
(org-ql-select (org-agenda-files)
'(todo \"PROJ\")
:action (lambda () (list (buffer-file-name)
(org-get-heading t t t t)))))
(princ (format \"%s\t%s\\n\" (car entry) (cadr entry)))))"
List TODOs tagged @work (JSON)
emacs --batch -l /home/ben/.emacs.d/org-agent-init.el --eval "(progn
(require 'json)
(let ((items
(org-ql-select (org-agenda-files)
'(and (todo \"TODO\") (tags \"@work\"))
:action (lambda ()
(list (buffer-file-name)
(org-get-heading t t t t)
(org-get-category)
(org-entry-get (point) \"PRIORITY\"))))))
(princ (json-encode items))))"
Find overdue tasks (deadline before today)
emacs --batch -l /home/ben/.emacs.d/org-agent-init.el --eval "(progn
(dolist (entry
(org-ql-select (org-agenda-files)
'(deadline :to -1)
:action (lambda () (list (buffer-file-name)
(org-get-heading t t t t)
(org-entry-get (point) \"DEADLINE\")))))
(princ (format \"%s\t%s\t%s\\n\" (nth 0 entry) (nth 1 entry) (nth 2 entry)))))"
Notes
- org-ql queries run over
org-agenda-filesby default. - prefer JSON output when you need to parse results programmatically.
- avoid editing org files via batch unless explicitly asked.