Last active
December 28, 2015 12:59
-
-
Save vegaasen/7504361 to your computer and use it in GitHub Desktop.
Simple sips script to convert multiple files in directories. This is just a script that I use in regards to uploading images to e.g forums or facebook. Only works on MAC (SIPS).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Simple script that performs SIPS-operations on mac. This script is created to simplify the actions in regards to sips, and also so that one dont have to | |
# remember what to do each time regarding the command I always tend to forget..duh.. | |
# ENjoi! | |
# | |
# @version 0.1b | |
# @author vegaasen | |
# @since 16.11.2013 | |
default_quality="normal"; | |
default_out="/tmp/smallified"; | |
location=$1; | |
size=$2; | |
pattern=$3; | |
quality=$4; | |
outLocation=$5; | |
flip=$6; | |
state=1; | |
log_level="info"; | |
function log() { | |
log=$1; | |
if [ -z "$log" ]; then | |
echo "-"; | |
fi | |
if [ $log_level = "debug" ]; then | |
log="[DEBUG][`date`] $log"; | |
else | |
log="[INFO] $log"; | |
fi | |
echo $log; | |
} | |
function printDecition() { | |
echo "Do you want to continue? [y,n]"; | |
read cont; | |
if [ -z "$cont" ]; then | |
echo "[y,n]"; | |
read cont; | |
fi | |
if [ -z "$cont" ]; then | |
cont="n"; | |
fi | |
if [ $cont = "y" ]; then | |
return; | |
else | |
log "Cancelled."; | |
exit -2; | |
fi | |
} | |
function assertRequiredAttributes() { | |
if [ -z "$location" ] || [ -z "$size" ] || [ -z "$pattern" ]; then | |
log "Required attributes (location, size, pattern) cannot be undefined."; | |
state=0; | |
fi | |
if [ -z "$quality" ]; then | |
log "Quality uses default value. Set to $default_quality."; | |
quality=$default_quality; | |
fi | |
if [ -z "$outLocation" ]; then | |
log "Save-location not set - using default value. Set to $default_out."; | |
outLocation=$default_out; | |
fi | |
if [ ! -d "$outLocation" ]; then | |
log "The defined outputlocation does not exists or is not a directory. Creating it now."; | |
printDecition; | |
mkdir $outLocation; | |
fi | |
if [ ! -w "$outLocation" ]; then | |
log "Missing write-rights. Will try to add missing permissions."; | |
printDecition; | |
chmod u+rw $outLocation; | |
fi | |
if [ ! ${outLocation:${#outLocation} - 1} = "/" ]; then | |
outLocation="$outLocation/"; | |
log "Formatted outLocation ($outLocation)."; | |
fi | |
if [ ! ${location:${#location} - 1} = "/" ]; then | |
location="$location/"; | |
log "Formatted location ($location)"; | |
fi | |
} | |
function printUsage() { | |
log "Usage: smallify.sh <location> <size> <pattern> <(opt)outLocation> <(opt)qualit[low,normal,high,best]> <(opt)flip[vertical,horizontal]>"; | |
log "Example: ./smallify.sh /Users/vegardaasen/develop/github/vegaasen_com/vegaasen_com_version5/resources/img/interrests/ 400 *.jpg"; | |
} | |
function sipsify() { | |
command="sips -Z $size --setProperty formatOptions \"$quality\""; | |
if [ ! -z "$flip" ]; then | |
command="$command --flip $flip"; | |
fi | |
command="$command $location$pattern --out $outLocation"; | |
log "Executing command {$command}"; | |
$command; | |
} | |
function doTheMagic() { | |
assertRequiredAttributes; | |
if [ $state = 0 ]; then | |
log "Mandatory attributes were not set. Cancelled."; | |
printUsage; | |
exit -1; | |
else | |
log "Using the following attributes:"; | |
log "Location: $location"; | |
log "Pattern: $pattern"; | |
log "Downsize-to: $size"; | |
log "Quality: $quality"; | |
log "Flip/Rotate: $flip"; | |
fi | |
printDecition; | |
sipsify; | |
log "Sipsify have now completed the conversion."; | |
open $outLocation; | |
} | |
log "Initializating."; | |
doTheMagic; | |
log "Completed."; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment