Open many images in Preview from Terminal

May 18, '04 09:54:00AM

Contributed by: guybrush

Here's a simple script that will allow you to open multiple files in Preview as a collection from the Terminal. This will allow you to browse multiple files in a single Preview window, very handy if you don't like Finder but dig the Terminal :)

Example of usage:

[guybrush@powerbook guybrush]$ cd photos
[guybrush@powerbook photos]$ preview *.jpg *.png
  1. Place the following code in the file preview:
    #!/bin/sh
    # A simple bash script that uses Applescript to
    # open multiple files in Preview. For example:
    # "preview hello.png world.gif *.jpg"
    
    if [ -z "$*" ]; then
      echo "Usage: $0 [FILE(S)]"
      exit
    fi
    
    PWD=${PWD//\//:}
    for file in $*;
    do
      if [ -n "$FILES" ]
      then
        FILES="$FILES, \"$PWD:$file\""
      else
        FILES="\"$PWD:$file\""
      fi
    done;
    osascript<<EOF
    tell application "Preview"
      activate
      open {$FILES}
    end tell
    EOF
    
  2. Make the script executable by typing chmod +x preview
  3. Copy the script to your bin directory for easy access: sudo cp preview /bin/preview
  4. Then execute it by typing: preview *.png
This script only works on files in the current working directory, due to the simplicity of the script.

[robg adds: You can, of course, just type open *.jpg in the Terminal to sort of do the same thing. The main difference is that this script opens one preview window with a drawer; the open method opens a bunch of individual windows. This script worked fine for me on 10.3.3 in tcsh...]

Comments (7)


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