0

I was recently reading a blog about Do-nothing scripting which is about encoding the instructions for a task as a step towards automating it. It occurred to me that this would be very nice paired with org mode todo entries.

What I want is to do be able to run code contained in a task when I change the task's state from TODO -> ACTIVE.

* TODO Provision new user #+BEGIN_SRC shell python3 /path/to/myscript.py #+END_SRC * ACTIVE Some other heading #+BEGIN_SRC emacs-lisp (message "TEST") #+END_SRC 

I guess this can be achieved using org-after-todo-state-change-hook, but I'm unsure where to start. Could anybody offer some advice on how to go about this?

1

1 Answer 1

1

Wrote a function to do this. It finds a code block with the name specified in the task properties and runs it.

Org file:

** ACTIVE Make tea :PROPERTIES: :RUN_ON_ACTIVE: make-tea :END: #+NAME: make-tea #+begin_src elisp :results output (defun ask-completed (question) (while (not (y-or-n-p (concat question " Completed?"))))) (ask-completed "Pour water in the kettle.") (ask-completed "Boil the water.") (ask-completed "Put teabag in cup.") (ask-completed "Pour hot water in cup.") #+end_src #+RESULTS: make-tea 

Method in config.el

(defun execute-named-code-block-in-task () "Execute a named source block within the current task when its state is changed to ACTIVE." (when (string= org-state "ACTIVE") ; Check if the state is changed to ACTIVE (let ((block-name (org-entry-get nil "RUN_ON_ACTIVE"))) ; Get the code block name from properties (when block-name (org-with-wide-buffer (save-excursion (org-babel-goto-named-src-block block-name) (org-babel-execute-src-block))))))) (add-hook 'org-after-todo-state-change-hook 'execute-named-code-block-in-task) 
1
  • This is really cool! I've been wondering how to implement for quite a while. Thank you!! Commented 2 hours ago

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.