2

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.

  • 1
    How about writing an applescript to do that? A search may give you an example to start... – Solar Mike Dec 16 '17 at 5:44
  • 1
    It’s a slippery slope : I found one for one task then did a couple more! Good luck! – Solar Mike Dec 16 '17 at 7:08
  • 1
    Once you have a script you can set it as a service : available throuh the right-click menu... – Solar Mike Dec 16 '17 at 7:32
  • 1
    @SolarMike I found that too, but I also can do one better; I can embed the script into the Finder window and have the script execute once I drop the file into the script icon! See this: macosxautomation.com/applescript/firsttutorial/16.html And this: developer.apple.com/library/content/documentation/… Also, if you want the Reputation, feel free to add your solution as an answer, I'll accept it. – Zachary David Saunders Dec 16 '17 at 8:51
  • 1
    @ You provided the links : have the pleasure to do an answer and I will +1 for you. ! But enjoy, cheers Mike – Solar Mike Dec 16 '17 at 9:20
2

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
  • 1
    Sorry, I've just seen your additional note about ideally wanting a generalised method that works for all file types. Obviously, I limited mine to your example situation dealing with images, but it's easy to adapt the bits and turn it into a general file replacement service. If you need any additional help, leave a comment. – CJK Dec 23 '17 at 22:42
  • Sorry for the delay. Yes, this is exactly what I was looking for. Thank you for your detailed write-up. I've accepted your answer, cheers! – Zachary David Saunders Jan 7 '18 at 13:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .