Using Quick Look from the terminal
Posted on April 14th, 2008 in Bash, Mac OS X | No Comments »
Leopard users know how useful Quick Look can be, but using it from the command line is harder than it should be. After looking at other people’s solutions, I decided to write my own.
#!/bin/bash
#
# ql(1)
#
# Quick Look command for terminal use
#
if [[ ${#} -lt 1 || ${1} == "-h" || ${1} == "--help" ]]; then
echo -e "Usage: ql [options] "
echo -e "\t-t\tForce text mode"
else
if [[ ${1} == "-t" ]]; then
shift
qlmanage -p -c public.plain-text "${@}" >& /dev/null &
else
qlmanage -p "${@}" >& /dev/null &
fi
PID=${!}
PID_IN_USE=1
while [ ${PID_IN_USE} ]; do
PID_IN_USE=`ps | awk '{ print $1 }' | grep ${PID}`
read -sn 1 -t 1
if [[ ${?} -eq 0 && ${PID_IN_USE} ]]; then # user quit via GUI
kill ${PID}
exit 0
fi
done
fi
Besides saving on typing, this script has a few other advantages over the qlmanage command:
- It automatically returns the command line to you if you close Quick Look with the GUI (after a short delay)
- You can force plaintext mode for text files that don’t have generators configured (for example, CSS files by default) with the -t option.
- You can hit any key to close the Quick Look window.