For a correct answer that basis on that of Tuan's answer the following is needed which will not only rename the file but also preserves the action chain for following actions.
Take note also that we don't want to rename the parent folder in any way, just the files.
for f in "$@" ; do
pn=$dirname=$(dirname "$f")
fn=$basename=$(basename "$f")
bn=$name=${fn%basename%.*}
ex=$ext=${fn##*basename##*.}
bn="$name="$(echo $bn"$name" | sed -E -eEe 's/^ +$+//' -e 's;s/^ ++$//')"
new="$dirname/$name.$ext"
mv "$f" "$pn"/"$bn"."$ex""$new"
echo "$pn"/"$bn"."$ex""$new"
done
You also don't need to put this into a shell script itself, automator can have this embedded into the action "Run Shell Script".