動的にWindow+TextView+ボタンを作成し、指定フォントで指定文字を表示するAppleScriptです。
スクリプトエディタ上でControl+Command+Rによって実行します(メインスレッドで実行)。
AppleScript名:テキストビュー+ボタンを作成(フォント指定)v2 |
— Created 2016-02-01 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" use framework "Carbon" — AEInteractWithUser() is in Carbon property windisp : false property NSFont : a reference to current application’s NSFont property NSPredicate : a reference to current application’s NSPredicate property NSFontManager : a reference to current application’s NSFontManager set fRes to choose from list getEveryFontPSName() of me if fRes = {} or fRes = missing value then return set aFontName to contents of first item of fRes set aWidth to 600 set aHeight to 450 if current application’s AEInteractWithUser(-1, missing value, missing value) is not equal to 0 then return set aTitle to "テキストビューのじっけん/TextView Test" –Window Title set aButtonMSG to "OK" –Button Title –表示用テキストの作成 set aRes to checkExistenceOfFont(aFontName) of me if aRes = false then display dialog "There is no <" & aFontName & "> font. Designate another one." –No font return end if set bRes to retDefinedCharactersInFont(aFontName) of me set dispStr to listToStringUsingTextItemDelimiter(bRes, ", ") of me dispTextView(aWidth, aHeight, aFontName, dispStr, aButtonMSG, 180, aFontName, 36) of me on dispTextView(aWidth as integer, aHeight as integer, aTitle as text, dispStr, aButtonMSG as text, timeOutSecs as number, fontID, fontSize) set aColor to current application’s NSColor’s colorWithDeviceRed:0.0 green:0.0 blue:0.0 alpha:1.0 set (my windisp) to true –Text View+Scroll Viewをつくる set aScroll to current application’s NSScrollView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight)) set aView to current application’s NSTextView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight)) aView’s setRichText:true aView’s useAllLigatures:true aView’s setTextColor:(current application’s NSColor’s yellowColor()) –cyanColor aView’s setFont:(current application’s NSFont’s fontWithName:fontID |size|:fontSize) –ヒラギノ明朝Pro W3 aView’s setBackgroundColor:aColor aScroll’s setDocumentView:aView aView’s enclosingScrollView()’s setHasVerticalScroller:true –Buttonをつくる set bButton to (current application’s NSButton’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, 40))) bButton’s setTitle:aButtonMSG bButton’s setTarget:me bButton’s setAction:("clicked:") –SplitViewをつくる set aSplitV to current application’s NSSplitView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aHeight, aWidth)) aSplitV’s setVertical:false aSplitV’s addSubview:aScroll aSplitV’s addSubview:bButton aSplitV’s setNeedsDisplay:true –WindowとWindow Controllerをつくる set aWin to makeWinWithView(aSplitV, aWidth, aHeight, aTitle, 0.9) aWin’s makeKeyAndOrderFront:(missing value) set wController to current application’s NSWindowController’s alloc() wController’s initWithWindow:aWin aWin’s makeFirstResponder:aView aView’s setString:dispStr wController’s showWindow:me set aCount to timeOutSecs * 10 –timeout seconds * 10 repeat aCount times if (my windisp) = false then exit repeat end if delay 0.1 set aCount to aCount – 1 end repeat my closeWin:aWin end dispTextView –Button Clicked Event Handler on clicked:aSender set (my windisp) to false end clicked: –make Window for Input on makeWinWithView(aView, aWinWidth, aWinHeight, aTitle, alphaV) set aScreen to current application’s NSScreen’s mainScreen() set aFrame to {{0, 0}, {aWinWidth, aWinHeight}} set aBacking to current application’s NSTitledWindowMask –NSBorderlessWindowMask set aDefer to current application’s NSBackingStoreBuffered — Window set aWin to current application’s NSWindow’s alloc() (aWin’s initWithContentRect:aFrame styleMask:aBacking backing:aDefer defer:false screen:aScreen) –aWin’s setBackgroundColor:(current application’s NSColor’s whiteColor()) aWin’s setTitle:aTitle aWin’s setDelegate:me aWin’s setDisplaysWhenScreenProfileChanges:true aWin’s setHasShadow:true aWin’s setIgnoresMouseEvents:false aWin’s setLevel:(current application’s NSNormalWindowLevel) aWin’s setOpaque:false aWin’s setAlphaValue:alphaV –append aWin’s setReleasedWhenClosed:true aWin’s |center|() –aWin’s makeKeyAndOrderFront:(me) — Set Custom View aWin’s setContentView:aView return aWin end makeWinWithView –close win on closeWin:aWindow repeat with n from 10 to 1 by -1 (aWindow’s setAlphaValue:n / 10) delay 0.02 end repeat aWindow’s |close|() end closeWin: –指定PostScript名称のフォントがコンピューター上に存在するかどうかチェック on checkExistenceOfFont(fontName as string) if fontName = "" then return false set aFont to current application’s NSFont’s fontWithName:fontName |size|:9.0 if aFont = missing value then return false else return true end if end checkExistenceOfFont –指定Postscript名称のフォントに定義されている文字数を数えて返す on countDefinedCharactersInFont(fontName as string) script spdF property aList : {} end script set aFont to current application’s NSFont’s fontWithName:fontName |size|:9.0 if aFont = missing value then return false set aSet to aFont’s coveredCharacterSet() set aList of spdF to {} repeat with i from 1 to 65535 set aRes to (aSet’s characterIsMember:i) as boolean if aRes = true then set the end of aList of spdF to (string id i) end if end repeat return length of (aList of spdF) end countDefinedCharactersInFont –指定Postscript名称のフォントに定義されている文字を返す on retDefinedCharactersInFont(fontName as string) script spdG property aList : {} end script set aFont to current application’s NSFont’s fontWithName:fontName |size|:24.0 set aSet to aFont’s coveredCharacterSet() set aList of spdG to {} repeat with i from 1 to 65535 set aRes to (aSet’s characterIsMember:i) as boolean if aRes = true then set the end of aList of spdG to (string id i) end if end repeat return (aList of spdG) end retDefinedCharactersInFont on listToStringUsingTextItemDelimiter(sourceList, textItemDelimiter) set the CocoaArray to current application’s NSArray’s arrayWithArray:sourceList set the CocoaString to CocoaArray’s componentsJoinedByString:textItemDelimiter return (CocoaString as string) end listToStringUsingTextItemDelimiter –インストールされているフォントのpost script nameを取得する on getEveryFontPSName() set aFontList to NSFontManager’s sharedFontManager()’s availableFonts() set thePred to NSPredicate’s predicateWithFormat:"NOT SELF BEGINSWITH ’.’" set aFontList to (aFontList’s filteredArrayUsingPredicate:thePred) as list set aList to {} repeat with i in aFontList set aName to contents of i set the end of aList to aName end repeat return aList end getEveryFontPSName |
More from my site
(Visited 77 times, 1 visits today)