(or emacs irrelevant)

My org-protocol setup, part 2.

This continues the code from the part 1.

I tried to make the first call to youtube-dl asynchronous, but it wasn't working out. So for the current code, there's still about a 2 second delay before the capture buffer appears.

(require 'async)
(defun org-handle-link-youtube (link)
  (lexical-let*
      ((file-name (org-trim
                   (shell-command-to-string
                    (concat
                     "youtube-dl \""
                     link
                     "\""
                     " -o \"%(title)s.%(ext)s\" --get-filename"))))
       (dir "~/Downloads/Videos")
       (full-name
        (expand-file-name file-name dir)))
    (add-hook 'org-link-hook
              (lambda ()
                (concat
                 (org-make-link-string dir dir)
                 "\n"
                 (org-make-link-string full-name file-name))))
    (async-shell-command
     (format "youtube-dl \"%s\" -o \"%s\"" link full-name))
    (find-file (org-expand "ent.org"))
    (goto-char (point-min))
    (re-search-forward "^\\*+ +Videos" nil t)))

Some notes for people who want to learn more Elisp:

  • lexical-let* is needed to have dir and full-name visible in the lambda.
  • org-make-link-string is a nice utility command that escapes all sorts of characters that org-mode doesn't like, e.g. brackets etc.

You can see my full org-capture and org-protocol setup here.