by shigemk2

当面は技術的なことしか書かない

Finderとdiredをリンクさせたい

Emacs, Mac OS X and Unix: Dired からゴミ箱に捨てる、情報を見る、 Finder で表示
またしてもここから。
そしてid:masutaka26から。
ありがとうございます。

下記3つを設定ファイルに書く。

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

Mac OS X 10.4 以上が必要と思われるらしい。
あと、勿論だがEmacsが必須である。
キーバインドはお好みで。