1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Finder 最前面フォルダパス取得のスクリプト(AppleScript 利用)

Last updated at Posted at 2015-09-06

こんにちは。
AppleScript(osascript コマンド)を使って Finder 最前面フォルダのパス取得してみました。シェルスクリプト(オプションで pbcopy も実行)と Ruby で書いてみました。

$ ./findertopwindow.sh
/Users
$ cd `./findertopwindow.sh`
findertopwindow.sh
# !/bin/sh
DIR_FRONTMOST=`
  osascript << SCRIPT
  tell application "Finder" to set folder_frontmost to (target of window 1) as string
  if folder_frontmost is not "" then set folder_frontmost to POSIX path of folder_frontmost
  return folder_frontmost
SCRIPT`

echo "${DIR_FRONTMOST}"
if [ "${DIR_FRONTMOST}" != "" ]; then
  for OPT in $*
  do
    case $OPT in
      "--pbcopy" ) echo "${DIR_FRONTMOST}" | tr -d "\n" | pbcopy ; shift 1 ;;
    esac
  done
fi
exit 0

なお、スクリプト内に cd コマンドを書いている場合(下記)は、source コマンド(.)でスクリプトを動かす必要があります。

$ . cd_findertopwindow.rb
cd_findertopwindow.rb
# !/usr/bin/ruby
DIR_FRONTMOST=`
  osascript << SCRIPT
  tell application "Finder" to set folder_frontmost to (target of window 1) as string
  if folder_frontmost is not "" then set folder_frontmost to POSIX path of folder_frontmost
  return folder_frontmost
SCRIPT`

# puts "#{DIR_FRONTMOST}"
if "#{DIR_FRONTMOST}" != ""
  system("cd #{DIR_FRONTMOST}")
end
exit(0)
1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?