Using Shortcuts for Changing Spaces in File Names
30 Nov 2024On Windows, I use the amazingly functional file manager XYplorer, and I have various scripts that allow me to work with files in a variety of ways that just make it one of my most productive tools on the PC. I would love to have something as functional on macOS, but instead I end up using tools outside of my file management apps (I actually use Finder, Path Finder, and Forklift in different circumstances as each has their particular strengths) to enhance them. One of the things I created some XYplorer scripting for over on Windows was converting to and from spaces in file names. I like spaces in file names, but sometimes downloads come with spaces replaced by other characters, and sometimes uploads or system configurations don’t like spaces.
While it is less frequent outside of the day job, I do still get these challenges in my personal computing, and so I came up with a semi-smart Apple Shortcuts-based automation to do some file renaming for me.
Making the Shortcut Available
Shortcuts can be set as quick actions and made available in the services menu on macOS, and so I began by setting this up for the shortcut in the shortcut details. This then enabled the receive options for the shortcut. Since I am working purely with file names, I set the receive options to be files only and to stop if there were no files passed in.
With this set, the shortcut now appears in file management apps in the quick actions and services menus.
Configuration
To make the shortcut flexible, I decided to create it in a way that was easily customisable. I find that spaces are most often substituted with URL encoded spaces (%20
), hyphens, underscores, or pluses. But others may find other substitution cases such as non-breaking spaces, or periods, or tildes, or something else. To that end I added a dictionary at the start of the shortcut that lets you specify what spaces can replace, or have been replaced with.
The dictionary has two root level keys - SpaceTo
and SpaceFrom
.
SpaceTo
is itself a dictionary and has a set of key value pairs that can be added to. Each key is how the substitution is described in a list to the user for them to choose from, and the value is the entry to replace a space with. For example, the first entry means a user who is processing a file whose name contains spaces will be offered the option “URL Encode (%20)”, which if selected will have the shortcut replace spaces in the file name with the string “%20”.
SpaceFrom
is an array that lists the character strings that a file name will be checked for in order to substitute spaces. Note, this is an array and so unlike the dictionary, the entry order is always maintained; and this will matter later on. This is also why I don’t have just one set of keys and values to derive everything from.
Repeat and First Check
The shortcut is configured to loop through all the files it is passed and process each of them independently. This is good if you have a mixture of different substitutions to deal with, but perhaps not so great if you have a lot you want to process in the same way. However, with only a relatively small modification, you could change this shortcut to do that, but foregoing some of its flexibility.
For each file the shortcut grabs the filename explicitly, because implicitly does not work for all file types in Shortcuts on macOS at the time of writing. We can often end up getting the file content rather than the file name when referencing the name property in a magic variable.
With the file name obtained, we then begin with the primary check - does it contain any spaces.
Conversion From Space
If spaces are found, the shortcut will attempt to convert to something else. It reads in the configuration data and builds a list of options from the SpaceTo
keys. The list is presented to the user and then the value for the selected entry is looked up. The original file name then has the spaces it contains substituted by the value and the result is saved into a variable called New Name
.
Conversion To Space
If spaces were not found, then the shortcut will look for strings to convert to spaces in the file name. It begins by setting a variable called Already Matched
to a string, “FALSE”. This is an initialisation step because we use the ordered array to look at each alternative string in order and we only want to process the first match.
As the array entries are taken, if the Already Matched
variable remains “FALSE”, and if a match is found, the shortcut will then run a substitution for the string to a space. After this, it will look for occurrences of three spaces and do a partial substitution back.
For example, imagine an original file name of foo - bar.txt
. If spaces were substituted with hyphens, this would become foo---bar.txt
. If this were just a direct hyphen to space conversion back, we get foo bar.txt
. We can make a reasonable optimisation to account for this and while it is not perfect, we can effectively get data loss, my experience is this helps cover the majority of cases.
Again the converted text is put in the New Name
variable for use later, but this time the Already Matched
variable is updated to “TRUE” meaning that it won’t try and do any matches with later entries in the array.
Rename the File
At the end of the main repeat loop, each file is processed by renaming it. Now it is possible that a file may not have contained any spaces or any characters that were configured for conversion to spaces. In those situations, the New Name
variable will not have been created, so the initial step is to confirm that it has, and if it has, the file being processed is then renamed to the new name.
Conclusion
I have separate actions for each variation in XYplorer, but not one that does multiple actions, and since my quick actions and services menus are pretty full of useful automations, I decided that I’d go for a ‘smarter’ single automation with a little less fixed control.
You can download the shortcut using the link below, and don’t forget to modify the configuration dictionary if you want to add other conversions.
Make sure you understand the purpose and use of the shortcut before you try it out, and if you are unsure, don’t forget to take backups first.