・ Cocoa Emacs の GUI 起動
・ Carbon Emacs の GUI 起動
・ Carbon Emacs の -nw 起動
3つそれぞれ微妙に違ったコードを書かなければならず面倒なので、 do-applescript は使わず外部コマンドの osascript を呼ぶ方法を紹介します。
キーバインドの例では、
M-DEL で Finder の「ゴミ箱に入れる」
M-i で Finder の「情報を見る」
M-r で Finder の「オリジナルを表示」
に割り当ててますが、適当に変えてください。 (特に M-r)
Dired で Finder の「ゴミ箱に入れる」
(defun my-dired-do-trash-1 (async &rest files) (let ((script (concat "on run argv\n" " set itemArray to {}\n" " repeat with i in argv\n" " set itemArray to itemArray & (i as POSIX file as Unicode text)\n" " end repeat\n" " tell application \"Finder\"\n" " delete itemArray\n" " end tell\n" "end run\n"))) (if async (apply 'start-process "osascript-trash" nil "osascript" "-e" script files) (apply 'call-process "osascript" nil nil nil "-e" script files)))) ;; Trash files (defun my-dired-do-trash (&optional arg) "Trash the marked files. If no files are marked or a specific numeric prefix arg is given, the next ARG files are used. Just \\[universal-argument] means the current file." (interactive "P") (let ((files (dired-get-marked-files nil arg)) point) (message "Trashing...") ;; Position of the first mark (save-excursion (goto-char (point-min)) (setq point (re-search-forward dired-re-mark nil t))) (unless point (setq point (point))) (apply 'my-dired-do-trash-1 nil files) (revert-buffer) ;; Restore the position (goto-char (point-min)) (forward-line (line-number-at-pos point)) (forward-line -1) (dired-move-to-filename) (message "Trashing...done") ;; Display the result (if (null (cdr files)) (message "Trashed 1 item: %s" (file-name-nondirectory (car files))) (display-message-or-buffer (format "Trashed %d items:\n%s" (length files) (mapconcat 'file-name-nondirectory files "\n")))))) (eval-after-load "dired" '(define-key dired-mode-map "\M-\C-?" 'my-dired-do-trash)) ; M-DEL, dired-unmark-all-files, use U or * ? RET
Dired で Finder の「情報を見る」
;; Get info (defun my-dired-do-getinfo (&optional arg) "Show Finder's information window of the marked files. If no files are marked or a specific numeric prefix arg is given, the next ARG files are used. Just \\[universal-argument] means the current file." (interactive "P") (let ((files (dired-get-marked-files nil arg)) (script (concat "on run argv\n" " tell application \"Finder\"\n" " activate\n" " end tell\n" " repeat with i in argv\n" " set i to (i as POSIX file as Unicode text)\n" " tell application \"Finder\"\n" " open information window of item i\n" " end tell\n" " end repeat\n" "end run\n"))) (apply 'start-process "osascript-getinfo" nil "osascript" "-e" script files))) (eval-after-load "dired" '(define-key dired-mode-map "\M-i" 'my-dired-do-getinfo)) ; tab-to-tab-stop
Dired で Finder の「オリジナルを表示」
;; Reveal in Finder (defun my-dired-do-reveal (&optional arg) "Reveal the marked files in Finder. If no files are marked or a specific numeric prefix arg is given, the next ARG files are used. Just \\[universal-argument] means the current file." (interactive "P") (let ((files (dired-get-marked-files nil arg)) (script (concat "on run argv\n" " set itemArray to {}\n" " repeat with i in argv\n" " set itemArray to itemArray & (i as POSIX file as Unicode text)\n" " end repeat\n" " tell application \"Finder\"\n" " activate\n" " reveal itemArray\n" " end tell\n" "end run\n"))) (apply 'start-process "osascript-reveal" nil "osascript" "-e" script files))) (eval-after-load "dired" '(define-key dired-mode-map "\M-r" 'my-dired-do-reveal)) ; move-to-window-line
動作確認: GNU Emacs 22, Mac OS X 10.6
日本語や濁点の付いたファイル名に対しても動作しています。
手元の GNU Emacs 22 (Carbon) では「名\称\\未\\\設"定""フ"""ォルダ」のような悪魔的フォルダ名に対しては Dired がファイル名の取得に失敗します。これは Dired の問題です。
osascript の引数処理の関係で Mac OS X 10.4 以上が必要と思われます。
Terminal からゴミ箱に捨てる方法はこちら。 コマンドラインからゴミ箱に捨てるシェルスクリプト。