Skip to content

Instantly share code, notes, and snippets.

@ttscoff
Created November 2, 2013 16:28
Show Gist options
  • Save ttscoff/7280716 to your computer and use it in GitHub Desktop.
Save ttscoff/7280716 to your computer and use it in GitHub Desktop.
Open File Type script. Fuzzy matches extension fragment to open matching files.
oft() {
local target fnd app all
target="."
fnd=''
if [[ $# == 0 ]]; then
echo -n "Enter an ext or partial ext: "
read ext
else
[[ "$1" =~ -[h\?] ]] && echo "Usage: oft [-a \"app name\"] [path (fragments)] file extension (fragment)" && return
while [[ $# -gt 1 ]]; do
if [[ "$1" == "-a" ]]; then
app="$2"
shift
shift
elif [[ "$1" == "-all" ]]; then
all=true
shift
else
fnd+="$1 "
shift
fi
done
ext=$1
fi
if [[ $fnd != '' && !all ]]; then
if [[ -d "${fnd%% *}" ]]; then
cd "${fnd%% *}" >> /dev/null
target="`pwd`"
cd - >> /dev/null
else
target=$(ruby <<SCRIPTTIME
if (File.exists?(File.expand_path('~/.oftlist')))
query = "$fnd".strip
input = File.open(File.expand_path('~/.oftlist'),'r').read
re = query.gsub(/\s+/,' ').split(" ").join('.*?')
res = input.scan(/.*?#{re}.*?$/i)
unless res.nil? || res.empty?
out = res.uniq.sort[0]
exit if out =~ /^\s*$/
print out
end
end
SCRIPTTIME
)
if [[ "$target" =~ ^\s*$ ]]; then
echo "No match found for directory: $fnd"
return
fi
fi
fi
CHKFILES=$(ls -C1 "$target/*.*$ext*" 2> /dev/null | wc -l)
if [ $CHKFILES -ne 0 ]; then
echo "No files matching \"$ext\" found."
return
fi
if [[ $all ]]; then
[[ $target == "." ]] && target="`pwd`"
echo "${target%/}" >> ~/.oftlist
sort -u ~/.oftlist -o ~/.oftlist
if [[ $app ]]; then
$(/usr/bin/open -a "$app" $target/*\.*$ext*)
else
$(/usr/bin/open $target/*\.*$ext*)
fi
return
fi
declare -a fileList=( $target/*\.*$ext* )
if [[ ${#fileList[*]} -gt 1 ]]; then
IFS=$'\n'
if [[ $app ]]; then
PS3="Open which file in $app? "
else
PS3='Open which file? '
fi
select OPT in "Cancel" ${fileList[*]}; do
if [ $OPT != "Cancel" ]; then
[[ $target == "." ]] && target="`pwd`"
echo "${target%/}" >> ~/.oftlist
sort -u ~/.oftlist -o ~/.oftlist
if [[ $app ]]; then
$(/usr/bin/open -a "$app" "$OPT")
else
$(/usr/bin/open "$OPT")
fi
fi
unset IFS
break
done
else
echo "${target%/}" >> ~/.oftlist
sort -u ~/.oftlist -o ~/.oftlist
if [[ $app ]]; then
$(/usr/bin/open -a "$app" "${fileList[0]}")
else
$(/usr/bin/open "${fileList[0]}")
fi
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment