Screen capture ' stencil '

Dear All, thanks for looking at this. I have some skills with AppleScript now - I have received a lot of help here.

I’m trying to do something new and have not got much idea where to start. What I want to do is to be able capture a specific section of the screen and have a ‘sight’ which can guide me. (The Apple options of whole screen, window or selected area don’t work well for what I want to do.)
It is surprisingly difficult to get your start position dead right and then capture the fixed square thats needed.
So, I want to try to build a "stencil’

To describe simply, i want to be able start the script -
1.) a box will appear on the screen
2.) that box will have fixed dimensions (say 400px x 400px)
3.) The box is moved around the screen using the mouse.
4.) When the right location is reached, the mouse button is clicked.
5.) Whatever is in the box is captured and saved as a png.

It will only need to work on windows with still images, i.e. no video)

I am able to create a dialogue for capturing the default box sizes and where to save files and so on.
I cannot find anything in the script dictionaries for scripting the mouse beyond emulating mouse clicks.

IS there anyone who ca point me in the right direction?

Thanks again

Max

There is a fine CLI named Cliclick which ia able to move the mouse and click it.
Look at https: //www.bluem.net/en/projects/cliclick/

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 6 juin 2019 15:39:27

Yvan,

Thanks as usual. That should keep me reading for a while.

At first glance, it does not look like i can use it, but I’ll read the documentation first.

Thank you .

Max

Dear Yvan.

I have done what we Brits call a bodge. I have produced an image file of exactly the size of screen capture i want to do. I then place this OVER the area of the image that I will want to select. When the image is obscuring the exact area I want, i know where to start and stop my screen grab.

I then click on the lower window which brings my target image to the front and do a Ctrl+ Shift +4 screen grab.

Its not even a workaround really but I was intimidated by CliClick!

What is the French term for a “right bodge”?

Thanks

Max

I thought this might be done with the macOS screenshot utility but that doesn’t appear to be the case.

It’s easy to write a script that captures a specific area of the screen with the screencapture command-line utility. The hard part, of course, is to select the area to be captured.

Just to be see how this might work (not for actual use), I have used a Terminal window to select the area to be captured. Just run the script, move the Terminal window to the location to be captured, and close the Terminal window (Command+W keyboard shortcut). The screenshot will be saved to the desktop.

This is an area of interest to me and I’ll have a look to see if I can find a utility that will do what the Terminal window does below.

property ssCounter : 1

set stencilApp to "Xee³"

--This is a solid-white image 400 x 400 pixels
set stencilImage to "Macintosh HD:Users:peavine:Documents:Stencil Window.png" as alias

tell application stencilApp
	activate
	open stencilImage
end tell

tell application "System Events" to tell process stencilApp
	repeat
		try
			set {x, y} to position of window 1
			set {w, h} to size of window 1
		on error
			exit repeat
		end try
		delay 0.5
	end repeat
end tell

set ssBounds to x & "," & y & "," & w & "," & h
set ssFolder to POSIX path of ((path to desktop) as alias)
set ssFile to ssFolder & "screenshot-" & (ssCounter as text) & ".png"

do shell script "screencapture -R" & ssBounds & " " & ssFile

set ssCounter to ssCounter + 1

tell application stencilApp to quit

EDIT: Rewrote to use Xee image viewer.

Hello Peavine.

This does what I needed as it is. I added the Terminal shut down lines at the end because it would not allow a second go!

It is surprisingly useful for me!
If there was a way to make the terminal window background transparent, it would be perfect, functionally.

Thanks for your effort

-- Peavine screen grab
-- Authored by Macsripter member Peavine of Prescott Arizona, very slightly modifed by HeadroomSan
-- Very much Beta at this stage

property ssCounter : 1

set ssFolder to POSIX path of ((path to desktop) as alias)

tell application "Terminal"
	activate
	set bounds of window 1 to {50, 50, 450, 450}
	
end tell

tell application "System Events"
	repeat
		try
			tell process "Terminal"
				exists window 1
				set {x, y} to position of window 1
				set {w, h} to size of window 1
			end tell
		on error
			exit repeat
		end try
		delay 1
	end repeat
end tell

set newBounds to x & "," & y & "," & w & "," & h

do shell script "screencapture -R " & newBounds & " " & ssFolder & "screenshot-" & (ssCounter as text) & ".png"

set ssCounter to ssCounter + 1
tell application "Terminal"
	tell application "System Events" to keystroke "exit"
	tell application "System Events" to keystroke return
	delay 2
end tell
tell application "Terminal"
	quit
end tell

I’m glad that’s helpful. I’ll do some more research on the transparent part.

BTW, you may want to play around with the delay to see what works best. I changed it from 1 to 0.2 and things seemed to work a bit better.

Peavine,

I’ll do that.

I’m going to add some more to this tomorrow and I’ll forward the resulting script.

Thanks again.

Max

Hey.

I advise you to automate any application that performs the most tasks you described in the steps above. After the all, AppleScript is designed to automate other applications, not to create an application without a foundation. For example, automate Finder for this task. Plus… Excel… or LibreOffice… and so on.

It is better that the application has its active window element, which can be clicked with the mouse and moved. I would not use Terminal for this.

Simple solution:


tell application "Preview" to activate
tell application "System Events" to tell application process "Preview"
	click menu bar item "File" of menu bar 1
	click menu item "Take Screenshot" of menu 1 of menu bar item "File" of menu bar 1
	click menu item "From Selection…" of menu "Take Screenshot" of menu item "Take Screenshot" of menu "File" of menu bar item "File" of menu bar 1
end tell

Continue as usual in “Preview” - to save screenshot were you need

Thanks. Your solution is simplest and best for me.

As for the best managing the “stencil” with fixed sizes and managing the mouse events, this involves programming with other programming languages. Or… involves using third-party applications as “Xee”. But both 2 your solutions was helpfull, so thanks again.