use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "QuartzCore"
use framework "AppKit"
set anImage to current application's NSImage's imageNamed:(current application's NSImageNameComputer)
--set aPath to POSIX path of (choose file of type {"public.image"})
--set aNSImage to current application's NSImage's alloc()'s initWithContentsOfFile:aPath
set newImage to my resizeNSImage:anImage toScale:10.0
set aDesktopPath to (current application's NSProcessInfo's processInfo()'s environment()'s objectForKey:("HOME"))'s stringByAppendingString:"/Desktop/"
set savePath to aDesktopPath's stringByAppendingString:((current application's NSUUID's UUID()'s UUIDString())'s stringByAppendingString:".png")
set fRes to saveNSImageAtPathAsPNG(newImage, savePath) of me
on resizeNSImage:aSourceImg toScale:imgScale
set aSize to aSourceImg's |size|()
set aWidth to (aSize's width) * imgScale
set aHeight to (aSize's height) * imgScale
set aRep to current application's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:aWidth pixelsHigh:aHeight bitsPerSample:8 samplesPerPixel:4 hasAlpha:true isPlanar:false colorSpaceName:(current application's NSCalibratedRGBColorSpace) bytesPerRow:0 bitsPerPixel:0
set newSize to {width:aWidth, height:aHeight}
aRep's setSize:newSize
current application's NSGraphicsContext's saveGraphicsState()
--current application's NSGraphicsContext's setCurrentContext:(current application's NSGraphicsContext's graphicsContextWithBitmapImageRep:aRep)
set theContext to current application's NSGraphicsContext's setCurrentContext:(current application's NSGraphicsContext's graphicsContextWithBitmapImageRep:aRep)
theContext’s setShouldAntiAlias:false - - ** This line cause error ***
theContext's setImageInterpolation:(current application's NSImageInterpolationNone)
aSourceImg's drawInRect:(current application's NSMakeRect(0, 0, aWidth, aHeight)) fromRect:(current application's NSZeroRect) operation:(current application's NSCompositeCopy) fraction:(1.0)
current application's NSGraphicsContext's restoreGraphicsState()
set newImg to current application's NSImage's alloc()'s initWithSize:newSize
newImg's addRepresentation:aRep
return newImg --Unnecessary??
end resizeNSImage:toScale:
on saveNSImageAtPathAsPNG(anImage, outPath)
set imageRep to anImage's TIFFRepresentation()
set aRawimg to current application's NSBitmapImageRep's imageRepWithData:imageRep
set pathString to current application's NSString's stringWithString:outPath
set newPath to pathString's stringByExpandingTildeInPath()
set myNewImageData to (aRawimg's representationUsingType:(current application's NSPNGFileType) |properties|:(missing value))
set aRes to (myNewImageData's writeToFile:newPath atomically:true) as boolean
return aRes
end saveNSImageAtPathAsPNG