Automator Help: take Fixed-Position Screenshot, click “Next Page” and Repeat (that’s it!)

applescriptautomatorscreen capture

I don't know code which is really embarrassing for simple tasks like this one. All I'm trying to do is to make a Quick Action in Automator for this:

  1. take a screenshot based on two fixed points
  2. click a "next" button in an app (in my case; Kindle for Mac)
  3. Repeat X times

Caveat (if relevant): my screenshots capture in JPG format (not PNG).

Though my "watch me do" was not successful, the recording process did capture me depressing the app's page-advance button which I see is called "Next Page."

A workaround would be to use CliClick to push the advance button, if which I already have installed if "Next Page" might not work for whatever reason. The repeating screenshot would be a pre-defined pair of coordinates based on upper-left and lower-right (which I can get via CMD+SHIFT+4).

Thanks so much in advance! Any method to pull this off in Automator is fine by me. Cheers!

EDIT 1: added code for @ankii below here
EDIT 2: deleted what was added in EDIT 1 (no longer relevant)

Best Answer

Kindle is not AppleScript scriptable in that it does not contain an AppleScript dictionary within its application bundle; however it is, within limits, using System Events and UI Scripting. Note though that with UI Scripting one must not use the system for other things while the script is running as Kindle needs to remain frontmost while the script is running.

The following example AppleScript code, tested under macOS High Sierra took screen shots of the first 5 pages of the BASH Reference Manual:

set saveToLocation to POSIX path of (path to pictures folder)
set bookName to "BASH Reference Manual"
set pageCount to 5

set pathFileName to saveToLocation & bookName

tell application "Kindle" to activate

delay 0.5

tell application "System Events" to ¬
    tell window 1 of application process "Kindle" to ¬
        set {position, size} to {{693, 66}, {1006, 1089}}

repeat with i from 1 to pageCount

    set shellCMD to ¬
        "screencapture -R837,124,770,994 -t jpg '" & ¬
        pathFileName & " - Page " & ¬
        i & " of " & pageCount & ".jpg'"

    do shell script shellCMD
    delay 1.5
    --  # Press right-arrow key.
    tell application "System Events" to key code 124
    delay 1.5

end repeat

Note: The example AppleScript code is just that and does not contain any 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.