Automatically name screenshots with the active window's title on macOS

Automatically name screenshots with the active window's title on macOS

·1210 words·6 mins

On macOS, screenshots are named like Screenshot 2025-10-06 at 1.39.16 PM.png. When I’m taking multiple screenshots of figures in multiple papers, I can’t tell which screenshot file corresponds to which paper.

In this post, I describe one way to automatically name the screenshots with some information about the active window that I am capturing in the screenshot (e.g., a screenshot of the Music app should be saved as Music 2025-10-06 at 1.39.16 PM.png).

By the way, on Windows, the Greenshot app automatically names screenshots with the window title.

macOS has unhelpful screenshot filenames
#

First, some background.

I use the Paperpile PDF reader to read scientific papers because it gives the PDF files tidy names (and allows me to highlight and take notes):

I often read multiple papers in one session, and sometimes I prepare slides about the papers to keep notes for myself or present to colleagues later.

I use the built-in shortcut Shift++4 to take screenshots of the most interesting figures, so I can put them in my slides later.

After a reading session, I have folder with indistinguishable screenshots named like this:

Screenshot 2025-10-06 at 1.32.01 PM.png
Screenshot 2025-10-06 at 1.35.25 PM.png
Screenshot 2025-10-06 at 1.36.49 PM.png
Screenshot 2025-10-06 at 1.40.26 PM.png

This makes it difficult to figure out which screenshot comes from which paper.

Wouldn’t it be nice if the filename of the screenshot had information about which paper it came from?

Existing solutions do not help
#

Mac Jim ID suggested a way to pop up a window where you manually type the file name for each screenshot.

The folks at Setapp suggested multiple options for how to rename screenshot file names. One option is the AI tool Keep it Shot that can be launched on a set of selected images to rename them based on the content in each image. Interesting, but not really want I want.

I didn’t like any of these existing solutions, so I made my own.

Applescript can automatically name screenshots
#

Since I take thousands of screenshots, I wanted a way to automatically name the PNG files without any extra steps.

Applescript is a built-in programming language for macOS that allows you to control the computer, and it turns out that we can use it to flexibly rename screenshots the way we want.

However, we also need a way to get the keyboard shortcut Shift++4 to execute our Applescript instead of the default behavior.

After trying a few options, Hammerspoon seems to be one of the best solutions for this. You can write Lua files to customize all sorts of behavior on your computer.

If you prefer to configure things in a graphical user interface instead of text files, you might want to try other options for launching an Applescript like Alfred.

By the way, there is a way to launch an Applescript with a keyboard shortcut by using the the built-in Automator app instead of a 3rd party app, but it is a bit tedious.

Write scripts in the Hammerspoon configuration directory
#

Once you have installed Hammerspoon, you can configure any keyboard shortcut to run an Applescript.

Steps:

  1. Make a configuration folder for Hammerspoon.
  2. Make an init.lua file to set up a keyboard shortcut.
  3. Copy the Applescript into a text file in the configuration folder.
  4. In the Hammerspoon app, select Reload Config.

Open your terminal and make a new folder for Hammerspoon:

mkdir ~/.hammerspoon/

Paste this code into a new file ~/.hammerspoon/init.lua:

-- Capture the keystroke before macOS sees it
hs.hotkey.bind({'shift','cmd'}, '4', function()
    hs.task.new('/usr/bin/osascript', nil, {
      os.getenv('HOME')
      ..'/.hammerspoon/scripts/screenshot-with-title.applescript'
    }):start()
end)

Make a new folder for the Applescript file:

mkdir ~/.hammerspoon/scripts

Paste this code into a new file ~/.hammerspoon/scripts/screenshot-with-title.applescript:

-- ~/.hammerspoon/scripts/screenshot-with-title.applescript
-- Kamil Slowikowski
-- 2025-05-26

