Q:

In org-mode: Open attachment with one keystroke

As I want to open attachment files with one keystroke, I am looking for a keybinding that basically does the same as org-attach o or C-c C-a o, respectively.  This should work in org files and the agenda. 
I defined this keybinding (global-set-key (kbd "<f9>") 'org-attach-open), but this is not working in the agenda, prompting Need absolute ’org-attach-directory’ to attach in buffers without filename. 
Minimal working example to reproduce this: In your agenda, move the point on a line with an attachment and execute M-x org-attach-open. 
How do I define a key binding that does exactly org-attach o, not regarding from where I am calling it? 

A:

When the user selects the letter o after calling org-attach from an *Org Agenda* buffer, the user has technically already left the *Org Agenda* buffer (even though this is done under the hood).   Therefore, we need a new function that does the same thing as org-attach before running org-attach-open. 
(defun org-agenda-attach-open ()
"My doc-string." 
(interactive)
  (unless (eq major-mode 'org-agenda-mode)
    (let ((debug-on-quit nil))
      (signal 'quit '("This was written expressly for `*Org Agenda*`.")))) 
  (let ((marker (or (get-text-property (point) 'org-hd-marker)
                    (get-text-property (point) 'org-marker))))
    (if marker
      (save-excursion
        (set-buffer (marker-buffer marker))
        (goto-char marker)
        (org-back-to-heading t)
        (call-interactively 'org-attach-open))
      (error "No task in current line"))))

