Monday, August 19, 2019

#1 2019-06-06 03:55:15 am

Headroom San
Member
From:: Stratford upon Avon
Registered: 2016-07-15
Posts: 34

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


Beware the beast Man, for he is the Devil's pawn. Alone among God's primates, he kills for sport or lust or greed. Yea, he will murder his brother to possess his brother's land. Let him not breed in great numbers, for he will make a desert of his home and yours. Shun him; drive him back into his jungle lair, for he is the harbinger of death.


Filed under: capture, target, stencil

Offline

 

#2 2019-06-06 07:39:32 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3537

Re: Screen capture ' stencil '

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

Offline

 

#3 2019-06-06 08:39:19 am

Headroom San
Member
From:: Stratford upon Avon
Registered: 2016-07-15
Posts: 34

Re: Screen capture ' stencil '

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


Beware the beast Man, for he is the Devil's pawn. Alone among God's primates, he kills for sport or lust or greed. Yea, he will murder his brother to possess his brother's land. Let him not breed in great numbers, for he will make a desert of his home and yours. Shun him; drive him back into his jungle lair, for he is the harbinger of death.

Offline

 

#4 2019-06-06 09:38:10 am

Headroom San
Member
From:: Stratford upon Avon
Registered: 2016-07-15
Posts: 34

Re: Screen capture ' stencil '

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


Beware the beast Man, for he is the Devil's pawn. Alone among God's primates, he kills for sport or lust or greed. Yea, he will murder his brother to possess his brother's land. Let him not breed in great numbers, for he will make a desert of his home and yours. Shun him; drive him back into his jungle lair, for he is the harbinger of death.

Offline

 

#5 2019-06-06 09:59:18 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 133

Re: Screen capture ' stencil '

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.

Applescript:

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.

Last edited by peavine (2019-06-08 08:09:15 pm)


2018 Mac mini - macOS Mojave

Offline

 

#6 2019-06-06 10:22:57 am

Headroom San
Member
From:: Stratford upon Avon
Registered: 2016-07-15
Posts: 34

Re: Screen capture ' stencil '

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


Applescript:

-- 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


Beware the beast Man, for he is the Devil's pawn. Alone among God's primates, he kills for sport or lust or greed. Yea, he will murder his brother to possess his brother's land. Let him not breed in great numbers, for he will make a desert of his home and yours. Shun him; drive him back into his jungle lair, for he is the harbinger of death.

Offline

 

#7 2019-06-06 10:30:22 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 133

Re: Screen capture ' stencil '

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.


2018 Mac mini - macOS Mojave

Offline

 

#8 2019-06-06 10:32:45 am

Headroom San
Member
From:: Stratford upon Avon
Registered: 2016-07-15
Posts: 34

Re: Screen capture ' stencil '

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


Beware the beast Man, for he is the Devil's pawn. Alone among God's primates, he kills for sport or lust or greed. Yea, he will murder his brother to possess his brother's land. Let him not breed in great numbers, for he will make a desert of his home and yours. Shun him; drive him back into his jungle lair, for he is the harbinger of death.

Offline

 

#9 2019-06-07 12:42:45 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 133

Re: Screen capture ' stencil '

I couldn't find a command-line utility that would work as the stencil app. The Terminal app has a preference option for opacity and this actually works reasonably well (in a kludgey sort of way). To set opacity go to:

Terminal Preferences > Profiles > Text > Background Color & Effects

To work really well, additional steps might include:

* Create a Terminal profile for this specific purpose (so that you can use the Terminal with normal colors).
* Find some way to reduce or eliminate the command-line prompt.
* Adjust the window bounds in the script so that the screenshot doesn't include the area under the Terminal title bar.


2018 Mac mini - macOS Mojave

Offline

 

#10 2019-06-08 03:51:55 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 424

Re: Screen capture ' stencil '

Headroom San wrote:


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.



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.

Last edited by KniazidisR (2019-06-08 04:06:48 am)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#11 2019-06-08 08:06:51 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 133

Re: Screen capture ' stencil '

KniazidisR wrote:


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.



Using the Terminal to delineate the area of a screenshot is a kludge--no doubt about that. smile

An alternative--which remains a kludge but works a bit better--is to use a lightweight image viewer as the stencil app. I tried this using the Xee app. It works well and has the ability to be moved with a click-and-drag anywhere within the window.

I agree it would be best to automate an existing app--I just don't know what that app is.


2018 Mac mini - macOS Mojave

Offline

 

#12 2019-06-09 08:25:28 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 424

Re: Screen capture ' stencil '

Simple solution:

Applescript:


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


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#13 2019-06-09 09:03:45 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 133

Re: Screen capture ' stencil '

I understood that a basic requirement was the use of a "stencil" to delineate the area of the screen to be captured. If the OP is willing to select the screen area to be captured, then an alternative AppleScript would be:

Applescript:

property ssCounter : 1

set ssFolder to POSIX path of (path to desktop)
set ssFile to ssFolder & "screenshot-" & (ssCounter as text) & ".png"

do shell script "screencapture -i " & quoted form of ssFile

set ssCounter to ssCounter + 1

The Screenshot utility in macOS Mojave will do most of what the OP wants and, as a practical matter, that might be the best alternative. It involves one or two additional steps but it works well.

Last edited by peavine (2019-06-10 10:00:07 am)


2018 Mac mini - macOS Mojave

Offline

 

#14 2019-06-09 01:14:05 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 424

Re: Screen capture ' stencil '

peavine wrote:


The Screenshot utility in macOS Mojave will do most of what the OP wants and, as a practical matter, that might be the best alternative. It involves one or two additional steps but it works well.



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.

Last edited by KniazidisR (2019-06-09 01:39:45 pm)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)