2

When I save a screenshot, the current filename shown as follows: Screen Shot 2017-03-01 at 10.05.05.png. Instead of using a whitespace character, can we change it to another character like _?

Example filename: Screen_Shot_2017-03-01_at_10.05.05.png

Please note that on the latest macOS there is no <key>%@ %@ at %@</key> <string>%@ %@ at %@</string> in the ScreenCapture.strings file.

Related: Save screenshot with custom name

Allan
  • 101,432
alper
  • 218
  • Why exactly was this downvoted? – Allan Apr 20 '23 at 13:37
  • Before I go to the trouble of writing an answer, I need to know if you're OK with an answer that uses a shell script (i.e not a GUI) to change the filenames that Apple automatically assigns. This would involve using Terminal.app :-0 – Seamus Apr 20 '23 at 18:28
  • @Seamus Yes sir I am 100% OK with a shell script :-) – alper Apr 21 '23 at 09:02

2 Answers2

1

Here is one possible solution that you may be interested in. Using Automator, you can create a Folder Action that will rename files added to the specified folder, whose names contain blank spaces, with underscores.

First, open Automator.app and create a new Folder Action. Then add a Run Shell Script command to the workflow and insert the following code. The following image should show you what to do.

for f in "$@" ;do echo "$f" |mv "$f" "$(sed 's@ @_@g')" ;done

enter image description here

Then just save and name your new Folder Action.

After that, all you need to do is just open the Folder Action Setup.app and attach your new Folder Action to the target folder.

enter image description here

The only issue is that with the above method, every single file added to that folder will be renamed if there are spaces in the file names... Not just the screenshot files.


Now here is a little bit of a different solution if you were to already have a folder with a bunch of files, including the screenshot files with spaces in their names, and you only want to rename those screenshot files with spaces in their names while leaving everything else untouched… You can paste and run this following code into Script Editor.app, which will ask you to choose a folder and then it will rename those screenshot files with spaces in their names, within that folder.

activate
set chosenFolder to quoted form of POSIX path of (choose folder)

try do shell script "cd " & chosenFolder & ¬ " ;for f in Screen* ; do echo &quot;$f&quot; |mv &quot;$f&quot; &quot;$(sed 's@ @_@g')&quot; ;done " end try

wch1zpink
  • 7,571
1

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.

  • Use the watch command

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:

  • Automator perhaps?

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?

Seamus
  • 4,547
  • I appreciate how you explain everything from basics. When rename occurs does it affect the creation time of the file? – alper Apr 27 '23 at 11:30
  • @alper: No - in my experiment, it did not change either the Date Created, nor the Date Modified. The Date Added did change, but this was due to copying the files from my original screenshot folder to my test folder. – Seamus Apr 27 '23 at 16:29