10.5: Automatically Quick Look certain downloaded files

Mar 26, '08 07:30:04AM

Contributed by: Anonymous

I have wanted to Quick Look files directly from the browser for a while (especially .doc and .ppt files). A switch to Firefox recently exacerbated this problem, as there is no PDF plug-in. So I wrote a simple script that monitors the Downloads directory and opens any new files with qlmanage, and thought others might find it useful. Copy and paste the code below into a file (say ql_downloaded.sh) and make it executable (chmod a+x ql_downloaded.sh). Launch the script in a separate Terminal window, then start browsing.

#!/bin/zsh

downloaddir="/Users/amol/Desktop/Downloads"
difffile=$downloaddir/.ql_downloaded_diff_file
difffiletemp=$downloaddir/.ql_downloaded_diff_file.temp
/bin/rm -f $difffile
/bin/ls $downloaddir > $difffile

while :
do
  if [[ $downloaddir -nt $difffile ]]; then
    echo "-- directory has been updated"
    sleep 1 # must wait for a few seconds, otherwise ".part" files cause issues

    /bin/mv $difffile $difffiletemp
    /bin/ls $downloaddir > $difffile

    for i in `diff $difffile $difffiletemp | grep '^<' | sed 's/< //'` # find all new files
    do
      if [[ $i == *.(pdf|ppt|xls|doc) ]]; then
        echo "-- quicklook #$i#"
        qlmanage -p "$downloaddir/$i" 2>/dev/null
      else
        echo "-- not calling quicklook on downloaded file $i"
      fi
    done
   fi
   sleep 1
done
This bit -- if [[ $i == *.(pdf|ppt|xls|doc) ]]; then -- restricts which files are Quick Looked. In Firefox, the handling option for these types of files should be set to Save to Disk. The above code should work with most any browser; it works most of the time for me, but there are still some random issues once in a while (probably due to the temporary ".part" files created during downloads). Hope you find this useful!

[robg adds: This worked for me in testing with Firefox and Safari.]

Comments (6)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20080318151932894