I want to focus emacs by running some elisp. I have tried using do-applescript and the following snippet to focus Emacs:

if application "Emacs" is running then tell application "System Events" to click UI element "Emacs" of list 1 of application process "Dock" end if The problem is that I don't know how to grant emacsclient assistive access (The elisp code is given to emacsclient as an argument, see my full script at the end to get a feel for what I mean. I have given Emacs.app that access.) Note that just "tell to activate" doesn't work if Emacs is in fullscreen. I have also tried these, they don't work:

(raise-frame) (x-focus-frame nil) (make-frame-invisible) (make-frame-visible) (other-frame 0) (select-frame-set-input-focus (selected-frame))

Here is my full bash script that gets a file and its position from IntelliJ and opens emacsclient with the necessary code to open the file and focus emacs:



#printf "%q\n" "hello\world"
#printf -v file "%q\n" $1
#printf "%q\n" "hello\world$1 $2 $3"


# if [ ! -e $socket ]; then
#     # $emacsdir/Emacs &
#     /usr/local/Cellar/emacs-mac/emacs-25.3-mac-6.8/Emacs.app/Contents/MacOS/Emacs &
#     while [ ! -e $socket ]; do
#         sleep 1
#     done
# fi

#Printf $file
emacsclient \
    --no-wait \
  -e \

       ;; Load the file
       (find-file \"$file\")

       ;; Jump to the same point as in IntelliJ
       ;; Unfortunately, IntelliJ doesn't always supply the values
       ;; depending on where the open is invoked from; e.g. keyboard 
       ;; works, tab context doesn't
         (when (not (string= \"\" \"$line\"))
         (goto-char (point-min))
         (forward-line (1- $2))
         (forward-char (1- $3)))

       ;; Raise/focus our window; depends on the windowing system
       (if (string-equal system-type \"darwin\")
(do-applescript \"tell application \\\"Emacs\\\" to activate\")
         (do-applescript \"if application \\\"Emacs\\\" is running then \\\n    tell application \\\"System Events\\\" to click UI element \\\"Emacs\\\" of list 1 of application process \\\"Dock\\\" \\\n end if\")))
  (x-focus-frame nil)
         (other-frame 0)
         (select-frame-set-input-focus (selected-frame))

       ;; Automatically pick up changes made in IntelliJ
       (auto-revert-mode t))"
  • 1
    I would recommend spending a little bit of time editing your question to provide more details about what it is exactly you are trying to accomplish, and how emacsclient fits into that equation. If it is not working, say what is not working (what happens) and what error message you receive instead (if any). You can try to launch the applescript manually from the terminal. There are a few threads on stackoverflow and perhaps here that have examples of applescripts dealing with emacsclient, but without more details in your question, it is impossible to know if any of those would help you ....
    – lawlist
    Mar 21, 2018 at 15:39
  • @lawlist I added some details.
    – HappyFace
    Mar 21, 2018 at 19:27

1 Answer 1


I have found that somehow running the following applescript in my script works (I have to give assistive access to IntelliJ, which is no problem):

osascript -e 'tell application "System Events" to click UI element "Emacs" of list 1 of application process "Dock"'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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