1

Problem

I want to get the value of a property of a org-contacts-db entry:

\* Example Contact :MYTAG:   
:PROPERTIES:  
:ID: foobar  
:END:

With the function call (org-contacts-filter "Example Contact" nil nil) I get an alist of the contact "Example Contact" from the org-contacts-db, which looks something like

((#("Example Contact" 0 15 (fontified nil org-category "contacts"))
  #<marker at 14837 in contacts.org>
 (("CATEGORY" . "contacts")
  ("ID" . "foobar") …

In order to get the value corresponding to the key "ID" I tried (cdr (assoc "ID" (org-contacts-filter "Example Contact" nil nil))), which only results in nil.

How do I get the value of the :ID: property?

Context

I want to build a capture template for events which can only take place at certain locations. Those locations and their addresses are all managed with org-contacts. The entries for the locations of interest share tagged the same. During the capture process I want to provide a list of locations, from which I can select the right one via the function my-capture-selection.

(defun my-capture-selection (list variable)  
  "Let the user choose between a pre-defined set of strings"  
  (make-local-variable variable)  
  (let ((selected-value (ido-completing-read "Select from list: " list)))  
    (set variable selected-value)  
    selected-value))

To create the list I can search my org-contacts-db for all entries with a certain tag. (org-contacts-filter nil "MYTAG" nil) The result is something like:

((#("NAME_1" … )) …
 ("ID" . "ORG_ID_1") …
 (#("NAME_2" … )) …
 ("ID" . "ORG_ID_2")

By calling %(my-capture-selection (org-contacts-filter nil "MYTAG" nil) 'my-selection)) in my capture template, I see all contacts tagged "MYTAG", select one of them and save the selection to "my-selection".

In order to provide a link to the org-contact for the location, I want to get the "ID" property, which was created with org-id. Since I already saved the name of the org-contacts-entry in "my-selection", I should be able to access any property of the entry. With access to the value of "ID" I could build a link to the entry with (concat "[[" …value of ID… "][" my-selection "]]")

  • How about org-id-get -- "Get the ID property of the entry at point-or-marker POM. ..."? – lawlist Jul 27 '17 at 0:26
  • Usually I won't start my capture process from within my org-contacts file. In order the get the ID of the correct contact I would have to visit my org-contacts file, search for the contacts heading, put a marker on it and invoke ´org-id-get´. Using the org-contacts-db seems to be the shorter and safer way to access a contacts properties. – rrog Jul 27 '17 at 10:23
2

You almost have the answer. The only problem is that the value returned by org-contacts-filter has several parts that need to be unpacked before you get to the assoc list. A bit of experimenting gave me this, which seems to work:

(cdr (assoc "ID"
            (nth 2 (car
                    (org-contacts-filter "Example Contact")))))

The second line is what I have added to your attempt.

  • Thank you! This solved my problem. The code stays valid for getting the value of every other property present in the org-contacts-entry by exchanging "ID" with another property-key. Very helpful! – rrog Jul 27 '17 at 10:11

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.