Lets say that I just captured a screenshot called "screenshot.jpg" and I have a folder of screenshots called "screenshots".
Screenshots
└ Work.jpg
└ School.jpg
└ Misc.jpg
I want to take this recently captured screenshot and replace the "misc.jpg" file within the screenshot folder. What is the best way to do this? I know that I can rename the file to the name of the file currently within the folder, copy+paste it into the folder, and then choose "replace file" option when prompted, but this isn't ideal. Is there a keyboard shortcut or other quick way to perform this action?
Note: This is just an example use case. It would be ideal if I could use a quick method regardless of filename or filetype.
Best Answer
This little AppleScript will take a screen shot and overwrite whatever file you currently have selected in Finder.
It doesn't move, copy, or delete; simply overwrites it. So, bear in mind, this means the created date will remain the same, which potentially could see you generating a new screen shot writing it to an image file created two years ago. (The modified date gets updated as you would expect.)
As someone suggested, this sort of thing would work well as a service in Finder, to which you could assign a shortcut.
I went about creating one:
The script is very similar to one above (and appended to the bottom of this answer in a code block for you to copy/paste). It has some error handling and notifications thrown in, given that this is going to be a service and it's good to make it a robust one. It also doesn't take any screen shot of its own accord, as a screen shot taken when the service is activated would necessarily have Finder in focus, which might not be the screen shot you want.
Therefore, this service assumes that you have taken the screenshot yourself already, and its lying in wait on the clipboard. Finder already has shortcuts that allow screenshots to be sent straight to the clipboard, so there's no need to do anything extra.
I saved the service as one called Replace Image File, as you'll notice the input is specifically image files in Finder. Therefore, the service won't be accidentally triggered if, say, an application file were selected.
It now turns up in the pop-up context menu whenever I right-click on an image in Finder:
Next, I went into System Preferences to assign a keyboard shortcut:
This shortcut shows up in the Finder menu under Services:
I chose ⌃⇧⌘R because it's reasonably difficult to press accidentally, but it's pretty adjacent to the default shortcuts for sending a screenshot straight to clipboard, namely ⌃⇧⌘3 and ⌃⇧⌘4, meaning the physical manoeuvre between taking a screenshot and triggering the service a convenient one to make.
Happy to say that, in testing, it worked splendidly, so I may just keep it for myself as I can see it being fairly useful.
Finally, here's the code block for the Automator service workflow: