指定の画像をJPEG形式+指定圧縮率で保存するAppleScriptです。
現在は、これをさらに発展させて指定ファイルサイズになるまで圧縮率を順次変更して動的に圧縮率を変更して保存する、という処理に発展し、これは日常的に(SNSなどでアップロード可能なファイルサイズに制限があるサイトに画像掲載を行うさいに)利用しています。
AppleScript名:指定画像をJPG形式で保存 |
— Created 2017-02-05 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" set aFile to POSIX path of (choose file of type {"public.image"} with prompt "Select Image A") set aImage to current application’s NSImage’s alloc()’s initWithContentsOfFile:aFile set fRes to retUUIDfilePath(aFile, "jpg") of me set sRes to saveNSImageAtPathAsJPG(aImage, fRes, 1.0) of me on retUUIDfilePath(aPath, aEXT) set aUUIDstr to (current application’s NSUUID’s UUID()’s UUIDString()) as string set aPath to ((current application’s NSString’s stringWithString:aPath)’s stringByDeletingLastPathComponent()’s stringByAppendingPathComponent:aUUIDstr)’s stringByAppendingPathExtension:aEXT return aPath end retUUIDfilePath –NSImageを指定パスにJPEG形式で保存、qulityNumは0.0〜1.0。1.0は無圧縮 on saveNSImageAtPathAsJPG(anImage, outPath, qulityNum as real) 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 NSJPEGFileType) |properties|:{NSImageCompressionFactor:qulityNum}) set aRes to (myNewImageData’s writeToFile:newPath atomically:true) as boolean return aRes –true/false end saveNSImageAtPathAsJPG |
More from my site
(Visited 68 times, 1 visits today)