Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 179 lines (155 sloc) 5.422 kB
#!/bin/bash
##############################################################################
# File : iPhoto-cleaner
# Author : Guillaume-Jean Herbiet <guillaume@herbiet.net>
#
#
# Copyright (c) 2010 Guillaume-Jean Herbiet (http://www.herbiet.net)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Guillaume-Jean Herbiet
# <guillaume.herbiet@uni.lu>
# University of Luxembourg
# 6, rue Richard Coudenhove-Kalergi
# L-1359 Luxembourg
##############################################################################
# Location of the iPhoto Library
user=$(whoami)
iPhotoLibraryLocation="/Users/$user/Pictures/iPhoto Library"
# Echo command
echo_cmd="logger -i -t \"$0\""
#echo_cmd="echo"
# Print a help message
usage()
{
echo "$0: a bash script to replace original files by"
echo "modified files in your iPhoto library."
echo "In standard mode, a backup of the modified files is created on your desktop."
echo "usage: $0 [-h] [-l <iPhoto library path>] [-u]"
echo " -h : prints this help message"
echo " -l : iPhoto library location (default: $iPhotoLibraryLocation)"
echo " -u : launch the unsafe mode (no backup folder is created on your desktop)"
exit 0
}
# Test if OSXUtils are installed on the system
if [ $(which hfsdata | wc -l) -lt 1 ]
then
echo "This program requires hfsdata. Please go to http://osxutils.sourceforge.net/"
echo "to install this tool or type \"sudo port install osxutils\" if you use MacPorts."
exit 1
fi
# Parse the command-line arguments
unsafe="0"
while getopts "hul:" OPTION
do
case $OPTION in
h)
usage;
die;
;;
l)
iPhotoLibraryLocation="$OPTARG"
;;
u)
unsafe="1"
;;
esac
done
# Test if the user requested an unsafe cleaning (i.e without backup)
# otherwise, set the proper directories for backup
if [ "$unsafe" == "1" ]; then
unsafe="1"
warning="Performing cleaning in UNSAFE mode!"
$echo_cmd $warning
echo $warning
else
unsafe="0"
# Get the current user identity
# and create a backup directory
backup_dir="/Users/$user/Desktop/iPhoto-cleaner"
if [ ! -d "/Users/$user/Desktop/iPhoto-cleaner" ]; then
mkdir -p "$backup_dir/Originals"
mkdir -p "$backup_dir/Modified"
fi
fi
# Store the foldee currently processed
# (starting with iPhoto '09, this is no longer meaningful, but it still helps
# following the progress of the script)
current_folder=""
# Look into the Modified folder for pictures
# this is safe for filename with spaces
find "$iPhotoLibraryLocation/Modified" -type f | while read filename
do
# Update the current folder
if [ "$current_folder" != "$(dirname "$filename")" ]; then
if [ "$current_folder" != "" ]; then
echo ""
fi
current_folder=$(dirname "$filename")
echo "Processing $current_folder "
fi
# Skip the files that are already aliases
if [ "$(hfsdata -k "$filename")" != "Alias" ]; then
# The original file we are looking for
original="${filename/Modified/Originals}"
# Test if the orginial file is really existing
# (this caches the case of movies for which a JPG snapshot has been
# created in "Modified")
if [ -f "$original" ]; then
# Now this file can be an alias
# (pictures are not copied to the iPhoto Library on import)
if [ $(hfsdata -k "$original") == "Alias" ]; then
# Acquire the alias target using OSXUtils
target=$(hfsdata -e "$original")
# Test if the target file is really existing
# if so, set the target as original
if [ -f "$target" ]; then
original="$target"
else
$echo_cmd "Target file $target of alias $filename does not exists, skipping this file."
original=""
fi
fi
# Now copy the modified file over the original file
# and create a symbolic link from the modified to the original
if [ "$original" != "" ]; then
# In safe mode, prepare the backup directories
# and copy modified and originals to respective backup places
if [ $unsafe == "0" ]; then
basename=${filename/"$iPhotoLibraryLocation"\/Modified}
# Create the subdirectories if requested
if [ ! -d "$backup_dir/Modified$basename" ]; then
mkdir -p "$(dirname "$backup_dir/Modified$basename")"
fi
if [ ! -d "$backup_dir/Originals$basename" ]; then
mkdir -p "$(dirname "$backup_dir/Originals$basename")"
fi
cp -f "$filename" "$(dirname "${filename/"$iPhotoLibraryLocation"/$backup_dir}")"
cp -f "$original" "$(dirname "$backup_dir"/Originals"$basename")"
fi
cp -f "$filename" "$(dirname "$original")"
rm -r "$filename"
mkalias -r "$original" "$filename"
fi
else
$echo_cmd "No original file matching $filename, skipping this file."
fi
# Original file is already an alias, we skip it
else
$echo_cmd "$filename has already been processed, skipping this file."
fi
echo -n "."
done;
echo ""
Jump to Line
Something went wrong with that request. Please try again.