Adding a rectangle to an image?

Is there a way to overlay a rectangle onto an image? I have been scraping my graded quizzes from an online class and some answers overlay a rectangle over an image using CSS. The Notes app doesn’t seem to catch the CSS overlay, so I thought perhaps I could use the CSS border information to draw a BezierPath or create a rectangle image from the CSS border and then merge the images?

I have learned a lot from reading the posts here, so I am hoping that I can get pointed in the right direction.

This might get you started:

use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions



set thePath to POSIX path of (choose file)
set inFile to current application's NSURL's fileURLWithPath:thePath
set outFile to inFile's URLByDeletingPathExtension()'s URLByAppendingPathExtension:"jpg"
set theImage to current application's NSImage's alloc()'s initWithContentsOfURL:inFile
set {width:theWidth, height:theHeight} to theImage's |size|()

theImage's lockFocus()
set theBezier to current application's NSBezierPath's bezierPathWithRect:{{theWidth / 4, theHeight / 4}, {theWidth / 2, theHeight / 2}}
theBezier's setLineWidth:1
current application's NSColor's whiteColor()'s |set|()
theBezier's fill()
current application's NSColor's blackColor()'s |set|()
theBezier's stroke()
theImage's unlockFocus()

set theData to theImage's TIFFRepresentation()
set newRep to current application's NSBitmapImageRep's imageRepWithData:theData
set theData to (newRep's representationUsingType:(current application's NSJPEGFileType) |properties|:{NSImageCompressionFactor:0.8, NSImageProgressive:false})
set theResult to (theData's writeToURL:outFile atomically:true)

Thanks for this! I’ll give it a try when I have some time, but it looks close to what I need.