1

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)

2
  • the cliclick is what I was about to recommend too. Can you upload: 1 Open you watch me do in automator. 2 select the step where it clicks the next button, so that its highlighted. 3 press command + c 4 Open Script Editor.app and paste. 5 upload the script that you get. apple.stackexchange.com/a/378287/313842
    – anki
    Commented Jun 11, 2020 at 17:55
  • Thanks for helping me with this! I pasted the resulting code above in the original post, so it would display as it did when I pasted it in Script Editor. Thanks so much for helping me with this!
    – Ryan
    Commented Jun 11, 2020 at 19:58

1 Answer 1

1

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.

8
  • I'm super excited to try this. Will report back asap (within 60 min). Sorry for the novice question, but which set of four numbers are for my screenshot corners? I assume these are the ones just after "screencapture" but the "R" throws me off. Again, I don't know code unfortunately. Thanks in advance for your time. Can't wait to try this out!
    – Ryan
    Commented Jun 11, 2020 at 20:59
  • @Ryan, In Terminal run screencapture --help and near the bottom you'll see: -R<x,y,w,h> capture screen rect Also the e.g., set {position, size} to {{693, 66}, {1006, 1089}} can be obtained using tell application "System Events" to get {position, size} of window 1 of application process "Kindle" Commented Jun 11, 2020 at 21:01
  • Thanks! I'll run this command but must advise that this sounds over my head a little. I should clarify my desire: to manually set the screen-capture coordinates. This is because I want the images to be cropped to within the "page" edges of the displayed ebook pages as they advance--instead of screenshots of the entire Kindle app window. Sorry if I read that wrong. "I know not what I do" is the understatement here, in my case ;)
    – Ryan
    Commented Jun 11, 2020 at 21:21
  • @Ryan, I use the {position, size} of window 1 of application process "Kindle" as an example to do just that; however, it's the -R<x,y,w,h> that is being captured and for me it was just the page of the document, not the window itself. Commented Jun 11, 2020 at 21:33
  • Thanks for the help! Everything works splendidly except for the lower-right coordinates of the image crop, which is beyond where it needs to be. As mentioned in OP, I'm using crosshairs for x/y via CMD+SHIFT+4. I'm using upper-left as first set of values, and this one works, but lower-right now is instead picking a position that is instead equal to the bottom of the Kindle app window, and about a half inch beyond it to the right. Still can't figure out what's causing this.
    – Ryan
    Commented Jun 11, 2020 at 22:32

You must log in to answer this question.

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