use scripting additions
use framework "Foundation"
use framework "AppKit"
on jpegFromPath:imagePath compressFactor:compFactor -- 0.0 = max compression, 1.0 = none
-- build destination path
set pathNSString to current application's NSString's stringWithString:imagePath
set destPath to pathNSString's stringByDeletingPathExtension()'s stringByAppendingPathExtension:"jpg"
-- load the file as an NSImage
set theImage to current application's NSImage's alloc()'s initWithContentsOfFile:imagePath
if theImage = missing value then return false
-- get bitmap as data
set theData to theImage's TIFFRepresentation()
-- make imagerep from data
set newRep to current application's NSBitmapImageRep's imageRepWithData:theData
-- turn the imagerep into JPEG data
set theData to (newRep's representationUsingType:(current application's NSJPEGFileType) |properties|:{NSImageCompressionFactor:compFactor, NSImageProgressive:false})
-- write it to disk
set theResult to (theData's writeToFile:destPath atomically:true)
return (theResult = 1)
end jpegFromPath:compressFactor:
its jpegFromPath:(POSIX path of (choose file)) compressFactor:1.0