I want to create a file with
org-open-at-point from a template if that file does not exist. If it exists, I want to open it in an external app.
In org-mode, file-links are created by
C-c C-o opens that file. This behavior is defined by
org-file-apps depending on the file extension.
According to the docs,
org-file-apps can take functions as argument:
Possible values for the command are: string A command to be executed by a shell; %s will be replaced by the path to the file. function A Lisp function, which will be called with two arguments: the file path and the original link string, without the "file:" prefix.
(add-to-list 'org-file-apps '("\\.svg\\'" . "inkscape %s"))
opens that file in inkscape.
I wrote a function to create the file beforhand if it does not exist:
(defun inkscape-open (path &optional link) "Open the path in inkscape. Copy template if path does not exist." (unless (file-exists-p path) (shell-command (format "cp /home/jolla/Dropbox/org/sketches/default.svg %s" path))) (shell-command (format "inkscape %s" path)) )
and set it for svg-files:
(add-to-list 'org-file-apps '("\\.svg\\'" . inkscape-open))
1.) This does not work: emacs tells me that the file does not exist. If it exists, emacs opens the file.
2.) I would like to modify my function so that it only creates the file if a specific directory (like Dropbox/org/sketches) is in path. What would I have to do?
3.) How can I switch focus to inkscape after org-open-at-point?
PS: if I create a new link-parameter like
[[svg:path-to-file.ext]] like this:
(org-link-set-parameters "svg" :follow (lambda (path) (let ((actions '(("find-file" . find-file) ("edit in inkscape" . inkscape-open)))) (funcall (cdr (assoc (completing-read "Action: " actions) actions)) path))))
I can chose
inkscape-open and the function works as expected. This is also described here. However, I don't want to create a new link type because this would prevent org-mode from showing inline images.