Save screenshot to clipboard and file, simultaneously [duplicate]

Within OS X 10.11.6, the Cmd ⌘ Shift ⇧ 4 key combination saves a screenshot to the desktop by default.

Is there a way to automatically copy the file to the clipboard (in addition to saving the file), so that the screenshot can be pasted into a document? Maybe there is some clever why to tee and pipe the image to the clipboard?

The intent is to:

  1. save screenshot to a file and
  2. copy screenshot to the clipboard

with a single command. Examples are always appreciated.


Solution 1:

The following is an example of what I'd do, if I needed both, to place a screen shot on the clipboard and save it as a file at the same time.

I'd use Automator to create a Service1workflow, to which a keyboard shortcut could be assigned, to run an AppleScript script to have these two events happen in conjunction with each other.

In Automator, create a new Service1 with the following settings:

  • Service receives (no input) in (any application)
  • Add a Run AppleScript action, replacing the default code
    with the example AppleScript code shown further below:
  • Save the Automator Service1 as, e.g.: Screen Shot to Clipboard and File
  • Assign a shortcut, in System Preferences > Keyboard > Shortcuts > Services:
    • Screen Shot to Clipboard and File      ⇧⌘5 2

Now when you press ⇧⌘5 2 the crosshair cursor appears just as though you had pressed ⇧⌘4, however after making the selection as normal and releasing the mouse, the selected area is both copied to the clipboard and saved to a file on the Desktop.


macOS Mojave Update:

  • 1In macOS Mojave, a Service in Automator is now called a Quick Action, so select that.
  • 2By default, ⇧⌘5 in macOS Mojave is by assigned to a new Screenshot feature, so try ⇧⌘6 instead.

The file naming convention is that of the macOS default for Screen Shots saved normally, in my region. You may need to adjust the following line of code for it to be as in your region:

set theDateTimeNow to (do shell script "date \"+%Y-%m-%d at %l.%M.%S %p\"")

In my region, this command produces the following example output where the value of the theDateTimeNow variable would be, e.g.:

2018-01-13 at 12.04.30 PM

Between the line of code above and the two lines that follow it in the script, they produce, e.g.:

Screen Shot 2018-01-13 at 12.04.30 PM.png

In Terminal, have a look at the man page for both date and strftime, in order to make adjustments to format the date and time value of the theDateTimeNow variable, as needed or wanted.

Note: Read the comments throughout the example AppleScript code so as to understand what the script is doing.

This was tested under macOS 10.13.1 and worked for me without issue.


Example AppleScript code:

on run {input, parameters}

    --  # Screen Shot to Clipboard and File

    --  # Clear the clipboard so the 'repeat until isReady ...' loop works properly.

    set the clipboard to ""

    --  # Copy picture of selected area to the clipboard, press: ⌃⇧⌘4
    --  # Note that on my system I need to keystroke '$' instead of '4'.
    --  # I assume this is because the 'shift' key is being pressed.        

    tell application "System Events"
        keystroke "$" using {control down, shift down, command down}
    end tell

    --  # Wait while user makes the selection and releases the mouse or times out.
    --  # Note that the time out also acts as an escape key press of sorts. In other
    --  # words, if the user actually presses the escape key it has no effect on this
    --  # script like it would if pressing the normal shortcut outside of the script.
    --  #       
    --  # As coded, the time out is 5 seconds. Adjust 'or i is greater than 10' and or  
    --  # 'delay 0.5' as appropriate for your needs to set a different length time out.
    --  # This means, as is, you have 5 seconds to select the area of the screen you
    --  # want to capture and let go of the mouse button, otherwise it times out.

    set i to 0
    set isReady to false
    repeat until isReady or i is greater than 10
        delay 0.5
        set i to i + 1
        set cbInfo to (clipboard info) as string
        if cbInfo contains "class PNGf" then
            set isReady to true
        end if
    end repeat
    if not isReady then
        --  # User either pressed the Esc key or timed out waiting.
        return  --  # Exit the script without further processing.
    end if

    --  # Build out the screen shot path filename so its convention is of 
    --  # the default behavior when saving a screen shot to the Desktop.

    set theDateTimeNow to (do shell script "date \"+%Y-%m-%d at %l.%M.%S %p\"")
    set theFilename to "Screen Shot " & theDateTimeNow & ".png"
    set thePathFilename to POSIX path of (path to desktop folder as string) & theFilename

    --  # Retrieve the PNG data from the clipboard and write it to a disk file.

    set pngData to the clipboard as «class PNGf»
    delay 0.5
    try
        set fileNumber to open for access thePathFilename with write permission
        write pngData to fileNumber
        close access fileNumber
    on error eStr number eNum
        try
            close access fileNumber
        end try
        activate
        display dialog eStr & " number " & eNum buttons {"OK"} default button 1 with title "File I/O Error..." with icon caution
    end try

    --  # Convert the POSIX path filename to an alias.

    set thePathFilename to POSIX file thePathFilename as alias

    --  # Hide the file extension as is the default.

    tell application "Finder"
        try
            set extension hidden of thePathFilename to true
        end try
    end tell

end run

Note: The example AppleScript code above is just that, and sans the include error handling does not include any other as may be appropriate/needed/wanted, the onus is upon the user to add any error handling for any example code presented and or code written by the oneself.