Shortcut to Access the Preview Application from Terminal using AppleScript

If you ever wanted to open PDF, JPEG, PNG files etc.

#!/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

To use the script save it somewhere and run the following commands on the terminal in order to make it executable and move it to the directory of executables:

chmod +x preview
sudo mv preview /bin/preview

resources

Comments