▲Filtered Image

— Created 2017-03-21 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "QuartzCore"
use framework "AppKit"

property CIFilter : a reference to current application’s CIFilter
property CIColor : a reference to current application’s CIColor
property NSUUID : a reference to current application’s NSUUID
property CIImage : a reference to current application’s CIImage
property NSColor : a reference to current application’s NSColor
property NSString : a reference to current application’s NSString
property NSImage : a reference to current application’s NSImage
property NSDictionary : a reference to current application’s NSDictionary
property NSProcessInfo : a reference to current application’s NSProcessInfo
property NSPNGFileType : a reference to current application’s NSPNGFileType
property NSBitmapImageRep : a reference to current application’s NSBitmapImageRep

set aPath to POSIX path of (choose file of type {"public.image"})
set aNSImage to NSImage’s alloc()’s initWithContentsOfFile:aPath

set aCIColor to makeCIColorFromParams(0.0, 1.0, 0.0, 1.0) of me

set paramRec to {inputColor:aCIColor, inputIntensity:1.0}
set imgRes to execCIFilterWithNSImageAndParams(aNSImage, "CIColorMonochrome", paramRec) of me

set aDesktopPath to (NSProcessInfo’s processInfo()’s environment()’s objectForKey:("HOME"))’s stringByAppendingString:"/Desktop/"
set savePath to aDesktopPath’s stringByAppendingString:((NSUUID’s UUID()’s UUIDString())’s stringByAppendingString:".png")

set fRes to saveNSImageAtPathAsPNG(imgRes, savePath) of me

on execCIFilterWithNSImageAndParams(aNSImage, aFilterName as string, paramRec as record)
  set aDict to NSDictionary’s dictionaryWithDictionary:paramRec
set aCIImage to convNSImageToCIimage(aNSImage) of me
set aFilter to CIFilter’s filterWithName:aFilterName
aFilter’s setDefaults()
aFilter’s setValue:aCIImage forKey:"inputImage"
set aOutImage to aFilter’s valueForKey:"outputImage"
set keyList to aDict’s allKeys() as list
repeat with i in keyList
    set aVal to (aDict’s valueForKey:i) as list of string or string –as anything
aFilter’s setValue:aVal forKey:(i as string))
  end repeat
set newNSImage to convCIimageToNSImage(aOutImage) of me
return newNSImage
end execCIFilterWithNSImageAndParams

on makeCIColorFromParams(redValue, greenValue, blueValue, alphaValue)
  set aNSColor to NSColor’s colorWithCalibratedRed:redValue green:greenValue blue:blueValue alpha:alphaValue
set aCIColor to CIColor’s alloc()’s initWithColor:aNSColor
return aCIColor
end makeCIColorFromParams

on convCIimageToNSImage(aCIImage)
  set aRep to NSBitmapImageRep’s alloc()’s initWithCIImage:aCIImage
set tmpSize to aRep’s |size|()
set newImg to NSImage’s alloc()’s initWithSize:tmpSize
newImg’s addRepresentation:aRep
return newImg
end convCIimageToNSImage

on convNSImageToCIimage(aNSImage)
  set tiffDat to aNSImage’s TIFFRepresentation()
set aRep to NSBitmapImageRep’s imageRepWithData:tiffDat
set newImg to CIImage’s alloc()’s initWithBitmapImageRep:aRep
return newImg
end convNSImageToCIimage

on saveNSImageAtPathAsPNG(anImage, outPath)
  set imageRep to anImage’s TIFFRepresentation()
set aRawimg to NSBitmapImageRep’s imageRepWithData:imageRep
set pathString to NSString’s stringWithString:outPath
set newPath to pathString’s stringByExpandingTildeInPath()
set myNewImageData to (aRawimg’s representationUsingType:(NSPNGFileType) |properties|:(missing value))
set aRes to (myNewImageData’s writeToFile:newPath atomically:true) as boolean
return aRes –成功ならtrue、失敗ならfalseが返る
end saveNSImageAtPathAsPNG

★Click Here to Open This Script 

Please follow and like us:


メールアドレスが公開されることはありません。 * が付いている欄は必須項目です