Select Area of Screen and Get Image

Post 4 of the thread located here contains my script which uses the screencapture utility to create an image and then performs OCR on the image. This works reasonably well but I would prefer not to use screencapture.

Is there an ASObjC approach that I can use to replace the screencapture code? I did some Google research but found nothing. Thanks!

How about ScreenCaptureKit.framework ?



Thanks Piyomaru. I’ll give ScreenCaptureKit a try.

Apple have build-in screen capture.
If you do shift + command + 4 it will copy the screen capture to NSPasteboard.
You could then use changeCount method from NSPasteboard class to trigger
your OCR to convert the image to text and update your NSPasteboard.

In other words you could use shift + command + 4 to capture an area of the screen and next have string in NSPasteboard instead of a capture bitmap.

Reference: GitHub - RhetTbull/textinator: Simple MacOS StatusBar / Menu Bar app to automatically detect text in screenshots

This code has a error it will display dialog at first run… but ignore that.
Every time you make shift + command + 4 the display dialog will show
You could use this trigger to do OCR with your screen capture.

use framework "AppKit"
use framework "Foundation"
use scripting additions

property lastChangeCount : missing value

on idle
	my performSelector:"checkPasteboard:" withObject:(missing value) afterDelay:0
	return 0.25
end idle

on checkPasteboard:sender
	set pasteboard to current application's NSPasteboard's generalPasteboard()
	set currentChangeCount to pasteboard's changeCount()
	if (currentChangeCount is lastChangeCount) then return
	display dialog "Pasteboard changed: "
	set lastChangeCount to currentChangeCount
	log lastChangeCount
end checkPasteboard: