              (evernote-get-current-line-string)
              evernote-search-mode-formatted-name-displayed-name-alist))
       (kill-buffer (current-buffer))
-      (evernote-set-minibuffer-string displayed-name)
-      (exit-minibuffer))))
+      (if (active-minibuffer-window)
+          (progn
+            (evernote-set-minibuffer-string displayed-name)
+            (exit-minibuffer))))))
 
 
 (defun evernote-find-opened-buffer (guid)

            "Saved search:"
            search-alist
            nil t)
           search-alist))))))))


(defun evernote-open-note-common (note-command-output)
  "Common procedure of opening a note"
  (let (note-attr note-guid note-name note-edit-mode note-tags opened-buf)
    (setq note-attr (evernote-read-note-attr note-command-output))
    (setq note-guid (evernote-assoc-cdr 'guid note-attr)
          note-name (evernote-assoc-cdr 'name note-attr)
          note-edit-mode (evernote-assoc-cdr 'edit-mode note-attr)

   (lambda ()
     (evernote-open-note-common
      (evernote-command-get-note-list-from-query
       (read-string "Query:"))
      t))))


(defun evernote-do-saved-search ()
