Shorten Path

Summary: Truncates the filenames of the selected records so that the path is less than 255 characters.
Requires: EagleFiler
Install Location: ~/Library/Scripts/Applications/EagleFiler/

Description

EagleFiler and the Mac filesystem support filenames up to 255 characters in length. Microsoft OneDrive is limited to fewer than 255 characters for the entire path, and in fact can crash with longer paths. This script renames the selected files, removing characters from the end of the basename so as to preserve the file extension, to satisfy this maximum path length. If a filename cannot be shortened enough, the script warns you and leaves the original name intact. You could then try shortening the file’s path by renaming or removing its containing folders.

Note that this script only ensures the length of the record’s file. If the file is a package directory, e.g. a “.rtfd” file, it may contain other files whose paths exceed the limit.

See also the shortening file names forum thread.

Download in Compiled Format · Download in Text Format

Script

tell application "EagleFiler"
    
set AppleScript's text item delimiters to ""
    
set _records to selected records of browser window 1
    
repeat with _record in _records
        
set _oldName to _record's filename
        
repeat until my isPathLengthOK(_record) or not my canFilenameBeShortenred(_record)
            
set _oldBasename to _record's basename
            
set _newBasename to (characters 1 thru -2 of _oldBasename) as text
            
set _record's basename to _newBasename
        
end repeat
        
if not my isPathLengthOK(_record) then
            
-- Can't fix it, so restore original name
            
set _record's filename to _oldName
            
display dialog "The path “" & my posixPath(_record) & "” could not be shortened enough."
        
end if
    
end repeat
end tell

on posixPath(_record)
    
tell application "EagleFiler"
        
set _file to _record's file
        
return _file's POSIX path
    
end tell
end posixPath

on pathLength(_record)
    
return length of my posixPath(_record)
end pathLength

on isPathLengthOK(_record)
    
return my pathLength(_record) < 255
end isPathLengthOK

on canFilenameBeShortenred(_record)
    
tell application "EagleFiler"
        
set _basename to _record's basename
        
return length of _basename > 1
    
end tell
end canFilenameBeShortenred

Last Modified: 2014-04-22

Running the Script

You can use AppleScript Editor (AppleScript Utility prior to Mac OS X 10.6) to enable Mac OS X’s built-in Script menu. Download the compiled script file and install it in the Install Location recommended above, creating any necessary folders. To run the script, just select it from the menu. FastScripts provides a similar menu that supports keyboard shortcuts and other features.