Finder Shortcut – Keyboard Shortcut for Replacing File Within Finder

file-transferfinderkeyboardshortcut

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.

    # Check that precisely one file is selected 
    set F to the selection of application "Finder" as list
    if (count F) ≠ 1 then return beep

    set F to F's first item as alias

    # Perform a screengrab straight to the clipboard
    # then overwrite the file above with the image
    # data from the clipboard

    do shell script "screencapture -c"
    set screenshot to the clipboard as JPEG picture

    write screenshot to (F as alias)

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:

An Automator workflow

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:

Service visible in context menu

Next, I went into System Preferences to assign a keyboard shortcut:

System Preferences > Keyboard Shortcuts

This shortcut shows up in the Finder menu under Services:

Shortcut is visible in the Finder menu

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:

    on run {input, parameters}

        # Make sure precisely one file is passed to the service
        # Otherwise terminate with a beep
        if (count input) is not 1 then return beep

        # Error catching in the event that the clipboard
        # does not contain image data 
        try
            set ImageData to the clipboard as JPEG picture
        on error errMsg number errNo
            # Terminate script with a notification
            return display notification ¬
                "No image content found.  Unable to proceed." with title ¬
                "Replace Image File" subtitle ¬
                "Error: clipboard content is the wrong data type"
        end try

        # If the script reaches this point, all must be
        # well so we can try and overwrite the input file
        try
            write ImageData to input
        on error errMsg number errNo
            return display notification ¬
                "Unsuccessful overwriting." with title ¬
                "Replace Image File" subtitle ¬
                ("Error " & errNo as text) & ": " & errMsg
        end try

    end run