Save screenshot with document name and page

Is it possible to add the document name and document page when I do screenshots?

Using just what's setup by default, no; however, aside from doing it manually, it can be automated to various degrees depending on what methods and applications are involved.

So, if I open a PDF and do a screenshot at page 6, I will have: Document Name | Page 6 | Screen Shot 2012-06-12 at 8.02.40 AM

As an example, using Preview and AppleScript along with an Automator Service/Quick Action, and assigning it a keyboard shortcut in System Preferences > Keyboard > Shortcuts > Services, the example AppleScript code, shown further below, will take a screen shot of either the full window of the front window of Preview or just its document scroll area, depending on its settings.

The name of the screen shot will be in the format shown quoted above from your question.


Here is a sample file name and a scaled down image of the screen shot taken of the document scroll area in Preview of The Room Where It Happened and was saved as:

   The Room Where It Happened | 3 | Snap Shot 2020-7-7 at 8.55.30 PM.png

Screen Shot


There are three variables at the start of the example AppleScript code that are user configurable:

  • saveToLocation - Where the screen shot will be saved.
    • The default is the users Pictures folder.
  • ssFileType - Which can be set to: png, jpg, pdf or tiff.
    • The default is: png
  • captureArea - Which gets set to to either whole window, or scroll area.
    • The default is: scroll area (of the document, not whatever shows in the Sidebar.)

You should not need to edit the code below these initial variables, unless necessary, and the code is lightly commented to help one understand what it is doing.


The following example AppleScript code was tested as an Automator Service in macOS High Sierra, and should also work as a Quick Action in macOS Mojave and later. The Automator Service was assigned a keyboard shortcut of ⌥⌘S and was pressed while Preview had focus and was frontmost.

--  # Set the save to location, screen shot file type, and the capture area.
--  # The code below these three settings should not need to be edited.

--  # The saveToLocation must be in the form of a 
--  # POSIX path for the screencapture command.

set saveToLocation to POSIX path of (path to pictures folder)

--  # ssFileType can be set to "png", "jpg", "pdf" or "tiff".

set ssFileType to "png"

--  # Set the capture area to either 'whole window', or just the 'scroll area'.

set captureArea to "scroll area"


--  # Do not edit the code below this line unless necessary.


--  # In Preview, when a PDF document is opened, the 'window name' is in the
--  # format of 'filename (page x of y)' for multi-page documents, or  
--  # 'filename (1 page)' for single-page documents, and the 'filename' may contain '.pdf'.
--  # Example: "The Room Where It Happened.pdf (page 1 of 570)"
--  # Parse the text items of the 'window name' for parts of the screen shot name.


--  # Get then name of the first window of Preview.

tell application "System Events" to ¬
    set windowName to ¬
        the name of ¬
            the front window of ¬
                application process "Preview"

--  # Check to see the document is a PDF, and terminate if not.

if windowName contains "(page" or windowName contains "page)" then
    -- # Document is a PDF file. Continue processing...
else
    return
end if

--  # Parse 'windowName' to get the doc name and page number.

set text item delimiters to "("

set documentName to ¬
    the first text item of ¬
        the windowName

if documentName contains ".pdf" then
    set documentName to ¬
        characters 1 thru -6 of ¬
        the first text item of ¬
            the windowName
else
    set documentName to ¬
        characters 1 thru -2 of ¬
        the first text item of ¬
            the windowName
end if

if the first word of ¬
    the second text item of ¬
        the windowName is "1" then
    set pageNumber to ¬
        the first word of ¬
            the second text item of ¬
                the windowName
else
    set pageNumber to ¬
        the second word of ¬
            the second text item of ¬
                the windowName
end if

set text item delimiters to ""

--  # Get position and size of the front window or the
--  # document scroll area of Preview for the screen shot.

if captureArea contains "whole window" then
    tell application "System Events" to ¬
        set windowPositionSize to {position, size} of ¬
            the front window of ¬
                application process "Preview"
else if captureArea contains "scroll area" then
    tell application "System Events"
        if exists scroll area 2 of ¬
            splitter group 1 of ¬
            window 1 of ¬
            application process "Preview" then
            set windowPositionSize to ¬
                {position, size} of ¬
                scroll area 2 of ¬
                splitter group 1 of ¬
                window 1 of ¬
                application process "Preview"
        else
            set windowPositionSize to ¬
                {position, size} of ¬
                scroll area 1 of ¬
                splitter group 1 of ¬
                window 1 of ¬
                application process "Preview"
        end if
    end tell
end if

set x to item 1 of item 1 of windowPositionSize
set y to item 2 of item 1 of windowPositionSize
set w to item 1 of item 2 of windowPositionSize
set h to item 2 of item 2 of windowPositionSize

--  # Build out the date time string for the screen shot file name.

set currentDate to (current date)
set d to day of currentDate
set m to month of currentDate as integer
set yr to year of currentDate
set t to time string of currentDate

--  # Replace ':' with '.' in the 'time string'.

set text item delimiters to ":"
set t to text items of t
set text item delimiters to "."
set t to t as string
set text item delimiters to ""

--  # Set the screen shot file name.

set ssFileName to ¬
    (documentName as string) & ¬
    " | " & ¬
    pageNumber & ¬
    " | " & ¬
    "Snap Shot " & yr & "-" & d & "-" & m & " at " & t & "." & ssFileType

--  # Define the shell command.

set myShellCMD to ¬
    "screencapture -R" & x & "," & y & "," & w & "," & h & " -t " & ¬
    ssFileType & space & (saveToLocation & ssFileName)'s quoted form

--  # Take the screen shot.

do shell script myShellCMD

For the Run Shell Script action in the Automator Service/Quick Action, replace the entire default code with the example AppleScript code, and then edit the first three variables as wanted, if the default values are not what you want.


Note: The example AppleScript code is just that and does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.