A zsh
script:
This script is a minimal solution to renaming all of the screenshots in a directory to replace every space with an underscore (_).
#!/bin/zsh
# shotfnmv.sh # FILE NAME FOR THIS SCRIPT
cd $HOME/Desktop/screenshots2 # CHOOSE YOUR FOLDER
for afile in *.png # CHOOSE YOUR FORMAT; IF NOT .png
do
if [[ $afile == "Screenshot "* || $afile == "Screen Shot "* ]]; then
newfilenm=$(echo $afile | sed 's/[[:blank:]]\{1,\}/_/g')
mv $afile $newfilenm
fi
done
I've chosen to use the native (Apple) sed
(stream editor) utility to perform the substitution; every space [[:blank:]]
will be replaced by a single underscore _
; the result is stored in the variable newfilenm
. The filename (afile
) will be re-named (using mv
) to newfilenm
, and so the original file name will be gone after this script is run.
Only those files that match one of the two criteria will be renamed, so if you keep files with "custom names" in the same folder, this script shouldn't rename them. I used two criteria ("Screenshot "*
or "Screen Shot "*
) for this screen because I know that on Ventura the single-word Screenshot
is used instead of the dual-word Screen Shot
as it is on your system.
A logistical issue:
Note that this solution leaves you with a logistical issue to resolve: How do I run this script when it needs to be run??
I didn't address that issue here because you didn't specifically ask about it, but I'll outline a couple of approaches. You may choose to ask another question after reviewing the alternatives.
watch
is a Linux/Unix utility that can detect when a file in a designated directory is added or changed, and this can be used to trigger another action (e.g. running the script above). It's not available natively in macOS, but you can get it through either of the commonly-used 3rd-party Package Managers: MacPorts or HomeBrew.
- Set up a "User Agent" to run under
launchd
I use a 3rd-party app called LaunchControl
for this sort of thing, but you can also use the native launchctl
if you lean toward masochism :) LaunchControl
provides a watch
-like feature which makes this very easy to do. LaunchControl
isn't free, but it's not very expensive & well worth it IMHO.
- Run the script continuously with
sleep
I don't like the idea of this - it just strikes me a wasteful and inefficient, but it's quite easy. There's a thread on SO that discusses how to do this with some comparisons to watch
. Closely related is using the cron
utility to schedule times to run the script.
EDIT:
I've just noted a new answer that may provide a more complete answer... Unfortunately, I'm not familiar with Automator, but it may be just the ticket?
Terminal.app
:-0 – Seamus Apr 20 '23 at 18:28