LoginSignup
10
12

More than 5 years have passed since last update.

[MacOS] TerminalでFinderのカレントディレクトリにcdするワンライナー

Last updated at Posted at 2016-09-13

TerminalとFinderで連携して作業したいことってよくありますよね。
Terminal → Finderの方はopen .で開けますが、みなさん逆はどうやってますか?

現在Finderで最前面にあるウィンドウのカレントディレクトリにTerminal側でcdするワンライナーをAppleScriptを利用して書いてみました。

AppleScriptを利用するとシェルスクリプト側からMacOS固有のアプリケーションにアクセスできて便利ですね。

cd "`osascript -l JavaScript -e \"decodeURIComponent(Application('Finder').windows[0].target().url().replace('file://', '')).replace(/\\"/g, '\\\"');\"`" && pwd

これを適当にfcd.shなどのファイル名で保存して、~/.bash_profileに

.bash_profile
alias fcd='source /path/to/fcd.sh'

などとしておけば、fcdコマンド一発で実行することが出来ます。
(編集のあとはsource ~/.bash_profileを実行して反映させるのを忘れずに!)

$ pwd
/Users/Yuhsak/Documents
$ fcd
/Users/Yuhsak/Documents/QiitaProjects/fcd
$ pwd
/Users/Yuhsak/Documents/QiitaProjects/fcd
10
12
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
10
12