AppleScript - open Screenshot app and start recording screen (Mac mini M4)

I need to record the screen using the Screenshot application. This script launches the Screenshot app, but it does not start recording:

tell application “Screenshot”
activate
end tell
delay 3
tell application “System Events”
keystroke return
end tell

danielA1. The Screenshot app is not scriptable, which means GUI scripting has to be used. I’m not good with GUI scripting and can’t help you there.

An alternative you might consider is the screencapture utility. I ran this script by way of Script Editor then the Script Menu without issue on my Sequoia computer An error will be reported if necessary permissions have not been set. There are additional screencapture options–for example you can set the area of the screen to be captured–but the screencapture interactive option does not seem to work.

--set video length in seconds
set videoLength to 15

--get date stamp for file name
tell ((current date) as «class isot» as string) to set dateStamp to text 1 thru 4 & text 6 thru 7 & text 9 thru 10 & space & text 12 thru 13 & text 15 thru 16 & text 18 thru 19

--set path to video file
--an error will be reported if the file exists
set videoPath to (POSIX path of (path to desktop)) & "Screen Video " & dateStamp & ".mov"

--start screen capture
do shell script "screencapture -V " & videoLength & " " & quoted form of videoPath
1 Like

DanielA1. I fumbled around a bit and the following appears to do what you want. I tested this on my Sequoia computer and it worked as expected. An advantage of this approach is that you can stop the recording whenever desired.

set theDelay to 1 --test different values

do shell script "open -a Screenshot"

delay theDelay

tell application "System Events" to tell application process "screencaptureui"
	click checkbox "Record Entire Screen" of window "Window"
	delay theDelay
	click button "Record" of window "Window"
end tell
1 Like

Thank you very much.

How can I stop recording screen? You can use with delay.

danielA1. There are three approaches that I’m aware of.

The first approach is to use my first script which allows you to set a delay. However, this does not permit you to stop the recording prior to expiration of the delay.

The second approach is to put the delay in my second script followed by code that activates the Screenshot app and then uses GUI scripting to click the stop button. However, this would be recorded, so this approach is a bad one.

A third approach is to use GUI scripting to click on the button that is shown in the menu bar when the recording is ongoing. The trouble with this option is that it’s uses fixed screen coordinates and will fail if the screen coordinates of the recording button change. However, the following worked reliably on my computer.

do shell script "open -a Screenshot"
delay 0.5 --test different values

tell application "System Events" to tell application process "screencaptureui"
	click checkbox "Record Entire Screen" of window "Window"
	delay 0.5 --test different values
	click button "Record" of window "Window"
end tell

delay 15 --set to desired value

tell application "System Events" to click at {1542, 12} --set to correct values

There might be a fourth option which is to use GUI scripting to click on the Screenshot recording button in the menu bar. I don’t know how to do this–perhaps another forum member might.