—
– Created by: Takaaki Naganoya
– Created on: 2019/09/16
—
– Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
use rtfLib : script "readStyledTextLib"
use parseLib : script "parseAttrLib"
property NSFont : a reference to current application’s NSFont
property NSView : a reference to current application’s NSView
property NSAlert : a reference to current application’s NSAlert
property NSColor : a reference to current application’s NSColor
property NSMenu : a reference to current application’s NSMenu
property NSArray : a reference to current application’s NSArray
property NSTabView : a reference to current application’s NSTabView
property NSPredicate : a reference to current application’s NSPredicate
property NSTextView : a reference to current application’s NSTextView
property NSMenuItem : a reference to current application’s NSMenuItem
property NSTabViewItem : a reference to current application’s NSTabViewItem
property NSPopUpButton : a reference to current application’s NSPopUpButton
property NSMutableDictionary : a reference to current application’s NSMutableDictionary
property NSRunningApplication : a reference to current application’s NSRunningApplication
property NSFontAttributeName : a reference to current application’s NSFontAttributeName
property NSKernAttributeName : a reference to current application’s NSKernAttributeName
property NSLigatureAttributeName : a reference to current application’s NSLigatureAttributeName
property NSMutableAttributedString : a reference to current application’s NSMutableAttributedString
property NSStrokeWidthAttributeName : a reference to current application’s NSStrokeWidthAttributeName
property NSUnderlineStyleAttributeName : a reference to current application’s NSUnderlineStyleAttributeName
property NSForegroundColorAttributeName : a reference to current application’s NSForegroundColorAttributeName
property TabPosition : 4 —0=Top, 1=Left, 2=Bottom, 3=Right, 4=None
property returnCode : 0
property aTabV : missing value
property selectedNum : {}
set aFile to choose file of type {"public.rtf"}
set aStyledStr to readRTFfile(aFile) of rtfLib
set paramObj to {myMessage:"Select Style", mySubMessage:"Select style you want to filter", viewWidth:800, viewHeight:400, myStyledStr:aStyledStr}
my dispTabViewWithAlertdialog:paramObj –for debug
–my performSelectorOnMainThread:"dispTabViewWithAlertdialog:" withObject:paramObj waitUntilDone:true
return selectedNum
on dispTabViewWithAlertdialog:paramObj
–Receive Parameters
set aMainMes to (myMessage of paramObj) as string –Main Message
set aSubMes to (mySubMessage of paramObj) as string –Sub Message
set aWidth to (viewWidth of paramObj) as integer –TextView width
set aHeight to (viewHeight of paramObj) as integer –TextView height
set aStyledStr to (myStyledStr of paramObj)
set attrResTmp to getAttributeRunsFromAttrString(aStyledStr) of parseLib
set attrList to attributes of attrResTmp
set menuStyle to menuList of attrResTmp
set selectedNum to {}
set aView to NSView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight))
–Ppopup Buttonをつくる
set a1Button to NSPopUpButton’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, aHeight – 20, 400, 20)) pullsDown:false
a1Button’s removeAllItems()
–WYSIWHG popup menuをつくる
set a1Menu to NSMenu’s alloc()’s init()
repeat with i from 1 to (length of menuStyle)
set {tmpFont, tmpSize} to contents of item i of menuStyle
set aTitle to (tmpFont & " " & tmpSize as string) & " point"
set aMenuItem to (NSMenuItem’s alloc()’s initWithTitle:aTitle action:"actionHandler:" keyEquivalent:"")
set attributedTitle to makeRTFfromParameters(aTitle, tmpFont, tmpSize, NSColor’s blackColor()) of me
(aMenuItem’s setEnabled:true)
(aMenuItem’s setTarget:me)
(aMenuItem’s setTag:(i as string))
(aMenuItem’s setAttributedTitle:(attributedTitle))
(a1Menu’s addItem:aMenuItem)
end repeat
–Ppopup Buttonにmenuをつける
a1Button’s setMenu:a1Menu
–Make Tab View
set aTabV to NSTabView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight – 30))
aTabV’s setTabViewType:(TabPosition)
–TabViewに中身を入れる
repeat with i from 1 to (length of menuStyle)
set {tmpFont, tmpSize} to contents of item i of menuStyle
set tmpKey to (tmpFont as string) & "/" & (tmpSize as string)
set tmpArry to filterRecListByLabel1(attrList, "styleKey2 == ’" & tmpKey & "’") of me
set tmpStr to (tmpArry’s valueForKeyPath:"stringVal") as list
set aTVItem to (NSTabViewItem’s alloc()’s initWithIdentifier:(i as string))
(aTVItem’s setLabel:(i as string))
set aTextView to (NSTextView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth – 10, aHeight – 10)))
(aTextView’s setRichText:true)
set tmpAttr to makeRTFfromParameters(tmpStr as string, tmpFont, tmpSize, NSColor’s blackColor()) of me
(aTextView’s textStorage()’s appendAttributedString:tmpAttr)
(aTVItem’s setView:aTextView)
(aTabV’s addTabViewItem:aTVItem)
end repeat
aView’s setSubviews:{a1Button, aTabV}
aView’s setNeedsDisplay:true
— set up alert
set theAlert to NSAlert’s alloc()’s init()
tell theAlert
–for Messages
its setMessageText:(aMainMes)
its setInformativeText:(aSubMes)
–for Buttons
its addButtonWithTitle:"OK"
its addButtonWithTitle:"Cancel"
–Add Accessory View
its setAccessoryView:(aView)
–for Help Button
its setShowsHelp:(true)
its setDelegate:(me)
end tell
— show alert in modal loop
NSRunningApplication’s currentApplication()’s activateWithOptions:0
my performSelectorOnMainThread:"doModal:" withObject:(theAlert) waitUntilDone:true
if (my returnCode as number) = 1001 then error number -128
set selectedNum to contents of item ((a1Button’s indexOfSelectedItem()) + 1) of menuStyle
end dispTabViewWithAlertdialog:
on doModal:aParam
set (my returnCode) to aParam’s runModal()
end doModal:
on alertShowHelp:aNotification
display dialog "Help Me!" buttons {"OK"} default button 1 with icon 1
return false –trueを返すと親ウィンドウ(アラートダイアログ)がクローズする
end alertShowHelp:
–Popup Action Handler
on actionHandler:sender
set aTag to tag of sender as integer
aTabV’s selectTabViewItemAtIndex:(aTag – 1)
end actionHandler:
–書式つきテキストを組み立てる
on makeRTFfromParameters(aStr as string, aFontName as string, aFontSize as real, aColor)
–フォント
set aVal1 to NSFont’s fontWithName:(aFontName) |size|:aFontSize
set aKey1 to (NSFontAttributeName)
–色
set aVal2 to aColor
set aKey2 to (NSForegroundColorAttributeName)
–カーニング
set aVal3 to 0.0
set akey3 to (NSKernAttributeName)
–アンダーライン
set aVal4 to 0
set akey4 to (NSUnderlineStyleAttributeName)
–リガチャ
–set aVal5 to 2 –全てのリガチャを有効にする
–set akey5 to ( NSLigatureAttributeName)
–枠線(アウトライン)
–set aVal6 to outlineNum
–set akey6 to ( NSStrokeWidthAttributeName)
set keyList to {aKey1, aKey2, akey3, akey4}
set valList to {aVal1, aVal2, aVal3, aVal4}
set attrsDictionary to NSMutableDictionary’s dictionaryWithObjects:valList forKeys:keyList
set attrStr to NSMutableAttributedString’s alloc()’s initWithString:aStr attributes:attrsDictionary
return attrStr
end makeRTFfromParameters
–リストに入れたレコードを、指定の属性ラベルの値で抽出
on filterRecListByLabel1(aRecList as list, aPredicate as string)
set aArray to NSArray’s arrayWithArray:aRecList
set aPredicate to NSPredicate’s predicateWithFormat:aPredicate
set filteredArray to aArray’s filteredArrayUsingPredicate:aPredicate
set bList to filteredArray
return bList
end filterRecListByLabel1