4

My wife tediously renames files all the time when storing on her backup external storage, so I investigated using automator to do this for her. I now have an automator folder where she just drops the file in there and it will rename the file appropriately.

I'm now faced with a situation where the file may now contain leading and/or trailing whitespace, how might I simply trim this whitespace?

I've never really used AppleScript but am proficient in shell scripting, can I simply write a shell script that will perform this task? I will give AppleScript a go if someone could point me in the right direction.

2
  • Just write a shell script that will do this, as you had suggested and in Automator you can run shell scripts. In Automator look for Run Shell Script. Commented Oct 30, 2011 at 21:19
  • I was also looking to remove trailing whitespace from file superuser.com/questions/405127/… Commented Sep 27, 2012 at 16:39

5 Answers 5

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.)

6
  • Interesting, I like how you changed the variable names in mine to make it easier to read yet you put everything in a single regex in yours. One thing you might need to consider though is that we don't want spaces removed from existing paths, while they shouldn't be there that would actually cause the Automator script to fail.
    – Brett Ryan
    Commented Nov 1, 2011 at 3:10
  • Ah wait, my bad, you only capture the last / in the group. Okay, you win :)
    – Brett Ryan
    Commented Nov 1, 2011 at 3:13
  • Change your regex to this s|/$||;s| +$||;s|^ +||;s|/ +([^/]+$)|/\1|;s| *\. *([^\.]+)$|\.\1|; We may allow other characters in the extension and we also don't want the extension starting with a space.
    – Brett Ryan
    Commented Nov 1, 2011 at 3:49
  • s| *\. *([^.]+)$|\.\1| would replace EOL + . + LF.plist with EOL +.+ LF.plist. It wouldn't affect some .tar.gz.
    – Lri
    Commented Nov 1, 2011 at 5:43
  • Point noted, though neither does your regex. Actually, the extra period character looks to be in error before the group inclusion as it replaces a space with a period (some .tar.gz becomes some..tar.gz). Removing the period works, but doesn't prevent the space in the extension from appearing at the start. Suffice to say it doesn't matter too much, these things are more trivial and can be questioned forever.
    – Brett Ryan
    Commented Nov 1, 2011 at 13:51
1

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
9
  • This only works for filenames with 4 leading blanks :-(
    – nohillside
    Commented Oct 30, 2011 at 13:05
  • for f in *; do mv "$f" "echo $f | cut -b 1-"; done man cut Commented Oct 30, 2011 at 13:19
  • for f in *; do mv "$f" "$(echo $f)"; done then, doesn't have anything to do with cut at all.
    – nohillside
    Commented Oct 30, 2011 at 14:25
  • Thanks guys, but how does this work? Wouldn't automator call the script with an argument of the files? Wouldn't for f in "$@"; do echo mv "$f" "$(echo $f)" ; done be more appropriate?
    – Brett Ryan
    Commented Oct 30, 2011 at 14:51
  • 1
    @frozenwithjoy: This fails if $f contains blanks inbetween as well, e.g. " foo bar ".
    – nohillside
    Commented Oct 30, 2011 at 16:52
1

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
    dirname=$(dirname "$f")
    basename=$(basename "$f")
    name=${basename%.*}
    ext=${basename##*.}
    name="$(echo "$name" | sed -Ee 's/^ +//;s/ +$//')"
    new="$dirname/$name.$ext"
    mv "$f" "$new"
    echo "$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".

1

I was also looking to remove trailing whitespace from file and folder names. This Super User post helped me out.

0

There's an AppleScript (very similar to Automator) that trims file names. It's a bit long, so I can't post it here, but here is a link to the code. Make sure you read the beginning part of the code, as it explains what the script does and how it works.

1
  • 1
    Had a look at the script, now I don't know whether I should laugh or cry...
    – nohillside
    Commented Oct 30, 2011 at 16:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .