Helical Alpha Code

fpath: cd into finder


#!/bin/bash
#####################
#
# name: fpath (fpath.sh)
# vers: 0.1a
# date: 062308
# first: 062308
# auth: "Keith Beckman" <kbeckm@alphahelical.com>
# site: http://alphahelical.com/code/osx/fpath
# desc: fpath returns the path to the front Finder window, or the
# paths to all Finder windows.
#
# todo:
#
#####################

####configuration####

verbosity=1
verstring='fpath v0.1a 062308 keith beckman'
tempdir=/tmp
SIG_NO_WINDOWS=SIGUSR1

##end configuration##

echo_usage () {
 cat >&2 <<EOF
Usage: fpath [-ahqVv]
 fpath returns the path to the front Finder window, or the
 paths to all Finder windows.

 -a|--all lists all open Finder windows
 -q|--quiet disables error reporting
 -V|--verbose enables verbose listings and activity reporting
 -v|--version displays the version string
 -h|--help displays this help text
EOF
 }

report () {
# Usage: report message [verbosity_threshold]
 if [[ -n "${1}" && ${verbosity:-1} -gt ${2:-1} ]]; then
  printf "(fpath): %s\n" "${1}" >&2
 fi
 }

report_error () {
#Usage: report_error message
 report "Error: ${1:-uknown error}" 0
 }

trap 'report_error "No active Finder windows."; exit -1' $SIG_NO_WINDOWS

####option processing####

while [[ -n "${1}" ]]; do
 if [[ "${1:0:2}" == '--' ]]; then
  case ${i#--} in
   all)
    shopt=a
    ;;
   quiet)
    shopt=q
    ;;
   verbose)
    shopt=V
    ;;
   version)
    shopt=v
    ;;
   help|usage)
    shopt=h
    ;;
   '')
    shopt=-
    ;;
   *)
    report_error "Unknown long option '${1}'"
    echo_usage
    exit -1
    ;;
  esac
  args="$args -${shopt}"
 elif [[ "${1:0:1}" == '-' ]]; then
  args="$args ${1}"
 else
  args="$args '${1}'"
 fi
 shift
done

report "Constructed argument list: ${args}" 3
args=$(getopt 'aqVvh' $args 2> "${tempdir}/$$.errors")
getopt_rv=$?
report "Parsed argument list: ${args}" 3

if [[ $getopt_rv != 0 || "${args:1}" == '--' ]]; then
 if [[ $getopt_rv != 0 ]]; then
  sed 's/^getopt:/fpath (getopt):/' < "${tempdir}/$$.errors"
  echo_usage
  exit $getopt_rv
 fi
 rm "${tempdir}/$$.errors"
else
 eval "set -- ${args}"
 while [[ -n "${1}" ]]; do
  case "${1#-}" in
   a)
    mode=a
    ;;
   q)
    verbosity=0
    report "verbosity=$verbosity"
    ;;
   V)
    verbosity=2
    report "verbosity=$verbosity"
    ;;
   v)
    report "${verstring}" 0
    exit
    ;;
   h)
    echo_usage
    exit
    ;;
   -)
    shift
    break
    ;;
   *)
    report_error "Unknown option '${1}'."
    exit -1
    ;;
  esac
  shift
 done
 
 while [[ -n "${1}" ]]; do
  report_error "Option '${1}' unknown."
  exit -1
 done
fi

function get_front_finder_window () {
 osascript <<EOF
set out to ""
try
 tell application "Finder"
  set out to posix path of ((folder of the front window) as string)
 end tell
on error
 do shell script "kill -$SIG_NO_WINDOWS $$ 2> /dev/null"
 return
end try
return out
EOF
 }

function get_all_finder_windows () {
 (osascript <<EOF
tell application "Finder"
 set out to ""
 set theWindows to Windows
 repeat with theWindow in theWindows
  set out to out & posix path of ((folder of theWindow) as unicode text) & "\n"
 end repeat
end tell
if out is ""
 do shell script "kill -$SIG_NO_WINDOWS $$"
 return
else
 return out
end if
EOF
)
 }

case "${mode}" in
 a)
  get_all_finder_windows
  ;;
 *)
  get_front_finder_window
  ;;
esac


Generated by GNU enscript 1.6.1 and enscriptclean.

fpath | Download Source | View Source