SSブログ

テキスト(String)を画像(NSImage)化する [AppleScript辞書はつくれるか?]

image220627a.jpg

簡単にテキストを画像化する方法は2つある。

・drawAtPoint

  決められたポイントから文字を書き出す

 

set aText to "Abcひら漢字"

set {{x, y}, {w, h}} to imageView1's frame()

 

set myApp to current application

set theSize to myApp's NSMakeSize(w, h)

--set fontObj to myApp's class "NSFont"'s userFontOfSize:24.0  --|Font A|

set fontObj to myApp's class "NSFont"'s fontWithName:"Osaka" |size|:24.0  --|Font B|

set sizeValue to fontObj as list

set fontAttribute to (myApp's NSFontAttributeName) as list

set attributes to myApp's class "NSDictionary"'s dictionaryWithObjects:sizeValue forKeys:fontAttribute

set aString to myApp's class "NSString"'s stringWithString:aText

set anImage to myApp's class "NSImage"'s alloc()'s initWithSize:theSize

anImage's lockFocus()

aString's drawAtPoint:(myApp's NSMakePoint(1, 1)) withAttributes:attributes

anImage's unlockFocus()

imageView1's setImage:anImage

 

・drawInRect

  決められた範囲内に文字を書き出す

 

set aText to "Abcひら漢字"

set setSize to 48

set myApp to current application

set {{x, y}, {w, h}} to imageView1's frame()

set theSize to myApp's NSMakeSize(w, h)

--set fontObj to myApp's class "NSFont"'s userFontOfSize:setSize  --|Font A|

set fontObj to myApp's class "NSFont"'s fontWithName:"Osaka" |size|:setSize  --|Font B|

set sizeValue to fontObj as list

set fontAttribute to (myApp's NSFontAttributeName) as list

set attributes to myApp's class "NSDictionary"'s dictionaryWithObjects:sizeValue forKeys:fontAttribute

set aString to myApp's class "NSString"'s stringWithString:aText

set {width:w, height:h} to aString's sizeWithAttributes:attributes  --|大きさを計測|

set anImage to myApp's class "NSImage"'s alloc()'s initWithSize:theSize

anImage's lockFocus()

aString's drawInRect:(myApp's NSMakeRect(1, 1, w, h)) withAttributes:attributes

anImage's unlockFocus()

imageView1's setImage:anImage

 

drawAtPointは範囲を気にしなくても書き出せるが、drawInRectは書き出す範囲を指示しなければいけない。

逆に言えば、drawAtPointでは結果がどこまで書き出しているかを把握できないが、drawInRectなら把握することができる。

 

どちらを使うかは、その時の状況によるのでしょう。

 

 

(ソース全文)-スクリプトエディタ-

 

use AppleScript version "2.7"

use scripting additions

use framework "Foundation"

use framework "AppKit"

 

global myApp

global theWindow, mainView

global closeFlg

 

global imageView1

 

on run

my performSelectorOnMainThread:"mainMakeObject:" withObject:(missing value) waitUntilDone:true

--

log "End"

end run

 

on mainMakeObject:inputObj -- メインルーチン

set myApp to current application

set closeFlg to false

--

set aTitle to "文字列を画像として出力"

set aRect to {300, 150, 500, 350} -- theWindowSize

set moveCenter to true

set theWindow to my makeNewWindow(aTitle, aRect, moveCenter)

--

my makeObject()

--

repeat

if closeFlg then

my closeWin:theWindow

exit repeat

end if

delay 0.2

end repeat

end mainMakeObject:

 

on makeNewWindow(aTitle, aRect, moveCenter)

set {windowX, windowY, windowW, windowH} to aRect

set theRect to myApp's NSMakeRect(windowX, windowY, windowW, windowH)

set aScreen to myApp's class "NSScreen"'s mainScreen()

