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/
Last Modified: 2019-12-10

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.

Installation Instructions · 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