Out of desperation I tried adding delays where I thought they shouldn’t be necessary, with the former ones removed, and it works now.
A big thank you to Yvan, and also to Shane Stanley, who had input. Everythings much, much faster.
on jpegFromClipToPath:thePath compressFactor:compFactor -- 0.0 = max compression, 1.0 = none
try
tell current application to delay 0.2
set pb to current application's NSPasteboard's generalPasteboard() -- get pasteboard
tell current application to delay 0.2
set theData to pb's dataForType:"public.tiff" -- get tiff data off pasteboard
if theData = missing value then error "No tiff data found on clipboard"
set newRep to current application's NSBitmapImageRep's imageRepWithData:theData
set theData to (newRep's representationUsingType:(current application's NSJPEGFileType) |properties|:{NSImageCompressionFactor:compFactor, NSImageProgressive:false})
say 18
set theResult to (theData's writeToFile:thePath atomically:true)
say theResult as text
return (theResult = 1)
on error errmsg
tell application "System Events" to display dialog "jpegFromClipToPath " & errmsg
end try
end jpegFromClipToPath:compressFactor: