Mojave - Shift Command 4 screenshots not programmable?

Just upgraded to Mojave.
I need to program screenshots and their co-ordinates via KM.
This used to work consistently in High Sierra.
Now it may work 1 out of 10 times in the app of KM, but have not got it to work at all in Preview or Safari which is where I need it.
I do know of the screen capture action, but need to take screenshots via Shift Command 4.
Basic test Macro is included.
Any help is appreciated

Testing Screenshot.kmactions (1.4 KB)

Do you, in fact, mean that you need to take screenshots of a selectable area whose coordinates you wish to be able to specify ?

You can do this with an Execute a Shell Script action, and the following command (which, in this instance, will save the screenshot to your Pictures folder):

screencapture -R0,0,100,100 -t jpg "$HOME/Pictures/Screenshot on $(date +'%Y-%m-%d at %H.%M.%S').jpg"
2 Likes

@CJK thank you.
How would I specify the active application window (and coordinates within it) and not the coordinates as they apply to the full screen?
This shell scripts coordinates are applied to the full screen and not the active window.

Also how would I use pre-set KM variables for the Left Top Width Height and name in that shell script? ie.
VarLeft
VarTop
VarWidth
VarHeight
VarName

Note: I'd like to use more KM type action if possible, no reason why, other than learning. And also that maybe there is a fix for why the Shift Command 4 cannot be programmed.
If I use the screen capture action, the images are always a little blurry, not really sharp.
I use the screen capture action to the system clipboard and then write the clipboard to a file. I've tried all file types and also checked and unchecked 'always nominal resolution' within the screen capture action.
Any help is appreciated.
(the shell script image is very sharp and clear of course, I'll go with that as long as I can get the KM variable 'over there' to be used. )

Hey Troy,

Window functions in a calculation action.

image

It can also be done with text-tokens in a set variable to text action, but it's slightly more complicated.

Take a Screenshot at a Specific Position with a Specific Size.kmmacros (5.5 KB)

This is broken in the current context, but perhaps @peternlewis can fix it.

Adding a specific release action fails as well.

-Chris

1 Like

@ccstone has already provided a good, thorough example of how to use the builtin KM window functions.

Without meaning to step on his toes, we can compact the workflow a bit by retrieving the window coordinates all at once instead of as four separate entities. This can then be fed directly into the shell script action:

screencapture -R $(cat) -t jpg "$HOME/Pictures/Screenshot on $(date +'%Y-%m-%d at %H.%M.%S').jpg"

(Yes, that's the entire workflow.)

3 Likes

Hey @CJK,

That works nicely, but I believe Troy is not capturing the entire window frame – just a subsection from the top-left corner.

-Chris

1 Like

Thank you both @ccstone and @CJK

Awesome solutions and info for my learning library!

2 Likes

Hey @CJK,

You can force screencapture to take a screenshot of the front window:

screencapture -w ~/Downloads/test.png

But this still takes a click on the part of the user to complete the screenshot.

Do you know of a non-3pty means to either click or to force the screenshot to finish?

-Chris

D'oh! *facepalm*

By "non-third party", are you also including Keyboard Maestro in this exclusion criterion ?

I assume you are, because you will have already considered getting Keyboard Maestro to issue the click for you. In which case, the other way that springs to mind (having used it recently to help another user on this forum) is using JavaScript for Automation and the Core Graphics API to issue a pair of mouse-down/mouse-up events at appropriate coordinates.

I expect it is the same Mojave issue that prevents simulated keys invoke system hot keys unless they are plain letters. I don't know whether Apple plan to fix this.

Is there some reason the Screen Capture action does not work?

I really would like to use the screen capture action, it would make this macro so much easier!
I use these screenshots in an email to send to clients.
When I use a shell script or Shift Command 4 to take the screenshot they are crisp and clear.
But when I use the Screen Capture mode, although the file that is written to disk looks crips and clear, once inserted into an email it looks very fuzzy.
Following is an example of how they look in mail.
First is Screen Capture not nominal resolution
Second is Screen Capture with nominal resolution
Third is Shift Command 4

I would really like to figure this out. I'm sure it's something I have set wrong or am doing incorrectly with the file type or inserting into mail.

I'm working with the shell script to take the screenshot.
When I insert the image into OS X mail from the finder where it was saved, it gets much smaller than the image is when I open it directly in the finder in preview.
I would like it to remain the same size.
What am I missing?

Hey @CJK,

Yes, and now.

I don't mind using Keyboard Maestro in this case, but I also want to explore other options.

The Core Graphics stuff looks interesting.

Thanks.

-Chris

Hey Peter,

Nyet. I'm still using macOS 10.12.6.

-Chris

Hey Troy,

Open such an image in Preview and hit I to Get-Info.

See if the image is 72dpi.

-Chris

Hey Troy,

Make sure Mail's image setting is set to “Actual Size”

image

-Chris

2 Likes

Here's my Keyboard Maestro solution:

Is yours much the same, or something different ?

Here's the code I've been playing around with to achieve the same sort of thing:

ObjC.import('Cocoa');
nil=$();

const sys = Application('System Events');

const win = sys.processes.whose({ 'frontmost': true }).windows[0];
const pos = {x: win.position()[0][0] + 100, y: win.position()[0][1] + 100};

const mousedownevent = $.CGEventCreateMouseEvent(nil, 
		                     	$.kCGEventLeftMouseDown,
		                     	pos,
		                     	nil);

$.CGEventPost($.kCGHIDEventTap, mousedownevent);
$.CFRelease(mousedownevent);
							       
const mouseupevent = $.CGEventCreateMouseEvent(nil, 
		                   	$.kCGEventLeftMouseUp,
		                   	pos,
		                   	nil);
							     
$.CGEventPost($.kCGHIDEventTap, mouseupevent);
$.CFRelease(mouseupevent);

This works fine in Script Editor, but it doesn't fully issue the click if run inside Keyboard Maestro. It may be because KM uses osascript (I wish it didn't), and perhaps the CG events need a GUI context to operate in.

Thankfully, FastScripts uses the native AppleScript engine (whatever the correct terminology for that is), so this script should work via that.

Happy to hear your thoughts. Have you come across any other/better methods ?

The image is 144 dpi - when using the shell script.
But when using the screen capture action it is 72.
And my mail 'image' was set to small, DOH,
you again, have really helped me. Thank you
I'll like to go back and see using the 'actual size' setting, if I can simplify my whole macro and use the screen capture action. !!
Although I'm able to use/modify the shell script if necessary.

1 Like

Not when the dpi is 72.

-Chris

1 Like