指定サイズの画像に対して指定の文字を描画して指定ファイル名のPNG画像を書き出すAppleScriptです。
AppleScript名:画像+文字作成テスト_v4 |
— Created 2015-07-31 by Takaaki Naganoya — Modified 2015-08-01 by Shane Stanley — Modified 2017-11-19 by Takaaki Naganoya / macOS 10.13のバグに対応 use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" set aWidth to 400.0 –幅 set aHeight to 200.0 –高さ set outPath to "~/Desktop/test.png" –書き出し先のファイルパス set fillColor to current application’s NSColor’s blackColor –塗り色 set drawColor to current application’s NSColor’s whiteColor –文字色 set aText to "ぴよまるソフトウェア" set {xPos, yPos} to {1, 5} –新規画像を作成して背景を塗る set anImage to makeImageWithFilledColor(aWidth, aHeight, outPath, fillColor) of me –画像に文字を塗る(参照渡し(call by reference)で、結果はaImage1に入る) drawStringsOnImage(anImage, aText, "HiraKakuStd-W8", 36.0, drawColor, xPos, yPos) of me –ファイル保存 set aRes to saveImageRepAtPathAsPNG(anImage, outPath) of me –画像のうえに指定の文字を描画して画像を返す on drawStringsOnImage(anImage, aText, aFontName, aPoint, drawColor) set retinaF to (current application’s NSScreen’s mainScreen()’s backingScaleFactor()) as real –> 2.0 (Retina) / 1.0 (Non Retina) set aString to current application’s NSString’s stringWithString:aText set aDict to current application’s NSDictionary’s dictionaryWithObjects:{current application’s NSFont’s fontWithName:aFontName |size|:aPoint, drawColor} forKeys:{current application’s NSFontAttributeName, current application’s NSForegroundColorAttributeName} set imageSize to anImage’s |size|() set textSize to aString’s sizeWithAttributes:aDict set xPos to ((width of imageSize) – (width of textSize)) / 2 / retinaF set yPos to ((height of imageSize) – (height of textSize)) / 2 / retinaF –文字描画開始 anImage’s lockFocus() aString’s drawAtPoint:(current application’s NSMakePoint(xPos, yPos)) withAttributes:aDict anImage’s unlockFocus() end drawStringsOnImage –指定サイズの画像を作成し、背景を指定色で塗る on makeImageWithFilledColor(aWidth, aHeight, outPath, fillColor) set anImage to current application’s NSImage’s alloc()’s initWithSize:(current application’s NSMakeSize(aWidth, aHeight)) –描画開始 anImage’s lockFocus() set theRect to {{x:0, y:0}, {width:aWidth, height:aHeight}} set theNSBezierPath to current application’s NSBezierPath’s bezierPath theNSBezierPath’s appendBezierPathWithRect:theRect fillColor’s |set|() –色設定 theNSBezierPath’s fill() –ぬりつぶし anImage’s unlockFocus() –描画ここまで return anImage –画像を返す end makeImageWithFilledColor –画像を指定パスにPNG形式で保存 on saveImageRepAtPathAsPNG(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 –成功ならtrue、失敗ならfalseが返る end saveImageRepAtPathAsPNG |
More from my site
(Visited 64 times, 1 visits today)
値渡しハンドラと参照渡しハンドラ – AppleScriptの穴 says:
[…] v3 ・画像+文字作成テスト_v4 ・Tag […]