How can I remove leading/trailing whitespace from filenames in Automator?

Solution 1:

for f in "$@"; do new="$(echo -n "$f" | sed -E 's|/$||;s| +$||;s|^ +||;s|/ +([^/]+$)|/\1|;s| +(\.[^.][a-zA-Z0-9.]*)$|\1|g')"; mv "$f" "$new"; echo "$new"; done

(Paste as a Run Shell Script action and select Pass input: as arguments.)

Solution 2:

There's one line script for that, cd to that folder and ...

for f in *; do mv "$f" "$(echo $f)"; done

Put it into a shell script and call it from Applescript:

tell application "Terminal"
    do script "whatever"
end tell