— Created 2017-12-26 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "AppKit"
use framework "Carbon" — AEInteractWithUser() is in Carbon
use framework "dbColNamesKit" –https://github.com/daniel-beard/DBColorNames/
property NSView : a reference to current application’s NSView
property NSColor : a reference to current application’s NSColor
property NSArray : a reference to current application’s NSArray
property NSMenu : a reference to current application’s NSMenu
property NSImage : a reference to current application’s NSImage
property NSScreen : a reference to current application’s NSScreen
property NSButton : a reference to current application’s NSButton
property NSWindow : a reference to current application’s NSWindow
property NSTextField : a reference to current application’s NSTextField
property NSMenuItem : a reference to current application’s NSMenuItem
property NSBezierPath : a reference to current application’s NSBezierPath
property NSPopUpButton : a reference to current application’s NSPopUpButton
property NSSortDescriptor : a reference to current application’s NSSortDescriptor
property NSWindowController : a reference to current application’s NSWindowController
property NSTitledWindowMask : a reference to current application’s NSTitledWindowMask
property NSRoundedBezelStyle : a reference to current application’s NSRoundedBezelStyle
property NSNormalWindowLevel : a reference to current application’s NSNormalWindowLevel
property NSBackingStoreBuffered : a reference to current application’s NSBackingStoreBuffered
property NSMomentaryLightButton : a reference to current application’s NSMomentaryLightButton
property windisp : false
if current application’s AEInteractWithUser(-1, missing value, missing value) is not equal to 0 then return
tell application "TextEdit"
set dCount to count every document
if dCount = 0 then return
tell text of front document
set aList to color of every attribute run
end tell
end tell
–1D/2D Listのユニーク化
set ap1List to uniquify1DList(aList, true) of me
–色選択ダイアログを表示してポップアップメニューから色選択
set aButtonMSG to "OK"
set aWindowTitle to "Choose Color"
set aVal to getPopupValues(ap1List, 65535, aButtonMSG, aWindowTitle, 180) of me
if (aVal = false) or (aVal = missing value) then
display dialog "No Selection" buttons {"OK"} default button 1 with icon 2
return
end if
set targColor to item aVal of ap1List
set aRes to pickupColoredText(targColor) of me
set the clipboard to aRes –抽出結果をクリップボードへ
return aRes
on getPopupValues(ap1List, aColMax, aButtonMSG, aWindowMSG, timeOutSecs)
set (my windisp) to true
set aView to NSView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, 360, 100))
–Labelをつくる
set a1TF to NSTextField’s alloc()’s initWithFrame:(current application’s NSMakeRect(30, 60, 80, 20))
a1TF’s setEditable:false
a1TF’s setStringValue:"Color:"
a1TF’s setDrawsBackground:false
a1TF’s setBordered:false
–Ppopup Buttonをつくる
set a1Button to NSPopUpButton’s alloc()’s initWithFrame:(current application’s NSMakeRect(80, 60, 200, 20)) pullsDown:false
a1Button’s removeAllItems()
set a1Menu to NSMenu’s alloc()’s init()
set iCount to 1
repeat with i in ap1List
copy i to {rCol, gCol, bCol}
set j to contents of i
set aCocoaList to retCocoaColorList(j, aColMax) of me
set nsCol to (NSColor’s colorFromRGBAArray:aCocoaList)
set anImage to makeNSImageWithFilledWithColor(32, 20, nsCol) of me
set aColName to retColorDetailName(rCol, gCol, bCol, 65535) of me
set aTitle to "#" & (iCount as string) & " " & aColName
set aMenuItem to (NSMenuItem’s alloc()’s initWithTitle:aTitle action:"actionHandler:" keyEquivalent:"")
(aMenuItem’s setImage:anImage)
(aMenuItem’s setEnabled:true)
(a1Menu’s addItem:aMenuItem)
set iCount to iCount + 1
end repeat
a1Button’s setMenu:a1Menu
–Buttonをつくる
set bButton to (NSButton’s alloc()’s initWithFrame:(current application’s NSMakeRect(80, 10, 140, 40)))
bButton’s setButtonType:(current application’s NSMomentaryLightButton)
bButton’s setBezelStyle:(current application’s NSRoundedBezelStyle)
bButton’s setTitle:aButtonMSG
bButton’s setTarget:me
bButton’s setAction:("clicked:")
bButton’s setKeyEquivalent:(return)
aView’s addSubview:a1TF
aView’s addSubview:a1Button
aView’s addSubview:bButton
aView’s setNeedsDisplay:true
–NSWindowControllerを作ってみた
set aWin to (my makeWinWithView(aView, 300, 100, aWindowMSG))
set wController to NSWindowController’s alloc()
wController’s initWithWindow:aWin
wController’s showWindow:me
set aCount to timeOutSecs * 100
set hitF to false
repeat aCount times
if (my windisp) = false then
set hitF to true
exit repeat
end if
delay 0.01
set aCount to aCount – 1
end repeat
my closeWin:aWin
if hitF = true then
set s1Val to (a1Button’s indexOfSelectedItem() as integer) + 1
else
set s1Val to false
end if
return s1Val
end getPopupValues
on clicked:aSender
set (my windisp) to false
end clicked:
–make Window for Display
on makeWinWithView(aView, aWinWidth, aWinHeight, aTitle)
set aScreen to NSScreen’s mainScreen()
set aFrame to {{0, 0}, {aWinWidth, aWinHeight}}
set aBacking to current application’s NSTitledWindowMask
set aDefer to current application’s NSBackingStoreBuffered
— Window
set aWin to 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 setReleasedWhenClosed:true
aWin’s |center|()
–aWin’s makeKeyAndOrderFront:(me)
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:
–Popup Action Handler
on actionHandler:sender
set aTag to tag of sender as integer
set aTitle to title of sender as string
end actionHandler:
on makeNSColorFromRGBAval(redValue as integer, greenValue as integer, blueValue as integer, alphaValue as integer, aMaxVal as integer)
set aRedCocoa to (redValue / aMaxVal) as real
set aGreenCocoa to (greenValue / aMaxVal) as real
set aBlueCocoa to (blueValue / aMaxVal) as real
set aAlphaCocoa to (alphaValue / aMaxVal) as real
set aColor to NSColor’s colorWithCalibratedRed:aRedCocoa green:aGreenCocoa blue:aBlueCocoa alpha:aAlphaCocoa
return aColor
end makeNSColorFromRGBAval
–1D Listをユニーク化
on uniquify1DList(theList as list, aBool as boolean)
set aArray to NSArray’s arrayWithArray:theList
set bArray to aArray’s valueForKeyPath:"@distinctUnionOfObjects.self"
set bList to bArray as list
return bList
end uniquify1DList
–指定サイズのNSImageを作成し、指定色で塗って返す
on makeNSImageWithFilledWithColor(aWidth, aHeight, fillColor)
set anImage to NSImage’s alloc()’s initWithSize:(current application’s NSMakeSize(aWidth, aHeight))
anImage’s lockFocus()
—
set theRect to {{x:0, y:0}, {height:aHeight, width:aWidth}}
set theNSBezierPath to NSBezierPath’s bezierPath
theNSBezierPath’s appendBezierPathWithRect:theRect
—
fillColor’s |set|() –色設定
theNSBezierPath’s fill() –ぬりつぶし
—
anImage’s unlockFocus()
—
return anImage
end makeNSImageWithFilledWithColor
on pickupColoredText(aColList)
set outStrList to ""
tell application "TextEdit"
tell text of front document
set colorList to color of every attribute run
set textList to characters of every attribute run
set aCount to length of colorList
repeat with i from 1 to aCount
set aColCon to item i of colorList
if aColCon is equal to aColList then –指定色の箇所
set outStrList to outStrList & ((contents of item i of textList) as string) & return
end if
end repeat
end tell
end tell
return outStrList
end pickupColoredText
on retColorDetailName(rCol as integer, gCol as integer, bCol as integer, aColorMax)
set aColor to makeNSColorFromRGBAval(rCol, gCol, bCol, aColorMax, aColorMax) of me
set aCDB to current application’s DBColorNames’s alloc()’s init()
set aColorStr to (aCDB’s nameForColor:aColor) as string
return aColorStr
end retColorDetailName
on retCocoaColorList(aColorList as list, aMax as integer)
set tmpList to {}
repeat with i in aColorList
set j to (contents of i)
if j = 0 then
set the end of tmpList to 0
else
set the end of tmpList to (j / aMax) as real
end if
end repeat
set the end of tmpList to 1.0
return tmpList
end retCocoaColorList
–数値の1D List with Recordをソート
on sort1DRecList(aList as list, aKey as string, ascendingF as boolean)
set aArray to NSArray’s arrayWithArray:aList
set desc1 to NSSortDescriptor’s sortDescriptorWithKey:aKey ascending:ascendingF selector:"compare:"
set bList to (aArray’s sortedArrayUsingDescriptors:{desc1}) as list
return bList
end sort1DRecList