2011-02-19

Finder で最前面に表示されているフォルダを Emacs (Dired) で開く

Finder に切り替えて、フォルダを Dock の Emacs アイコンにドラッグ&ドロップすればいいのですが、もっと手軽にやりたい。

do-applescript 関数は互換性に関して面倒が多いので、 osascript を使います。

(defun my-dired-finder-window ()
  "Open the front most window of Finder in dired."
  (interactive)
  (let (file
        (script (concat
                 "tell application \"Finder\"\n"
                 "    if ((count Finder windows) >= 1) then\n"
                 "        get POSIX path of (target of window 1 as alias)\n"
                 "    else\n"
                 "        get POSIX path of (desktop as alias)\n"
                 "    end if\n"
                 "end tell\n")))
    (setq file (with-temp-buffer
                 (call-process "osascript" nil t nil "-e" script)
                 (buffer-substring-no-properties (point-min) (1- (point-max)))))
    (if (file-directory-p file)
        (dired file)
      (error "Not a directory: %s" file))))

(global-set-key "\C-cf" 'my-dired-finder-window)

動作確認: GNU Emacs 22, Mac OS X 10.6