LoginSignup
5
3

More than 5 years have passed since last update.

ターミナルにクリップボードの中身を貼り付けたときに確認画面を表示させる(Mac編)

Posted at

実現したいこと

Windowsのteratermにはターミナルにalt+vなどで貼り付けの動作をしたときに、クリップボードの中身を表示させる機能があります。
一般的な設定では、クリップボード内が複数行の場合に表示されると思います。

スクリーンショット 2014-12-21 18.01.06.jpg

teratermのようにMacのターミナルでも、複数行の貼り付けをした際に確認画面が表示されるようにする方法を紹介します。※今回はiTerm2での設定方法を紹介します。

但し、teratermのように確認画面内で内容の修正ができるというところまでは残念ながらできませんのでご了承ください。

Windowsでの実現方法は以下に投稿しました。
ターミナルにクリップボードの中身を貼り付けたときに確認画面を表示させる(Windows編)

automatorの設定

automatorにApplescriptを記述しworkflowとして登録することで実現します。

automatorを起動後新規作成にてサービスを選択します。
スクリーンショット 2014-12-21 19.25.24.jpg

左側に表示されているライブラリから、 「Applescriptを実行」を選択し、右側の領域にD&Dすると以下のような状態になります。
スクリーンショット 2014-12-21 19.27.05.jpg

スクリプトの内容は以下のとおりです。
以下の内容を記述後、適当な名前をつけてworkflowを保存してください。
例として「checkClipboardMultiline」という名前を付けたとします。
以下に保存されます。
~/Library/Services/checkClipboardMultiline.workflow

on is_running(appName)
    tell application "System Events" to (name of processes) contains appName
end is_running

--iTermの貼り付けのショートカットキーをcontrol+shift+vにしておかないと keystroke "v" using  {control down, shift down} が有効にならず貼り付けできないので注意
--最前面のアプリケーション名を取得
tell application "System Events"
    set frontApp to name of (path to frontmost application)
end tell

--iTermの起動状態を確認
set safRunning to is_running("iTerm")

if safRunning then
    --最前面のアプリがiTermの場合
    if frontApp = "iTerm.app" then
        tell application "iTerm"
            -- クリップボードが文字の場合
            set strOfClip to clipboard info for string
            if strOfClip is not {} then

                --クリップボードの中身をすべて格納
                set tmpClipboard to get the clipboard
                --クリップボードの中身を1行ずつ格納
                set strClipboard to every paragraph of tmpClipboard
                --行数を格納
                set clipboardRowCount to 1

                repeat with val in strClipboard
                    --クリップボードの中身が2行以上の場合
                    if clipboardRowCount ≥ 2 then
                        display dialog tmpClipboard & "


上記の内容を貼り付けますか?" with icon caution default button 1
                        tell i term application "System Events"
                            keystroke "v" using {control down, shift down}
                        end tell
                        return
                    end if
                    set clipboardRowCount to clipboardRowCount + 1

                end repeat
                --クリップボードの中身が1行の場合
                tell i term application "System Events"
                    keystroke "v" using {control down, shift down}
                end tell
                return
            end if
        end tell
    end if
end if

iTermの設定

標準の貼付け動作のショートカットキーはcmd+vなので、このショートカットキーにてautomatorのworkflowを動作させるためにiTermのショートカットキーの割り当ての変更を行います。

システム環境設定 - キーボード より、以下の設定を行います。

スクリーンショット 2014-12-21 19.54.32.jpg

これで、iTermのPasteの動作がctrl+shift+vに割り当てられます。

BetterTouchToolの設定

automatorで作成したworkflowをショートカットキーに割り当てるためにBetterTouchToolを利用します。
Keyboardを選択し、iTermのショートカットキー割り当て画面を表示します。
Add New Shortcut を押下し、ショートカットキーを以下のように追加します。
Trigger Predefine Actionには、Controlling Other Appkications - Start Automator Workflowを選択し、先ほど作成したworkflowを選択します。ライブラリディレクトリが見えない場合はこちらを参照して設定してください。

スクリーンショット 2014-12-21 19.56.54.jpg

動作確認

複数行の貼り付けをしようとすると以下のように表示されるようになります。
スクリーンショット 2014-12-21 20.11.22.jpg

動作の流れは以下のとおりです。
・BetterTouchToolに割り当てたcommand+vにて、automatorのworkflowを実行
・workflowのApplescriptにて、クリップボードの中身を確認し判定
・AppleScript内のkeystroke "v" using {control down, shift down}によって、iTermのctrl+shift+vが実行される
・ctrl+shift+vを割り当てたPaste動作を実施

問題点

・貼り付け内容が1行の場合でもショートカットキー押下してから実際に内容がターミナル上に表示されるまでタイムラグがあるので正直微妙かもです。。

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3