-- Get the title of the frontmost application window.
-- https://stackoverflow.com/questions/5292204
global frontApp, frontAppName, windowTitle
set windowTitle to ""
tell application "System Events"
    set frontApp to first application process whose frontmost is true
    set frontAppName to name of frontApp
    tell process frontAppName
        tell (1st window whose value of attribute "AXMain" is true)
            set windowTitle to value of attribute "AXTitle"
        end tell
    end tell
end tell

on sanitizeForFilename(inputString)
    -- Replace invalid characters with underscores or remove them
    set invalidCharacters to {"'", "'", "@", "%", "(", ")", "/", "\\", ":", "*", "?", "\"", "<", ">", "|"}
    set sanitized to inputString
    repeat with invalidChar in invalidCharacters
        set sanitized to my replaceText(invalidChar, "_", sanitized)
    end repeat
    -- Replace multiple underscores with a single underscore
    set sanitized to my replaceText("__", "_", sanitized)
    -- Remove leading and trailing underscores (optional)
    set sanitized to my trimText(sanitized, "_")
    return sanitized
end sanitizeForFilename

on replaceText(findText, replaceText, inputString)
    set AppleScript's text item delimiters to findText
    set textItems to every text item of inputString
    set AppleScript's text item delimiters to replaceText
    set modifiedString to textItems as string
    set AppleScript's text item delimiters to ""
    return modifiedString
end replaceText

on trimText(inputString)
    set trimmedString to inputString
    repeat until trimmedString does not start with " "
        set trimmedString to text 2 thru -1 of trimmedString
    end repeat
    repeat until trimmedString does not end with " "
        set trimmedString to text 1 thru -2 of trimmedString
    end repeat
    return trimmedString
end trimText

on FileExists(theFile) -- (String) as Boolean
    tell application "System Events"
        if exists file theFile then
            return true
        else
            return false
        end if
    end tell
end FileExists

-- sanitize the filename
set windowTitle to sanitizeForFilename(windowTitle)
-- display dialog "windowTitle: " & windowTitle

set windowTitle to do shell script "cut -c1-60 <<<" & quoted form of windowTitle

-- Set the path to the output file.
copy {do shell script "echo $HOME"} to {homeFolder}
copy {do shell script "date +%Y-%m-%d_%H-%M-%S"} to {dateNow}

-- basename of png file
set pngFile to windowTitle & " " & dateNow & ".png"

-- final output destination for optimized files
set outPath to homeFolder & "/Screenshots"

-- temporary destination before optimization
set rawPath to quoted form of ("/tmp/" & pngFile)
set optPath to quoted form of (outPath & "/" & pngFile)

-- display dialog rawPath

-- Capture the screenshot.
do shell script "screencapture -xi " & rawPath

if FileExists("/opt/homebrew/bin/pngquant") then
    -- Optimize the PNG file with pngquant: https://pngquant.org/
    do shell script "/opt/homebrew/bin/pngquant --output " & optPath & " " & rawPath
    -- Remove the unoptimized PNG file
    do shell script "rm -f " & rawPath
else
    do shell script "mv -f " & rawPath & " " & optPath
end if

-- Reveal the PNG file in Finder.
do shell script "open --reveal " & optPath

Finally, once you reload the Hammerspoon config, the new keyboard shortcut should be working.

Examples of named screenshots
#

Now, my folder of screenshots is much easier to browse:

Reynolds et al. 2020 - N. Engl. J. Med. - Brave 2025-10-06_15-02-11.png
Çakan et al. 2025 - J. Clin. Invest. - Brave 2025-10-06_14-40-11.png
Idkowiak et al. 2025 - Nat. Commun. - Brave 2025-10-04_15-59-45.png

At a glance, I can easily tell which screenshot comes from which paper.

The name of the screenshot is taken from the title of the focused application. So, if you take a screenshot of the Music app or App Store app, the names look like this:

Music 2025-10-06_15-05-19.png
App Store 2025-10-06_15-06-00.png

I’m sure you can customize the Applescript code to put even more information into the file name, but this is good enough for me.

Reply by Email
Kamil Slowikowski
Author
Kamil Slowikowski
Computational biologist at Mass General Brigham.