An easier way to capture/insert coordinates and area

I'm posting these macros for feedback and hoping to get some improvement suggestions or features added.

Background
I do a lot of image scraping so I'm often using Actions like Screen Capture Area. This action requires the user to manually enter in the X,Y, width, and height values, which is tedious to both acquire and enter in.

Screen Capture Area visual reference:

28pm

To remedy this, I recently created some macros to capture the values.

Capture Macros
Save- CursorXYTopLeft.kmmacros (1.6 KB)
Save- CursorXYBottomRight.kmmacros (1.6 KB)

Usage:
Use CMD-Option-W to capture the top left corner and CMD-Option-E to capture the bottom right corner of your target.


Next, I created a macro to write these values into the Screen Capture area:

Write Macro
Insert Capture Saved Area.kmmacros (56 KB)

Usage: Tap CMD-Option-1 while editing a macro and the coordinates for Capture Saved Area will be inserted along with the action.

Question/Request

  1. I'm writing macros to write macros! Is there a better way to capture and write these values rather than doing it manually or hacking around like I've done?

  2. @peternlewis - If not, would you consider adding a "Get" button to capture an area, similar to the way it's done in Actions like Click At Found Image ? (seen below). Obviously "Get" only has a point to capture and this needs to grab the width and height as well. I would expect it to be visually similar to the Mac's built-in CMD-Shift-4 screen capture feature, but instead it would capture coordinates and not an image. The "Get" button also has a timer so I'm not sure how it would work for an area but I'm sure you would have some ideas.

Hey Greg,

TLDR...   :sunglasses:

I just scanned through it.

One possibly useful hint if you're creating very similar macros is to:

  1. Create a template macro.
  2. Export the Macro XML and save it.

Then in your creation macro:

  1. Make a copy of the template.
  2. Massage the XML template text as needed -- for instance change coordinate values.
  3. Import the macro.

See Peter's post here:

Script KM to create macros

-Chris

I may not understand your use case, but it's not obvious to me why you don't just use (or automate) the standard macOS screencapture tools. @Tom (and others) have posted some nice macros to automate them.

But if you really do a lot of screen captures, then you may find it worthwhile to invest in a tool like SnagIT.

It is both very powerful and easy to use. Lots of features like initially auto-select a capture area, then you can adjust. Great annotation/editing tools. I've been using SnagIT for decades, and have never found anything better.

It ain't cheap. But like a lot of good tools, if you use them often they will quickly pay for themselves.

Whatever your choice, good luck.

Thanks Chris - this seems to handle the creation of additional macros, whereas I'm looking for an optimized way to insert values into an existing macro.

I also don't think it would be easier to edit XML when trying to capture image coordinates and area.

Hey Greg,

That's doable.

Here's an example with a static existing macro:

----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2018/07/12 22:40
# dMod: 2018/07/12 22:40 
# Appl: Keyboard Maestro, Keyboard Maestro Engine
# Task: Change the XML of an existing action in an existing macro.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Keyboard_Maestro, @Keyboard_Maestro_Engine, @Change, @XML, @Action, @Macro
----------------------------------------------------------------

tell application "Keyboard Maestro"
   # A specified macro:
   set workingMacro to macro id "AFB1EE80-3A76-4E58-A0E8-4C742CBC5C5F" of macro group id "0649F9D7-60E0-4367-83AE-DFFF5CA15C00" of application "Keyboard Maestro"
   tell workingMacro
      tell action 1
         set actionXML to its xml
         set newXML to kmReplace(findPattern, replacePattern, dataStr, true, true, true) of me
         set xml to newXML
      end tell
   end tell
end tell

----------------------------------------------------------------
--» HANDLERS
----------------------------------------------------------------
on kmReplace(findPattern, replacePattern, dataStr, regExBool, caseBool, tokensBool)
   tell application "Keyboard Maestro Engine"
      set foundDataList to search dataStr for findPattern replace replacePattern ¬
         regex regExBool case sensitive caseBool process tokens tokensBool
   end tell
