use framework "Foundation"
use framework "AppKit"
on pngFromPath:imagePath toPath:newPath maxDimension:maxDim
set theImage to current application's NSImage's alloc()'s initWithContentsOfFile:imagePath -- load the file as an NSImage
-- calculate required sizes
set theSize to (theImage's |size|()) as record
set oldWidth to width of theSize
set oldHeight to height of theSize
if oldWidth > oldHeight then
set theWidth to maxDim
set theHeight to oldHeight / oldWidth * maxDim
else
set theHeight to maxDim
set theWidth to oldWidth / oldHeight * maxDim
end if
set newImage to current application's NSImage's alloc()'s initWithSize:(current application's NSMakeSize(theWidth, theHeight)) -- make new blank image
-- draw from original to new
newImage's lockFocus()
theImage's drawInRect:{origin:{x:0, y:0}, |size|:{width:theWidth, height:theHeight}} fromRect:(current application's NSZeroRect) operation:(current application's NSCompositeSourceOver) fraction:1.0
newImage's unlockFocus()
set theData to newImage's TIFFRepresentation() -- get bitmap as data
set newRep to current application's NSBitmapImageRep's imageRepWithData:theData -- make bitmap from data
set theData to (newRep's representationUsingType:(current application's NSPNGFileType) |properties|:{NSImageGamma:1.0}) -- turn the bitmap into PNG data
(theData's writeToFile:newPath atomically:true) -- write it to disk
end pngFromPath:toPath:maxDimension:
Call it with:
use theLib : script "<name of lib>"
theLib's pngFromPath:"/Users/shane/Desktop/Test.pdf" toPath:"/Users/shane/Desktop/Test.png" maxDimension:2000
--
Shane Stanley <
email@hidden>
<
www.macosxautomation.com/applescript/apps/>