#!/usr/bin/env bash
#
# execute command from every selected Finder directory

set -- repo:files:cache:rm

usage() {
    echo "usage: $(basename $0) command [args ...]" 1>&2
    [ "$1" = "-h" ] || [ "$1" = "--help" ]; exit
}

[ "$1" = "-h" ] || [ "$1" = "--help" ] && usage "$@"

[[ $# == 0 ]] && usage

out="$(osascript "${BASH_SOURCE[0]%/*}"/.finder-selection.applescript 2>&1)" || exit
[[ -z "$out" ]] && {
    out="$(osascript "${BASH_SOURCE[0]%/*}"/.finder-target.applescript)" || exit;
}
[[ -n "$out" ]] && while IFS= read path; do
    [ -d "$path" ] && { cd "$path" || exit; "$@" || exit; }
done <<< "$out";:
