Function: eudc-macos-contacts-search-helper

eudc-macos-contacts-search-helper is a byte-compiled function defined in eudcb-macos-contacts.el.gz.

Signature

(eudc-macos-contacts-search-helper QUERY)

Documentation

Helper function to query the Contacts app via AppleScript.

Searches for all persons matching QUERY. QUERY is a list of cons cells (ATTR . VALUE) where ATTRs should be valid macOS Contacts attribute names with space characters replaced by _ characters. Thus, to for instance search for the "first name" attribute in the Contacts app, the corresponding ATTR would be the symbol first_name.

Note that due to the way the Contacts app exposes its data via AppleScript, the attributes listed in eudc-macos-contacts--unsearchable-attributes can not be searched efficiently. If and when one of these attributes appears in QUERY, it is thus skipped, and the query is composed from the other attributes in the QUERY.

Source Code

;; Defined in /usr/src/emacs/lisp/net/eudcb-macos-contacts.el.gz
(defun eudc-macos-contacts-search-helper (query)
  "Helper function to query the Contacts app via AppleScript.
Searches for all persons matching QUERY.  QUERY is a list of cons
cells (ATTR . VALUE) where ATTRs should be valid macOS Contacts
attribute names with space characters replaced by `_' characters.
Thus, to for instance search for the \"first name\" attribute in
the Contacts app, the corresponding ATTR would be the symbol
`first_name'.

Note that due to the way the Contacts app exposes its data via
AppleScript, the attributes listed in
`eudc-macos-contacts--unsearchable-attributes' can not be searched
efficiently.  If and when one of these attributes appears in
QUERY, it is thus skipped, and the query is composed from the
other attributes in the QUERY."
  (let ((crit-idx 0)
        (query-str (string)))
    ;; assemble a query string for use in an AppleScript "whose"
    ;; filter clause; generally, this has the form
    ;; (ATTR1 contains "VALUE1") and (ATTR2 contains "VALUE2") and ...
    (dolist (criterion query)
      (let ((attr (string-replace "_" " " (symbol-name (car criterion))))
            (term (cdr criterion)))
        ;; defend against unusable attribute names as they cause
        ;; AppleScript to emit an error message, which in turn will
        ;; cause elisp errors during results parsing in
        ;; `eudc-macos-contacts-query-internal'
        (if (or (not (rassq (car criterion)
                            eudc-macos-contacts-attributes-translation-alist))
                (memq (car criterion)
                      eudc-macos-contacts--unsearchable-attributes))
            (message (concat "[eudc] Warning in macOS Contacts backend: "
                             "can not search in attribute "
                             (format "\"%s\"; skipping it."  attr)))
          (progn
            (when (> crit-idx 0)
              (setq query-str (concat query-str " and ")))
            (setq query-str (concat query-str
                                    (format "(%s contains \"%s\")" attr term)))
            (setq crit-idx (1+ crit-idx))))))
    ;; if a useful query string could be assembled, insert it into the
    ;; AppleScript template, and run the resulting script; results are
    ;; captured in the current buffer
    (if (not (string= query-str ""))
        (if (executable-find "osascript")
            (call-process "osascript" nil t nil
                          "-e"
                          (format "
on joinLines(theText)
        if (theText is missing value) or (theText is \"\") then
                return \"\"
        else
                set thePars to paragraphs of theText
                set result to {}
                repeat with para in thePars
                        set result to result & {para & space}
                end repeat
                return text 1 thru -2 of (result as text)
        end if
end joinLines

on run
        set results to {}
        tell application \"Address Book\"
                set pList to every person whose %s
                repeat with pers in pList
                        set pText to ¬
                                first name of pers & \":\" & ¬
                                last name of pers & \":\"
                        if (job title of pers is not missing value) then ¬
                                set pText to pText ¬
                                        & my joinLines(job title of pers)
                        set pText to pText & \":\"
                        if (department of pers is not missing value) then ¬
                                set pText to pText ¬
                                        & my joinLines(department of pers)
                        set pText to pText & \":\"
                        if (organization of pers is not missing value) then ¬
                                set pText to pText ¬
                                        & my joinLines(organization of pers)
                        set pText to pText & \":\"
                        if (count emails of pers) > 0 then
                                repeat with emailAddr in emails of pers
                                        set pText to pText & value ¬
                                                of emailAddr & \",\"
                                end repeat
                                set pText to text 1 thru -2 of pText
                        end if
                        set pText to pText & \":\"
                        if (count phones of pers) > 0 then
                                repeat with phoneNmbr in phones of pers
                                        set pText to pText & value ¬
                                                of phoneNmbr & \",\"
                                end repeat
                                set pText to text 1 thru -2 of pText
                        end if
                        set results to results & {pText & \"\n\"}
                end repeat
                get results as text
        end tell
end run
" query-str))
          (message (concat "[eudc] Error in macOS Contacts backend: "
                           "`osascript' executable not found. "
                           "Is this is a macOS 10.0 or later system?"))))))