set aStyle to (myApp's NSWindowStyleMaskTitled as integer)

set aStyle to aStyle + (myApp's NSWindowStyleMaskClosable as integer)

set aStyle to aStyle + (myApp's NSWindowStyleMaskMiniaturizable as integer)

set aStyle to aStyle + (myApp's NSWindowStyleMaskResizable as integer)

set aBacking to myApp's NSBackingStoreBuffered

set aWindow to myApp's class "NSWindow"'s alloc()'s initWithContentRect:theRect styleMask:aStyle backing:aBacking defer:false screen:aScreen

tell aWindow

setDelegate_(me)

setTitle_(aTitle)

setMinSize_(myApp's NSMakeSize(300, 200))

setMaxSize_(myApp's NSMakeSize(10000, 10000))

setBackgroundColor_(myApp's class "NSColor"'s colorWithCalibratedRed:0.95 green:0.95 blue:0.95 alpha:1.0)

setDisplaysWhenScreenProfileChanges_(true)

setReleasedWhenClosed_(true)

makeKeyAndOrderFront_(me)

end tell

--

if moveCenter then

-- |windowをセンターに移動|

aWindow's |center|()

--

set theScreen to myApp's class "NSScreen"'s screens()

set {windowCenterX, winsowCenterY} to {windowX + (windowW / 2), windowY + (windowH / 2)}

repeat with screenNo from 1 to (count of theScreen)

set {{screenX1, screenY1}, {screenWidth, screenHeight}} to (item screenNo of theScreen)'s frame()

set {screenX2, screenY2} to {screenX1 + screenWidth, screenY1 + screenHeight}

if (windowCenterX > screenX1) and (windowCenterX < screenX2) and (winsowCenterY > screenY1) and (winsowCenterY < screenY2) then

set originX to screenX1 + (screenWidth / 2) - (windowW / 2)

set originY to screenY1 + (screenHeight / 2) - (windowH / 2)

(aWindow's setFrameOrigin:(myApp's NSMakePoint(originX, originY)))

exit repeat

end if

end repeat

end if

--

return aWindow

end makeNewWindow

 

(* 各オブジェクトの配置 *)

on makeObject()

set mainView to theWindow's contentView

set theWindowFrame to mainView's frame()

set {{windowX, windowY}, {windowWidth, windowHeight}} to theWindowFrame

--

set minXMargin to (myApp's NSViewMinXMargin) as integer -- 左の余白を柔軟に

set maxXMargin to (myApp's NSViewMaxXMargin) as integer -- 右の余白を柔軟に

set minYMargin to (myApp's NSViewMinYMargin) as integer -- 下の余白を柔軟に

set maxYMargin to (myApp's NSViewMaxYMargin) as integer -- 上の余白を柔軟に

set widthSizable to (myApp's NSViewWidthSizable) as integer -- 横方向の幅を柔軟に

set heightSizable to (myApp's NSViewHeightSizable) as integer -- 縦方向の幅を柔軟に

--

try

-- 新しくオブジェクトを配置する部分 --------------------

 

set theRect to myApp's NSMakeRect(windowX, windowY + 25, windowWidth, windowHeight - 25)

set imageView1 to myApp's class "NSImageView"'s alloc()'s initWithFrame:theRect

tell imageView1

setEditable_(false)

setImageFrameStyle_(myApp's NSImageFrameGrayBezel)

setImageAlignment_(myApp's NSImageAlignCenter)

setImageScaling_(myApp's NSImageScaleNone) -- NSImageScaleProportionallyDown

--setImage_(anImage)

end tell

mainView's addSubview:imageView1

--

set theRect to myApp's NSMakeRect(5, 1, 120, 24)

set button1 to myApp's class "NSButton"'s alloc()'s initWithFrame:theRect

tell button1

setBezelStyle_(1)

setButtonType_(0)

setTitle_("実行")

setTarget_(me)

setAction_("action1:")

setAutoresizingMask_(maxXMargin + maxYMargin)

end tell

mainView's addSubview:button1

----------------------------------------------------------

on error errText

log errText

end try

end makeObject

 

on action1:sender

set aText to "Abcひら漢字"

set setSize to 48

set {{x, y}, {w, h}} to imageView1's frame()

set theSize to myApp's NSMakeSize(w, h)

set fontObj to myApp's class "NSFont"'s fontWithName:"Osaka" |size|:setSize

set sizeValue to fontObj as list

set fontAttribute to (myApp's NSFontAttributeName) as list

set attributes to myApp's class "NSDictionary"'s dictionaryWithObjects:sizeValue forKeys:fontAttribute

set aString to myApp's class "NSString"'s stringWithString:aText

set {width:w, height:h} to aString's sizeWithAttributes:attributes

set anImage to myApp's class "NSImage"'s alloc()'s initWithSize:theSize

anImage's lockFocus()

aString's drawInRect:(myApp's NSMakeRect(1, 1, w, h)) withAttributes:attributes

anImage's unlockFocus()

imageView1's setImage:anImage

end action1:

 

(* ウインドウバーの閉じるボタンアクションを受けた時 *)

on windowShouldClose:sender

set closeFlg to true

end windowShouldClose:

 

(* ウインドウを閉じるための実際のアクション *)

on closeWin:aWindow

aWindow's orderOut:me

if (name of myApp) ≠ "Script Editor" then quit me

end closeWin:

 

 

 


nice!(0)  コメント(0) 
共通テーマ:パソコン・インターネット

nice! 0

コメント 0

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

TabViewの研究Xcode14からAppleScript.. ブログトップ

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。