use scripting additionsuse 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
When we copy a chart from Numbers, the clipboard is filled with these blocks of datas :
{{«class PNGf», 32844}, {«class 8BPS», 251290}, {GIF picture, 13608}, {«class jp2 », 24902}, {JPEG picture, 17032}, {TIFF picture, 435374}, {«class BMP », 432054}, {«class TPIC», 182620}}
I'm wondering if there is a way to grab the TIFF component and convert it into jpeg without using Preview + GUI scripting.
I assume that something like :
use scripting additions
use framework "Foundation"
use framework "AppKit"
on jpegFromPath:thePicture targetPath:thePath compressFactor:compFactor -- 0.0 = max compression, 1.0 = none
set destPath to current application's NSString's stringWithString:thePath
-- load the file as an NSImage
set theImage to current application's NSImage's alloc()'s initWithContentsOfFile:imagePath # Need to be edited
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:targetPath:compressFactor:
its jpegFromPath:(the clipboard as TIFF picture) targetPath:((path to desktop as text) & "theChart.jpg") compressFactor:1.0
If I understand well the question may be resumed to :
how must we edit the instruction :