Copying the Most Recent Screenshot Into the Clipboard
The built-in screenshot tool in MacOS (⌘+Shift+4) is really useful to quickly capture a part of your screen. In addition to using it for archival purposes, I use it when messaging and when building presentations to quickly pull material together.
You can modify the above shortcut using Control (⌘+Control+Shift+4) to put the screenshot in the clipboard directly, and this works great if you are going to paste it immediately but the process of going back to find the latest screenshot much more manual and laborious.
Opening Finder, navigating to your Desktop 1, opening the file in Preview, selecting the image, ⌘+C to copy it into my clipboard, switching to the correct destination app, and then finally ⌘+V to paste it takes a while and breaks my flow.
This is fixable with a small amount of scripting, and I have a single global hotkey (hyper + S) which copies the latest screenshot into the clipboard for easy pasting wherever you need it.
The Tech Details: Finding the Latest Screenshot
Since screen shots are always written to the same place, it is straightforward to write some code that looks in that folder (typically your Deskop), and finds the most recent file which contains “Screen Shot” in its name.
For Hammerspoon users, the code looks like this:
function getLatestScreenshot() local latestScreenshot = "" local earliestTimestamp = 0 local homeDir = os.getenv("HOME") .. "/Desktop" for file in hs.fs.dir(homeDir) do local creationTimestamp = hs.fs.attributes( homeDir .. file, "creation") if (earliestTimestamp == 0 and string.match(file, "Screen Shot")) then earliestTimestamp = creationTimestamp latestScreenshot = file end if ((creationTimestamp > earliestTimestamp) and string.match(file, "Screen Shot")) then earliestTimestamp = creationTimestamp latestScreenshot = file end end return os.getenv("HOME") .. "/Desktop/" .. latestScreenshot end
The Tech Details: Copying That Screenshot To the Clipboard
I started by using hs.image and hs.pasteboard in Hammerspoon to read the image and write it to the pasteboard. This worked, but sometimes would result in the destination application pasting an uncompressed TIFF, which was extremely large. hs.pasteboard doesn’t appear to let you customize the image types, so I generated a fragment of AppleScript instead and ran that instead so the image was always written to the clipboard as a JPEG.
function copyLatestScreenshotToClipboard() local latest = getLatestScreenshot() local asReadFile = "(read (POSIX file \"" .. latest .. "\") as JPEG picture)" local appleScriptFragment = "set the clipboard to " .. asReadFile local success, outputObject, result = hs.osascript.applescript(appleScriptFragment) hs.alert.show("Last screenshot copied.") end
You can bind the copyLatestScreenshotToClipboard function above to any hotkey you like, and then paste normally using ⌘+V.
Alternative: Use Shortcuts for Mac
Another (maybe better!) option, especially if you aren’t a Hammerspoon user, is to use the Shortcuts app in MacOS Monterey.
- Use the Get Contents of Folder action, and point to where your screen shots show up. By default, this is your desktop.
- Filter the folder: Sort by Creation Date, Order by Latest First, and then Limit to 1. You only need the last file
- Finally, copy that file to the clipboard.
You can trigger this Shortcut using the Shortcuts app, the Shortcuts menu bar item, or even from the command line using the shortcuts terminal command.
You can change the folder where your screen shots are stored if you’d like; There’s a Terminal command for it, but these days I typically just leave them on the Desktop. ↩︎