Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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.

share|improve this question
    
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. –  Kassym Dorsel Oct 30 '11 at 21:19
    
I was also looking to remove trailing whitespace from file superuser.com/questions/405127/… –  Joe J Sep 27 '12 at 16:39
add comment

5 Answers 5

up vote 1 down vote accepted

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

share|improve this answer
    
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 Nov 1 '11 at 3:10
    
Ah wait, my bad, you only capture the last / in the group. Okay, you win :) –  Brett Ryan Nov 1 '11 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 Nov 1 '11 at 3:49
    
s| *\. *([^.]+)$|\.\1| would replace EOL + . + LF.plist with EOL +.+ LF.plist. It wouldn't affect some .tar.gz. –  Lri Nov 1 '11 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 Nov 1 '11 at 13:51
show 1 more comment

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

share|improve this answer
add comment

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

share|improve this answer
add comment

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
share|improve this answer
    
This only works for filenames with 4 leading blanks :-( –  patrix Oct 30 '11 at 13:05
    
for f in *; do mv "$f" "echo $f | cut -b 1-"; done man cut –  Tuan Anh Tran Oct 30 '11 at 13:19
    
for f in *; do mv "$f" "$(echo $f)"; done then, doesn't have anything to do with cut at all. –  patrix Oct 30 '11 at 14:25
    
thanks for the fix @patrix –  Tuan Anh Tran Oct 30 '11 at 14:33
1  
@frozenwithjoy: This fails if $f contains blanks inbetween as well, e.g. " foo bar ". –  patrix Oct 30 '11 at 16:52
show 6 more comments

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.

share|improve this answer
1  
Had a look at the script, now I don't know whether I should laugh or cry... –  patrix Oct 30 '11 at 16:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.