I’m trying to draw some beziers over an image (or an empty image for usage as overlay)
Works fine so far for rectangles and ovals.
When trying to draw straight lines I fail.
Let’s say there is an array with to value pairs {{10, 10}, {90, 90}} – inside a given rect from image.
I have to initalize a bezier, move the start {10, 10} using moveToPoint:, the second {90, 90} addLineToPoint:
I’m “kind of too blind” to figure out to make a “point” out of the value pair.
Then my misunderstanding is even deeper
How would I create a simple line?
property pointList : {{10, 10}, {10, 100}}
property theWidth : 200
property theHeight : 200
set theImage to cA's NSImage's alloc()'s initWithSize:(current application's NSMakeSize(theWidth, theHeight))
set pointArray to cA's NSArray's arrayWithArray:pointList
theImage's lockFocus()
set aPath to current application's NSBezierPath's bezierPath
repeat with pointObject in pointArray
set res to (pointArray's indexOfObject:(pointObject))
log res
if (res = 0) then
try
(aPath's moveToPoint:(pointObject))
on error errMsg
log errMsg
end try
else
try
(aPath's lineToPath:(pointObject))
on error errMsg
log errMsg
end try
end if
end repeat
theImage's unlockFocus()
Sorry for leaving out the stroke() but it wouldn’t help here since I got an error earlier
(-[NSBezierPath lineToPath:]: unrecognized selector sent to instance 0x600001b832a0)
Sorry, late night typing at work. That should have been lineToPoint:.
use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
property pointList : {{10, 10}, {10, 100}}
property theWidth : 200
property theHeight : 200
set theImage to current application's NSImage's alloc()'s initWithSize:(current application's NSMakeSize(theWidth, theHeight))
set pointArray to current application's NSArray's arrayWithArray:pointList
set aPath to current application's NSBezierPath's bezierPath()
repeat with pointObject in pointArray
set res to (pointArray's indexOfObject:(pointObject))
if (res = 0) then
(aPath's moveToPoint:(pointObject))
else
(aPath's lineToPoint:(pointObject))
end if
end repeat
theImage's lockFocus()
aPath's setLineWidth:4
current application's NSColor's blackColor()'s |set|()
aPath's stroke()
theImage's unlockFocus()
set theData to theImage's TIFFRepresentation()
set thePath to (POSIX path of (path to desktop)) & "Testing.tiff"
theData's writeToFile:thePath atomically:true
But one more question. My try of the above script was thought to draw segmented lines with 2 or 3 parts - that’s what the repeat loop was for.
For a single line I thought there is strokeLineFromPoint:({0, 0}) toPoint:({50, 50})
but that didn’t work as well