I am creating a service for finder, using automator. The service has only one item: "run shell script" and the script is this:
currentDirectory=$(pwd)
for f in "$@"; do
DIRNAME="$(dirname "$f")"
export width=$( mdls "$f" | grep kMDItemPixelWidth | tail -n1 | cut -d= -f2 )
export height=$( mdls "$f" | grep kMDItemPixelHeight | tail -n1 | cut -d= -f2 )
oneWidth=$((width / 3))
oneHeight=$((height / 3))
twoWidth=$((umWidth * 2 ))
twoHeight=$((umHeight * 2 ))
IFS='@3x' read -ra NAMES <<< "$f" #Convert string to array
basename=${NAMES[0]}
extension="${f##*.}"
fullnameOne=${NAMES[0]}.$extension
fullnameTwo=${NAMES[0]}@2x.$extension
sips -z "$oneWidth" "$oneHeight" "$f" --out "$DIRNAME"/"$fullnameOne"
sips -z "$twoWidth" "$twoHeight" "$f" --out "$DIRNAME"/"$fullnameTwo"
done
The idea is this:
- I right click on a bunch of images which names contain
@3x, like [email protected], [email protected], etc. - I want the script to generate
@2xand@1xversions of the files, renaming them [email protected], [email protected], file.png and ball.jpg respectively. - The script has to respect the format. If the file is PNG, the output should be PNG and the same for JPG.
This script you see above runs from terminal, but not from automator. On automator the script generates just one file called Users.
What is wrong?
IFSdoesn't work the way you think it does. Check the value of$fcompared to the resulting value of$basename.