Skip to main content
2 of 3
In actual fact we only want to trim the base name, not the extension.
Brett Ryan
  • 793
  • 3
  • 10
  • 17

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 "$f")
    fn=$(basename "$f")
    bn=${fn%.*}
    ex=${fn##*.}
    bn="$(echo $bn | sed -E -e 's/ +$//' -e 's/^ +//')"
    mv "$f" "$pn"/"$bn"."$ex"
    echo "$pn"/"$bn"."$ex"
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".

Brett Ryan
  • 793
  • 3
  • 10
  • 17