Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Shell script to move a folder of correctly-named image files into their respective .xcassets subfolders
#!/bin/sh
function moveAssetImages
{
usage="usage: moveAssetImages /path/to/folderOfImages /path/to/Images.xcassets"
sourceDir=$1
assetsDir=$2
if [ $# != 2 ]; then
echo $usage
return
fi
if [[ ! -d $sourceDir ]]; then
echo "Directory not found: $sourceDir"
return
fi
if [[ ! -d $assetsDir ]]; then
echo "Directory not found: $assetsDir"
return
fi
echo $assetsDir
if [[ `echo $assetsDir | awk -F . '{print $NF}'` != "xcassets" ]]; then
echo $usage
return
else
for image in $sourceDir/*
do
filename=`basename $image`
extension=`echo $filename | awk -F . '{print $NF}'`
nameWithoutExtension=`basename $image .$extension`
# strip suffixes like ~iPad
if [[ $nameWithoutExtension == *"~"* ]]; then
deviceSuffix=`echo $nameWithoutExtension | sed 's/.*~//'`
nameWithoutExtension=`basename $nameWithoutExtension "~$deviceSuffix"`
fi
# strip suffixes like @2x
nameWithoutExtension=`basename $nameWithoutExtension @2x`
# move files into place
if [[ -e $assetsDir/$nameWithoutExtension.imageset/$filename ]]; then
cp $sourceDir/$filename $assetsDir/$nameWithoutExtension.imageset/$filename
fi
done
fi
}
moveAssetImages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.