How can I trim the first 3 characters in file name with AppleScript?

Is there a way to trim off the first 3 character of multiple file names? (or last 3 characters)


Solution 1:

This code will do it for you:

set whichFile to choose file with multiple selections allowed
repeat with aFile in whichFile
    tell application "Finder"
        set filename to name of aFile
        set name of aFile to ((characters 4 thru -1 of filename) as string) --trim first 3
        --set name of whichFile to ((characters 1 thru -4 of filename) as string) --trim last 3
    end tell
end repeat

Note that stripping the last three will get rid of the extension. If that isn't what you want to happen, let me know in a comment.

Solution 2:

Here's a shorter script:

tell application "Finder"
    repeat with f in (choose file with multiple selections allowed)
        set name of f to text 4 thru -1 of (get name of f)
    end repeat
end tell

Renaming files is often easier in the shell though:

for f in *; do mv "$f" "${f:3}"; done

Parameter expansion is documented in file:///usr/share/doc/bash/bashref.html#SEC30.