I got a request from a Default Folder X user yesterday, asking if I could add a feature to limit the length of the default filename supplied in Save As dialogs. Apparently, the cloud services and backup software that he and his colleagues use can’t handle filenames longer than 45 characters, so he wants to make sure they don’t save files with names longer than that. That’s a pretty unfortunate limitation of the software they’re using (I mean really, in 2024?), but I’m all for avoiding stupid problems.
I’ve addressed a similar request before, automatically prepending dates to the names of files as you save them. This post is an update, because Default Folder X now has a better way to handle this than the method described in that earlier post.
Background
Default Folder X has a flexible, AppleScript-based method for handling customized default folders and filenames in Save dialogs. Yeah, I know AppleScript is old and crufty, but it’s available on every Mac, can interoperate with other apps, and allows you to cobble together just about anything, so it’s a good solution here. Anyway, when a file dialog appears on the screen, Default Folder X looks for and runs special AppleScripts if they’re present, allowing you to use any logic you want to determine the starting location (the “default folder”) for a file dialog, and the name of the file suggested in Save As dialogs.
The AppleScripts should be located in this folder:
~/Library/Application Support/com.stclairsoft.DefaultFolderX5/Scripts/
where the ‘~’ character denotes your home folder. Default Folder X looks for scripts with predetermined names, depending on their purpose:
- getDefaultFilename.scpt – modifies the suggested filename in Save As dialogs
- getDefaultFolder.scpt – specifies the default folder for an Open or Save dialog
Note that the scripts must be saved with those names in order for them to work. If you want a script to only run in the Open and Save dialogs of a single application, put it in a subfolder with the same name as the app. For example, to have scripts that only run in Safari, save them in:
~/Library/Application Support/com.stclairsoft.DefaultFolderX5/Scripts/Safari/
The implementation details (the name of the AppleScript handler and the arguments that Default Folder X passes to it) are included at the beginning of each of these sample scripts:
- GetDefaultFilename-EnforceMaxLength.scpt
- GetDefaultFilename-PrependDateToPDF.scpt
- GetDefaultFolder-FinderSelectionOrActiveWindow.scpt
- GetDefaultFolder-FrontFinderWindow.scpt
Truncating Long Filenames
OK, back to the task at hand. In this case, we want to provide a “getDefaultFilename” handler that truncates the default filename in Save dialogs to no more than “maxLength” characters.
Here’s the AppleScript:
on getDefaultFilename(appName, suggestedName, fileExtension)
-- Specify our maximum filename length
set maxLength to 45
-- Make sure the filename extension isn't counted
set addExtension to false
if fileExtension is not "" and suggestedName ends with fileExtension then
set suggestedName to text 1 thru -((count fileExtension) + 2) of suggestedName
set addExtension to true
end if
-- Truncate the filename
if length of suggestedName is greater than maxLength then
set newName to text 1 thru maxLength of suggestedName
else
set newName to suggestedName
end if
-- Add the extension back on if appropriate
if addExtension is true then
set newName to newName & "." & fileExtension
end if
-- And give the name to Default Folder X
return newName
end getDefaultFilename
Enter the code above in Script Editor and save it as an AppleScript script or download GetDefaultFilename.zip. Save the “GetDefaultFilename.scpt” file in this location:
~/Library/Application Support/com.stclairsoft.DefaultFolderX5/Scripts/
Each time a Save As dialog appears, it will magically truncate any filenames longer than 45 characters. Note that you can still edit the name afterwards in the Save dialog if you don’t like the one created by the script.
Credit Where Credit is Due
This scheme originated with an idea from Jason Snell (of Six Colors and The Incomparable fame) when he needed to customize the way Default Folder X worked in his podcasting workflow. I later expanded it as requests came in for customized filenames as well, and Bo Engelbrecht’s email reminded me that I’d never blogged about those updates. Thanks Jason and Bo!