Using Quick Look from the terminal

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] <file>'
  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.

Download ql here.

Like this post? You might also like Coalmine, my centralized error tracking service for your apps. Coalmine captures errors and all kinds of helpful debugging information, notifies you, and makes it all searchable. Check it out!

Tags: ,

Leave a comment