Hello
I renamed the Shane's handler from convertPICTFileAt to convertPictureFileAt The original name let think that the entry document is supposed to be a PICT one which is wrong. It may be one of the numerous format treated by NSImage. PICT of course but PNG, TIFF, GIF, PDF and EPS if I made no error are also accepted.
So, I added this edited version to my library. As you may see, we may use it to create jpg or png files.
use scripting additions use framework "Foundation" use framework "AppKit"
set thePicture to (path to desktop as text) & "créer clé El Capitan.eps"
my convertPictureFileAt:{POSIX path of thePicture, "png"}
on convertPictureFileAt:{posixPath, destExt} # may be "png" or "jpg" -- build new path set oldPath to current application's NSString's stringWithString:posixPath set newPath to oldPath's stringByDeletingPathExtension()'s stringByAppendingPathExtension:destExt # EDITED -- make NSImage from file set theImage to current application's NSImage's alloc()'s initWithContentsOfFile:oldPath -- get TIFF version as data set theData to theImage's TIFFRepresentation() -- make bitmap representation from TIFF data set bitmapImageRep to current application's NSBitmapImageRep's imageRepWithData:theData -- extract jpeg representation from bitmap if destExt = "png" then set theData to bitmapImageRep's representationUsingType:(current application's NSPNGFileType) |properties|:{NSImageCompressionFactor:1.0} -- between 0.0 and 1.0 else set theData to bitmapImageRep's representationUsingType:(current application's NSJPEGFileType) |properties|:{NSImageCompressionFactor:1.0} -- between 0.0 and 1.0 end if --save to file theData's writeToFile:newPath atomically:true end convertPictureFileAt:
I tested it with PICT, PNG, PICT, PDF and EPS formats on entry. Of course, it would be a bad idea to ask it to convert a jpg file into a jpg one, or a png file into a png one. Today I'm too tired but one day, I will add a test to skip the conversion if the source format is the exit required one.
Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) lundi 17 août 2015 22:51:45
|