end kmReplace
----------------------------------------------------------------

Here's how you might work with the selected macro:

tell application "Keyboard Maestro"
   set workingMacro to item 1 of (get selected macros)
   tell workingMacro
      set theAction to first action whose name starts with "move and click"
   end tell
end tell

So you might have 1 or more macros to capture the necessary coordinates and then another one to pass them to the working macro.

On the other hand you could just pass the captured coordinates to the working macro directly and make as many passes as necessary.

-Chris

1 Like

My use case is not so much the capture of the image as it is the capture of the area where the image might appear later.

Simple use case example

Let's say I want to check if a forum post has been moved from the last known category.

Please ignore that we're doing this with a web page. I am normally capturing images from a video feed, but this is the simplest way to demonstrate my use case

Steps to write this macro

  1. Screenshot the current category image to look for later:
    59pm

  2. Insert an If Then Else action:
    08am

  3. Select New Condition > Found Image Condition, and then change all screens to area:

  4. Insert the captured image from #1:

  5. Manually get the width, height, X, and Y where the image is expected to appear. Here I use the CMD-Option-4 Mac screen capture feature which gives me a way to quickly see the TopLeft coordinate and the BottomRight coordinate. I don't know of a way to screen capture the coordinates I can see when using the screen capture tool so apologies that I don't have a picture for this one.

I then can calculate everything I need to fill in the area values:

Length = XBottomRightCoordinate - XTopLeftCoordinate = 371 - 201 = 170
Height = YBottomRightCoordinate - YTopLeftCoordinate = 183 - 157 = 26
X = XTopLeftCoordinate (201)
Y = YTopLeftCoordinate (257)

  1. Insert these values:

  2. Add a notification for both cases and a hot key trigger:

I can now use CMD-Option-4 when on the forum post page to tell me if it's in the same category or not.

Full Macro here:
Example Macro.kmmacros (30 KB)

Note that the hardest work being done here by the user is not the capturing of the image itself, but the capturing of the coordinates where we want to look for the image to appear. My goal is to streamline the capturing of these coordinates.

I've written that one macro to do Screen Capture Area but it's a bit of a hack and I'd need to write a different macro for various Actions where area is used.

There is nothing wrong with using Keyboard Maestro to help automate Keyboard Maestro. The Keyboard Maestro editor is just a normal application like many others on the Mac, complete with places it doesn't work the way you want it to work.

Yes, this is possible, “its just code”. Its on the todo list, but when or if it will happen is an open question. Area specification happens in Keyboard Maestro in a number of places related to finding or capturing images, as well as window positioning, but it is not necessarily a highly used feature, and so the justification for implementing some sort of screen capture-esque UI to allow specifying the rectangles (which is do-able, but definitely non-trivial) is a challenge.

I will give this a try, but I'm definitely a novice at the AppleScript stuff.

Thanks @peternlewis - yes that's true and I've written macros for KM in the past.

The problem here is that a I have to write a macro for every case where area appears. I'm going to try @ccstone's suggestion to take out some of the guesswork. The macro I posted for Screen Capture Area works well in all cases I've found but I'm having less success with other area references.

The consideration is much appreciated.

Side Note:
While doing this, I've noticed the order of the area values isn't always consistent.

2 Likes

Is this with Keyboard Maestro 8? I know there were issues in the past, but I thought I had dealt with them all, and if I look currently in 8.2.1, I get the correct symbols and meaning (left, top, width, height) in both cases.

Nope, it's 7.3.1. Looks like you've got it in order for 8.

I forget exactly why I'm not using 8 but it interfered with some other macros I wrote for KM.

OK, I'm glad to hear it is resolved.

Generally macros that change working behaviour between versions are fragile, typically caused by either timing issues or faulty assumptions. If there are specific macros you are having troubles with, post them to the forum and I'm sure we can help resolve them.

I highly recommend the upgrade to KM 8 -- now at 8.2.2.
Like Peter said, if you have any trouble with macros that break, we will help you if you need us.