I have this automator script that is to be a finder service to process images. The idea is:
- I select a bunch of images
- I right click and choose the script on Finder services
- the script asks me if I want to convert the image to 100x100, 200x200 or 300x300. I can choose multiple options.
Here is what I did: first, a module in applescript that asks me for the sizes I want:
on run
choose from list {"100", "200", "300"} with prompt "What size(s) do you want? (multiple sizes allowed)" with multiple selections allowed
set choice to the result as string
return choice
end run
because this service receive image files, and the following bash script is receiving a list of arguments, this choice variable will be the first on "$@". I need to remove it as the bash script (the next part) starts.
Here is the bash script:
#!/bin/bash
# here is the parameters passed to the functions
# $1 path to the file to be processed
# $2 original image name
# $3 original image directory
function f100 {
sips -z 100 100 "$1" --out "$3"/"$2"_file100.png
sips -z 200 200 "$1" --out "$3"/"$2"[email protected]
}
function f200 {
sips -z 200 200 "$1" --out "$3"/"$2"_file200.png
sips -z 400 400 "$1" --out "$3"/"$2"[email protected]
}
function f300 {
sips -z 300 300 "$1" --out "$3"/"$2"_file300.png
sips -z 600 600 "$1" --out "$3"/"$2"[email protected]
}
choice=$1
shift
for f in "$@"; do
DIRNAME="$(dirname "$f")"
filename=$(basename "$f")
name="${filename%.*}"
extension="${filename##*.}"
if [[ $choice == *"100"* ]]
then
f100 $f $filename $DIRNAME
fi
if [[ $choice == *"200"* ]]
then
f200 $f $filename $DIRNAME
fi
if [[ $choice == *"300"* ]]
then
f300 $f $filename $DIRNAME
fi
done
The Applescript part passes the choice as a string to the script. So, if I choose 100 and 200 it will pass a string "100200". If I choose all 3 options, it will pass "100200300", this is why I am searching for the substring on the ifs and running the proper functions.
This script works perfectly from terminal but does nothing when installed as a service on finder. No error, nothing.
