Skip to content

Instantly share code, notes, and snippets.

@CarterA
Created April 23, 2010 21:10
Show Gist options
  • Save CarterA/377181 to your computer and use it in GitHub Desktop.
Save CarterA/377181 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Written on 04/20/10 by Carter Allen
# Copyright Opt-6 Products, LLC, 2010
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# Allowed Arguments
# -i --input Path to the input directory. If omitted, ./ is used.
# -o --output Path to the output directory. If omitted, ./ is used.
# -p --optipng Path to an optipng binary
# -c --compression-level An integer (1 to 7) describing the amount of
# compression to apply to PNGs via optipng. 1
# is fastest, with the lowest compression ratio.
# 7 is the slowest, with the highest compression ratio.
# Default is 7.
# -s --sips Path to the sips utility. Shouldn't ever need to be used.
# -q --quiet Don't print any messages to the console.
# -v --verbose Print status messages to the console for optipng and sips.
# Script
# Set Defaults for Variables
INPUT_DIRECTORY="./";
OUTPUT_DIRECTORY="./";
OPTIPNG="./optipng";
COMPRESSION_LEVEL="7";
SIPS="sips";
QUIET=false;
VERBOSE=false;
# Convert Long Options to Short Options
# Source: http://kirk.webfinish.com/2009/10/
# bash-shell-script-to-use-getopts-with-gnu-style-long-positional-parameters/
for arg
do
delim=""
case "$arg" in
--input) args="${args}-i ";;
--output) args="${args}-o ";;
--optipng) args="${args}-p ";;
--compression-level) args="${args}-c ";;
--sips) args="${args}-s ";;
--quiet) args="${args}-q ";;
--verbose) args="${args}-v ";;
#pass through anything else
*) [[ "${arg:0:1}" == "-" ]] || delim="\""
args="${args}${delim}${arg}${delim} ";;
esac
done
eval set -- $args
# Set Variables to Arguments
while getopts ":i:o:p:c:s:qv" option 2>/dev/null
do
case $option in
i) INPUT_DIRECTORY=${OPTARG[@]};;
o) OUTPUT_DIRECTORY=${OPTARG[@]};;
p) OPTIPNG=${OPTARG[@]};;
c) COMPRESSION_LEVEL=${OPTARG[@]};;
s) SIPS=${OPTARG[@]};;
q) QUIET=true;;
v) VERBOSE=true;;
*) echo $OPTARG is an unrecognized option;;
esac
done
# Sanity Check Arguments
if (($QUIET == $VERBOSE) && ($QUIET == true)) then
VERBOSE=false;
fi
# Run the Conversion!
PSD_FILES=$INPUT_DIRECTORY*.psd;
for PSD_FILE in $PSD_FILES
do
PNG_FILE=${PSD_FILE%.psd}'.png';
echo "$PNG_FILE";
$SIPS -s format png "$PSD_FILE" --out "$PNG_FILE" > /dev/null;
#$OPTIPNG -o $COMPRESSION_LEVEL -q "$PNG_FILE";
done
@mattpat
Copy link

mattpat commented Apr 23, 2010

I don't know how, I don't know why, but I'm just going to chalk this up to sips being a bitch. :P See my fork, basically I just cd into $INPUT_DIRECTORY and iterate over the files from there, doesn't give me any complaints.

@CarterA
Copy link
Author

CarterA commented Apr 23, 2010

Okay! Thanks for the help, I should have thought of that. Would giving you credit in the file header be enough for you? Or do you want me to place an attribution link somewhere...?

@mattpat
Copy link

mattpat commented Apr 23, 2010

Ha ha, I don't care, credit me however you like, or not at all. :P Header is fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment