—
– Created by: Piyomaru Software
– Created on: 2019/01/29
—
– Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
use framework "AppKit"
property |NSURL| : a reference to current application’s |NSURL|
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 NSScreen : a reference to current application’s NSScreen
property NSBezierPath : a reference to current application’s NSBezierPath
property NSPNGFileType : a reference to current application’s NSPNGFileType
property NSBitmapImageRep : a reference to current application’s NSBitmapImageRep
set tCol to choose color
copy tCol to {rVal, gVal, bVal}
set aRadius to 12
set aNSColor to makeNSColorFromRGBAval(rVal, gVal, bVal, 65535, 65535) of me
set aNSImage to makeRoundedNSImageWithFilledWithColor(100, 100, aNSColor, aRadius) of me
set aPath to NSString’s stringWithString:(POSIX path of (choose file name))
set bPath to aPath’s stringByAppendingPathExtension:"png"
set aRes to saveNSImageAtPathAsPNG(aNSImage, bPath) of me
–指定サイズのNSImageを作成し、指定色で塗ってNSImageで返す、anRadiusの半径の角丸で
on makeRoundedNSImageWithFilledWithColor(aWidth, aHeight, fillColor, anRadius as real)
set anImage to NSImage’s alloc()’s initWithSize:(current application’s NSMakeSize(aWidth, aHeight))
anImage’s lockFocus()
—
set theRect to {{x:0, y:0}, {height:aHeight, width:aWidth}}
set theNSBezierPath to NSBezierPath’s bezierPathWithRoundedRect:theRect xRadius:anRadius yRadius:anRadius
—
fillColor’s |set|() –色設定
theNSBezierPath’s fill() –ぬりつぶし
—
anImage’s unlockFocus()
return anImage
end makeRoundedNSImageWithFilledWithColor
–aMaxValを最大値とする数値でNSColorを作成して返す
on makeNSColorFromRGBAval(redValue as integer, greenValue as integer, blueValue as integer, alphaValue as integer, aMaxVal as integer)
set aRedCocoa to (redValue / aMaxVal) as real
set aGreenCocoa to (greenValue / aMaxVal) as real
set aBlueCocoa to (blueValue / aMaxVal) as real
set aAlphaCocoa to (alphaValue / aMaxVal) as real
set aColor to NSColor’s colorWithCalibratedRed:aRedCocoa green:aGreenCocoa blue:aBlueCocoa alpha:aAlphaCocoa
return aColor
end makeNSColorFromRGBAval
–NSImageを指定パスにPNG形式で保存
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