Pruning long filenames on the Mac
February 17, 2006 1:51 PM   RSS feed for this thread Subscribe

Easy way to find and shorten long filenames on the Mac?

I'm trying to move a whole bunch of files from a Mac up to a shared server. The server won't allow filenames longer than 30 characters.

When I try to copy, every time the Mac (OS/X 10.3.2) hits one of those long filenames, it shows an error message, then cancels the copy. So right now I'm having to walk through the folder hierarchy, manually finding and pruning the long file names.

Please tell me there's an easier way to do this.
posted by ottereroticist to computers & internet (5 comments total)
Try A Better Finder Rename.
posted by sdrawkcab at 2:23 PM on February 17, 2006


You might find Peter Maurer's FileList fits your need.
posted by lstryder at 2:25 PM on February 17, 2006


An AppleScript would do this pretty handily.

on doIt(theFiles)
    set c to ""
    if class of theFiles is not list then set theFiles to {theFiles}
    repeat with f in theFiles
        tell application "Finder"
            set p to f as text
            if p ends with ":" then
                set c to every file of f as alias list
                -- display dialog result as text
                my doIt(c)
            end if
            set n to name of f
            set p to "This file's name is longer than 30 characters. Please shorten its name."
            repeat while length of n > 30 and p is not ""
                set n to text returned of (display dialog p default answer n)
                try
                    if length of n >= 30 then
                        set name of f to n
                        set p to "" -- flag good name accepted
                    else
                        set p to "Error! That's still too long. Please try again."
                    end if
                on error -- usually a name conflict
                    set p to "Error! Probably some other file already has this name. Please try again."
                end try
            end repeat
        end tell
    end repeat
end doIt

on open (theFiles)
    my doIt(theFiles)
end open

on run
    my doIt(choose folder with prompt "Choose the folder that contains the files to be renamed.")
end run
posted by kindall at 3:10 PM on February 17, 2006


You can take out "-- display dialog result as text", it's a debugging statement I forgot to take out. (It's commented out so it won't hurt anything to leave it in, though.)

In case it's not clear, just paste the above into Script Editor and run it, then select the folder you want to process with the dialog. Or save it as an application and drop the items you want to process onto it.
posted by kindall at 3:12 PM on February 17, 2006


All right, here's a revised version that actually recurses properly:

property maxNameLength : 30

on doIt(theFiles)
    set c to ""
    if class of theFiles is not list then set theFiles to {theFiles}
    repeat with f in theFiles
        tell application "Finder"
            if (f as text) ends with ":" then
                set c to every item of f as alias list
                my doIt(c)
            end if
            set n to name of f
            set p to "This file's name is longer than " & maxNameLength & " characters. Please shorten its name."
            repeat while length of n > maxNameLength and p is not ""
                set n to text returned of (display dialog p default answer n)
                try
                    if length of n ≥ maxNameLength then
                        set name of f to n
                        set p to "" -- flag good name accepted
                    else
                        set p to "Error! That's still too long. Please try again."
                    end if
                on error -- usually a name conflict
                    set p to "Error! Probably some other file already has this name. Please try again."
                end try
            end repeat
        end tell
    end repeat
end doIt

on open (theFiles)
    my doIt(theFiles)
end open

on run
    my doIt(choose folder with prompt "Choose the folder that contains the files to be renamed.")
end run
posted by kindall at 9:37 PM on February 17, 2006


« Older Question for NFL addicts. Help...   |   Sensory deprivation tanks: Are... Newer »
This thread is closed to new comments.