切换 Mac 应用

从 Minibuffer 读取一个运行着的应用,然后切换到它:

(defun chunyang-mac-switch-to-app (app)
  (interactive
   (let (apps)
     (with-temp-buffer
       (call-process "osascript" nil t nil "-e"
                     (concat
                      "tell application \"Finder\"\n"
                      "  get the name of every process whose visible is true\n"
                      "end tell"))
       (setq apps (split-string (buffer-string) "[,\n]" t " ")))
     (list (completing-read "Switch to Application: " apps))))
  (do-applescript (format "tell application \"%s\" to activate" app)))

P.S. 不知道为什么第一段 Applescript 用 do-applescript 得不到预期结果,所以改用了 osascript

3 个赞