├── README.md └── reveal-in-osx-finder.el /README.md: -------------------------------------------------------------------------------- 1 | reveal-in-osx-finder 2 | ===== 3 | 4 | **Usage:** 5 | 6 | - If ```M-x reveal-in-osx-finder``` is invoked in a file-associated buffer, it will open the folder enclosing the file in the OS X Finder. It will also highlight the file the buffer is associated with within the folder. 7 | 8 | - If ```M-x reveal-in-osx-finder``` is invoked in a dired buffer, it will open the current folder in the OS X Finder. It will also highlight the file at point if available. 9 | 10 | - If ```M-x reveal-in-osx-finder``` is invoked in a buffer not associated with a file, it will open the folder defined in the default-directory variable. 11 | 12 | 13 | **Installation** 14 | 15 | This package depends on ```dired.el```, which should be available in the default emacs installation. It only works on the OS X environment on Macs. 16 | 17 | It is available on the MELPA repository. Do the following, then choose and install reveal-in-osx-finder. 18 | 19 | To configure the MELPA, see this: http://melpa.milkbox.net/#/getting-started 20 | 21 | ``` 22 | M-x list-packages 23 | ``` 24 | 25 | Then, put the following in your emacs configuration file. 26 | 27 | ```lisp 28 | ;; To load at the start up 29 | (require 'reveal-in-osx-finder) 30 | ;; If you want to configure a keybinding (e.g., C-c z), add the following 31 | (global-set-key (kbd "C-c z") 'reveal-in-osx-finder) 32 | ``` 33 | 34 | **Acknowledgement:** 35 | 36 | This is a modified version of the ```open-finder``` found at the URL below. Thank you elemakil and lawlist for introducing this nice piece of code, 37 | 38 | http://stackoverflow.com/questions/20510333/in-emacs-how-to-show-current-file-in-finder 39 | 40 | and Peter Salazar for pointing out a useful link about AppleScript (below), 41 | 42 | http://stackoverflow.com/questions/11222501/finding-a-file-selecting-it-in-finder-issue 43 | 44 | and mikeypostman and purcell for auditing the code for MELPA approval. 45 | -------------------------------------------------------------------------------- /reveal-in-osx-finder.el: -------------------------------------------------------------------------------- 1 | ;;; reveal-in-osx-finder.el --- Reveal file associated with buffer in OS X Finder 2 | 3 | ;; Copyright (C) 2014- Kazuki YOSHIDA 4 | 5 | ;; Author: Kazuki YOSHIDA 6 | ;; Keywords: OS X, Finder 7 | ;; URL: https://github.com/kaz-yos/reveal-in-osx-finder 8 | ;; Version: 0.3.3 9 | 10 | ;; This program is free software; you can redistribute it and/or modify 11 | ;; it under the terms of the GNU General Public License as published by 12 | ;; the Free Software Foundation, either version 3 of the License, or 13 | ;; (at your option) any later version. 14 | 15 | ;; This program is distributed in the hope that it will be useful, 16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | ;; GNU General Public License for more details. 19 | 20 | ;; You should have received a copy of the GNU General Public License 21 | ;; along with this program. If not, see . 22 | 23 | ;;; Commentary: 24 | ;; 25 | ;; Usage: 26 | ;; 27 | ;; If M-x reveal-in-osx-finder is invoked in a file-associated buffer, 28 | ;; it will open the folder enclosing the file in the OS X Finder. 29 | ;; It will also highlight the file the buffer is associated with within the folder. 30 | ;; 31 | ;; If M-x reveal-in-osx-finder is invoked in a dired buffer, 32 | ;; it will open the current folder in the OS X Finder. 33 | ;; It will also highlight the file at point if available. 34 | ;; 35 | ;; If M-x reveal-in-osx-finder is invoked in a buffer not associated with a file, 36 | ;; it will open the folder defined in the default-directory variable. 37 | ;; 38 | ;; 39 | ;; Special thanks: 40 | ;; 41 | ;; This is a modified version of the open-finder found at the URL below. 42 | ;; Thank you elemakil and lawlist for introducing this nice piece of code, 43 | ;; http://stackoverflow.com/questions/20510333/in-emacs-how-to-show-current-file-in-finder 44 | ;; and Peter Salazar for pointing out a useful link about AppleScript (below), 45 | ;; http://stackoverflow.com/questions/11222501/finding-a-file-selecting-it-in-finder-issue 46 | ;; and mikeypostman and purcell for auditing the code for MELPA approval. 47 | 48 | 49 | ;;; Code: 50 | 51 | ;; Requires dired.el for (dired-file-name-at-point) 52 | (require 'dired) 53 | 54 | 55 | ;;;###autoload 56 | (defun reveal-in-osx-finder () 57 | "Reveal the file associated with the current buffer in the OS X Finder. 58 | In a dired buffer, it will open the current directory." 59 | (interactive) 60 | (let* ((path (buffer-file-name)) ; The full file path associated with the buffer. 61 | (filename-at-point (dired-file-name-at-point)) ; effective in dired only 62 | ;; Create a full path if filename-at-point is non-nil 63 | (filename-at-point (if filename-at-point 64 | (expand-file-name filename-at-point) ; full path 65 | nil)) ; if nil, return nil 66 | dir file) ; let* definition part ends here. 67 | 68 | ;; Conditionals: The first one that is non-nil is executed. 69 | (cond (path 70 | ;; If path is non-nil, 71 | (setq dir (file-name-directory path)) 72 | (setq file (file-name-nondirectory path))) 73 | 74 | (filename-at-point 75 | ;; If filename-at-point is available from dired, 76 | (setq dir (file-name-directory filename-at-point)) 77 | (setq file (file-name-nondirectory filename-at-point))) 78 | 79 | (t 80 | ;; Otherwise, 81 | (setq dir (expand-file-name default-directory)))) 82 | 83 | ;; Pass dir and file to the helper function. 84 | ;; (message (concat "dir:" dir " ; file:" file " ; path:" path " ; fap:" filename-at-point)) ; for debugging 85 | (reveal-in-osx-finder-as dir file) ; These variables are passed to the helper function. 86 | )) 87 | 88 | 89 | ;; AppleScript helper function. Thanks milkeypostman for suggestions. 90 | ;; Use let* to reuse revealpath in defining script. 91 | (defun reveal-in-osx-finder-as (dir file) 92 | "A helper function for reveal-in-osx-finder. 93 | This function runs the actual AppleScript." 94 | (let* ((revealpath (if file ; Define revealpath local variable. 95 | (concat dir file) ; dir/file if file name available. 96 | dir)) ; dir only if not. 97 | (script ; Define script variable using revealpath and text. 98 | (concat 99 | "set thePath to POSIX file \"" revealpath "\"\n" 100 | "tell application \"Finder\"\n" 101 | " set frontmost to true\n" 102 | " reveal thePath \n" 103 | "end tell\n"))) ; let* definition part ends here. 104 | ;; (message script) ; Check the text output. 105 | (start-process "osascript-getinfo" nil "osascript" "-e" script) ; Run AppleScript. 106 | )) 107 | 108 | 109 | (provide 'reveal-in-osx-finder) 110 | ;;; reveal-in-osx-finder.el ends here 111 | --------------------------------------------------------------------------------