#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "Appkit"
use scripting additions
property refMe : a reference to current application
set appFileManager to refMe's NSFileManager's defaultManager()
###################################
#####設定項目
###################################
###画像に印字するテキスト
set strInputText to "美しい日本語" as text
###余白
set numPadding to 10 as integer
###フォントサイズ
set numFontSize to 24 as integer
###カーニング値
set numKern to -1.8 as number
### テキスト色の透過
set numAlpha to 0.8 as number
###################################
#####入力ダイアログ
###################################
set ocidUserDesktopPathURLArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidUserDesktopPathURL to ocidUserDesktopPathURLArray's firstObject()
set aliasDefaultLocation to (ocidUserDesktopPathURL's absoluteURL()) as alias
set listUTI to {"public.png", "public.jpeg", "public.tiff"}
set strPromptText to "ファイルを選んでください" as text
set strMesText to "ファイルを選んでください" as text
set listAliasFilePath to (choose file strMesText with prompt strPromptText default location (aliasDefaultLocation) of type listUTI with invisibles and multiple selections allowed without showing package contents) as list
###################################
#####出力先フォルダ
###################################
set aliasFilePath to (item 1 of listAliasFilePath) as alias
set strFilePath to (POSIX path of aliasFilePath) as text
set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidContainerDirPath to (ocidFilePath's stringByDeletingLastPathComponent)
set strContainerDirPath to (ocidContainerDirPath's stringByDeletingLastPathComponent) as text
set aliasDefaultLocation to (POSIX file strContainerDirPath) as alias
set strPromptText to "保存先フォルダを選んでください" as text
set strMesText to "保存先フォルダを選んでください" as text
set aliasDistDirPath to (choose folder strMesText with prompt strPromptText default location aliasDefaultLocation with invisibles without multiple selections allowed and showing package contents) as alias
###################################
#####テキスト印字位置
###################################
set listTextPosition to {"上左", "上中", "上右", "中央", "下左", "下中", "下右"} as list
try
set listResponse to (choose from list listTextPosition with title "選んでください" with prompt "テキスト位置を選んでください" default items (item 7 of listTextPosition) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed) as list
on error
log "エラーしました"
return "エラーしました"
end try
if (item 1 of listResponse) is false then
return "キャンセルしました"
end if
set strResponse to (item 1 of listResponse)
set recordTextPosition to {|上左|:1, |上中|:2, |上右|:3, |中央|:4, |下左|:5, |下中|:6, |下右|:7} as record
set ocidTextPositionDict to refMe's NSMutableDictionary's dictionaryWithDictionary:recordTextPosition
set numPosition to (ocidTextPositionDict's valueForKey:strResponse) as integer
###################################
##### フォント選択
###################################
set strDefailtsFont to "Osaka-Mono" as text
set appFontManager to refMe's NSFontManager
set appSharedMaanager to appFontManager's sharedFontManager()
set ocidFontList to appSharedMaanager's availableFonts()
set numDefaultsFont to (ocidFontList's indexOfObject:(strDefailtsFont)) as integer
set listFontList to ocidFontList as list
try
set listResponse to (choose from list listFontList with title "選んでください" with prompt "フォントを選んでください" default items (item numDefaultsFont of listFontList) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed) as list
on error
log "エラーしました"
return "エラーしました"
end try
if listResponse is false then
return "キャンセルしました"
end if
set strFontName to (item 1 of listResponse) as text
###################################
#####テキストの色
###################################
#####RGB16bitColorに16bitRGB値を格納
tell application "Finder"
set the listRGB16bitColor to (choose color default color {65535, 55535, 0, 0}) as list
log class of listRGB16bitColor
end tell
##########Color Picker Value 16Bit
set numRcolor16Bit to item 1 of listRGB16bitColor as number
set numGcolor16Bit to item 2 of listRGB16bitColor as number
set numBcolor16Bit to item 3 of listRGB16bitColor as number
##########Standard RGB Value 8Bit
set numRcolor8Bit to numRcolor16Bit / 256 div 1 as number
set numGcolor8Bit to numGcolor16Bit / 256 div 1 as number
set numBcolor8Bit to numBcolor16Bit / 256 div 1 as number
##########NSColorValue Float
set numRcolorFloat to numRcolor8Bit / 255 as number
set numGcolorFloat to numGcolor8Bit / 255 as number
set numBcolorFloat to numBcolor8Bit / 255 as number
###################################
#####テキスト印字位置
###################################
repeat with itemAliasFilePath in listAliasFilePath
set aliasFilePath to itemAliasFilePath as alias
set strFilePath to (POSIX path of aliasFilePath) as text
set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
###
set ocidFileName to ocidFilePathURL's lastPathComponent()
set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension
set ocidExtensionName to ocidFilePathURL's pathExtension()
###################################
#####画像読み込み
###################################
set ocidImageRep to (refMe's NSBitmapImageRep's imageRepWithContentsOfURL:(ocidFilePathURL))
###サイズ取得
set recordImageRepSize to ocidImageRep's |size|()
set numPointWidth to width of recordImageRepSize
set numPointHeight to height of recordImageRepSize
##ピクセルサイズ
set numPixelsWide to ocidImageRep's pixelsWide() as integer
set numPixelsHigh to ocidImageRep's pixelsHigh() as integer
###サイズをピクセルサイズに変更しておく (解像度を72にしておく)
set recordTempImageSize to {width:numPixelsWide, height:numPixelsHigh} as record
(ocidImageRep's setSize:recordTempImageSize)
###################################
#####テキスト生成
###################################
###設定用のレコード
set ocidTextAttr to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
###################
##描画する文字列
set ocidText to (refMe's NSString's stringWithString:(strInputText))
###################
###色指定
## set ocidTextColor to refMe's NSColor's blackColor
###RGBA 透過指定
set ocidTextColor to (refMe's NSColor's colorWithSRGBRed:(numRcolorFloat) green:(numGcolorFloat) blue:(numBcolorFloat) alpha:(numAlpha))
(ocidTextAttr's setObject:(ocidTextColor) forKey:(refMe's NSForegroundColorAttributeName))
###################
##システムフォント
# set ocidFont to (refMe's NSFont's systemFontOfSize:numFontSize)
###PS名指定でもいける?
set ocidFont to (refMe's NSFont's fontWithName:(strFontName) |size|:numFontSize)
(ocidTextAttr's setObject:(ocidFont) forKey:(refMe's NSFontAttributeName))
###################
##行間
set ocidStyle to refMe's NSParagraphStyle's defaultParagraphStyle
(ocidTextAttr's setObject:(ocidStyle) forKey:(refMe's NSParagraphStyleAttributeName))
###################
##カーニング
# set ocidKern to refMe's NSTextScalingStandard ##デフォルト
# set ocidKern to refMe's NSTextScalingiOS ###iOS最適化
set ocidKern to numKern
(ocidTextAttr's setObject:(ocidKern) forKey:(refMe's NSKernAttributeName))
###################
##ドロップシャドウ
set ocidShadow to refMe's NSShadow's alloc()'s init()
###テキスト色と同色にする場合
set ocidShadowColor to (ocidTextColor's colorWithAlphaComponent:0.8)
###色指定する場合
set ocidShadowColor to (refMe's NSColor's colorWithDeviceRed:0 green:0 blue:0 alpha:0.5)
(ocidShadow's setShadowColor:(ocidShadowColor))
(ocidShadow's setShadowOffset:(refMe's NSMakeSize(1, -1)))
(ocidShadow's setShadowBlurRadius:4)
(ocidTextAttr's setObject:(ocidShadow) forKey:(refMe's NSShadowAttributeName))
###################
####描画されるテキストボックスのサイズ
set ocidTextSize to (ocidText's sizeWithAttributes:(ocidTextAttr))
set ocidTextWide to ocidTextSize's width as integer
set ocidTextHigh to ocidTextSize's height as integer
###################################
#####描画位置
###################################
###ここは、お好みで色々できるか?
if numPosition = 1 then ###上左
set ocidTextOrigin to refMe's NSMakePoint(numPadding, (numPixelsHigh - ocidTextHigh) - numPadding)
else if numPosition = 2 then ###上中央
set ocidTextOrigin to refMe's NSMakePoint((numPixelsWide - ocidTextWide) / 2, (numPixelsHigh - ocidTextHigh) - numPadding)
else if numPosition = 3 then ###上右
set ocidTextOrigin to refMe's NSMakePoint((numPixelsWide - ocidTextWide - numPadding), (numPixelsHigh - ocidTextHigh) - numPadding)
else if numPosition = 4 then ###真ん中
set ocidTextOrigin to refMe's NSMakePoint((numPixelsWide - ocidTextWide) / 2, (numPixelsHigh - ocidTextHigh) / 2)
else if numPosition = 5 then ###左下
set ocidTextOrigin to refMe's NSMakePoint(numPadding, numPadding)
else if numPosition = 6 then ###下中央
set ocidTextOrigin to refMe's NSMakePoint((numPixelsWide - ocidTextWide) / 2, numPadding)
else if numPosition = 7 then ###右下
set ocidTextOrigin to refMe's NSMakePoint((numPixelsWide - ocidTextWide - numPadding), numPadding)
end if
###################################
#####NSGraphicsContext用のアートボード
###################################
##透過指定
set ocidSetColor to refMe's NSColor's clearColor
set ocidFormat to refMe's NSBitmapFormatAlphaFirst
set ocidColorSpace to refMe's NSCalibratedRGBColorSpace
####生成本処理
set ocidArtBoardRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(numPixelsWide) pixelsHigh:(numPixelsHigh) bitsPerSample:8 samplesPerPixel:4 hasAlpha:true isPlanar:false colorSpaceName:(ocidColorSpace) bitmapFormat:(ocidFormat) bytesPerRow:0 bitsPerPixel:32)
####Rectを生成(画像ピクセルサイズ)
set ocidArtBordRect to refMe's NSMakeRect(0, 0, numPixelsWide, numPixelsHigh)
###################################
#####テキスト描画 本処理
###################################
####NSGraphicsContextは透明アートボード
set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep))
###初期化
refMe's NSGraphicsContext's saveGraphicsState()
###透明アートボードでNSGraphicsContext初期化
(refMe's NSGraphicsContext's setCurrentContext:(ocidContext))
####読み込んだ画像をレンダリングする
(ocidImageRep's drawInRect:(ocidArtBordRect) fromRect:(ocidArtBordRect) operation:(refMe's NSCompositeSourceOver) fraction:1.0 respectFlipped:true hints:(missing value))
###テキスト描画する
(ocidText's drawAtPoint:(ocidTextOrigin) withAttributes:(ocidTextAttr))
####画像作成終了
refMe's NSGraphicsContext's restoreGraphicsState()
###解像度を戻す
set recordPointImageSize to {width:numPointWidth, height:numPointHeight} as record
(ocidArtBoardRep's setSize:recordPointImageSize)
###################################
#####保存オプション PNG
###################################
set ocidFileType to refMe's NSBitmapImageFileTypePNG
set ocidProperties to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
###インターレース
(ocidProperties's setObject:false forKey:(refMe's NSImageInterlaced))
###ガンマ
(ocidProperties's setObject:(1 / 2.2 as number) forKey:(refMe's NSImageGamma))
####NSImageColorSyncProfileData カラースペース
set ocidColorSpace to refMe's NSColorSpace's displayP3ColorSpace()
set ocidColorSpaceData to ocidColorSpace's colorSyncProfile()
###プロファイルを付ける
(ocidProperties's setObject:(ocidColorSpaceData) forKey:(refMe's NSImageColorSyncProfileData))
####出力用データに変換
set ocidNSInlineData to (ocidArtBoardRep's representationUsingType:(ocidFileType) |properties|:(ocidProperties))
###################################
#####保存ファイル パス処理
###################################
set strDistDirPath to (POSIX path of aliasDistDirPath) as text
set ocidDistDirPathStr to (refMe's NSString's stringWithString:(strDistDirPath))
set ocidDistDirPath to ocidDistDirPathStr's stringByStandardizingPath()
set ocidDistDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidDistDirPath) isDirectory:false)
set ocidSaveFileName to (ocidBaseFileName's stringByAppendingPathExtension:"png")
set ocidSaveFilePathURL to (ocidDistDirPathURL's URLByAppendingPathComponent:(ocidSaveFileName))
###################################
#####保存
###################################
set boolDone to (ocidNSInlineData's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error|:(reference))
end repeat
tell application "Finder"
open aliasDistDirPath
end tell
return "処理終了"