Skip to content

Instantly share code, notes, and snippets.

@slowkow
Last active May 26, 2025 20:13
Show Gist options
  • Save slowkow/a384bef43a3afd987b0e6727a71ad0f6 to your computer and use it in GitHub Desktop.
Save slowkow/a384bef43a3afd987b0e6727a71ad0f6 to your computer and use it in GitHub Desktop.
Applescript to take a screenshot with the application title in the filename.
-- take-screenshot.scpt
-- 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
@beswooned
Copy link

Hi,

I tried this. Everything works fine until the pngquant. I get no file or directory, but when I change the pngquant to go to the desktop instead, I get it's a directory. Could you please edit this so it is more generic, and saves to the computer? I am not a coder. I was just trying stuff, but it didn't work.

@slowkow
Copy link
Author

slowkow commented May 26, 2025

@beswooned I modified the script to check if pngquant is installed. Then, I tested that it works when pngquant is not installed.

You will need to have a folder "~/Screenshots" for the images to be saved into, so go ahead and make that folder if it does not already exist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment