画面上の任意の場所の色を調べたい [AppleScript辞書はつくれるか?]
あるソフトウエア開発の上で、画面上の任意の場所の色を調べたい。
それもいくつも素早く調べたいという場面があり作ってみました。 (動作) ・マウスポインターの位置でリアルタイムで値が表示される。 ・その値は『#A1B2C3』のような16進数で表現。 ・場所と色を表示させる。 ・特定のキー『s』を押すとその時の結果がテキストとしてデスクトップに書き出される。 (あまり意味はないけれど、動作する・しないのチェックボタンを追加) というもの。 |
script AppDelegate property parent : class "NSObject" property theWindow : missing value
global imageView1, colorWell1 global textField1, textField2 global actionButton1, actionButton2 global updateTimer global desktopHeight global oldMouseX, oldMouseY
on applicationWillFinishLaunching_(aNotification) (* theWindowの設定 *) set theSize to current application's NSMakeSize(143,70) -- サイズ設定 tell theWindow setTitle_("色を調べる") setContentSize_(theSize) -- ウインドウサイズを変更 setMinSize_(theSize) -- 最小サイズを固定 setMaxSize_(theSize) -- 最大サイズを固定 end tell (* メインモニターのサイズを取得 *) set screenList to current application's class "NSScreen"'s screens() -- 全てのモニターサイズを取得 set {{x, y}, {w, h}} to (item 1 of screenList)'s frame() -- メインモニター(リストの1番目)の数値を取得 set desktopHeight to h as integer -- 天地サイズを取得して整数化 (* NSImageViewの作成 *) set theSize to current application's NSMakeSize(31, 31) -- サイズ形式設定 set aImage to current application's class "NSImage"'s alloc()'s ¬ initWithSize_(theSize) --空のイベージのインスタンスを作成 set theRect to current application's NSMakeRect(3, 36, 31, 31) --矩形設定 set imageView1 to current application's class "NSImageView"'s alloc()'s ¬ initWithFrame_(theRect) -- ImageViewの作成 tell imageView1 setImageFrameStyle_(3) -- NSImageGroove 溝付き(= 3) setImageAlignment_(0) -- NSImageAlignCenter 中心寄せ(=0) setImageScaling_(3) -- NSScaleProportionally 縦横比を維持して拡大(=3) setImage_(aImage) -- 表示するイメージの変更 end tell theWindow's contentView()'s addSubview_(imageView1) -- 作成したオブジェクトをtheWindowのコンテンツビューに配置 (* NSColorWellの作成 *) set theRect to current application's NSMakeRect(3, 3, 30, 30) set colorWell1 to current application's class "NSColorWell"'s alloc()'s ¬ initWithFrame_(theRect) -- ColorWellのインスタンス作成 tell colorWell1 setBordered_(false) -- 枠線を表示(非表示) setColor_(current application's class "NSColor"'s whiteColor()) --色を変更(白) end tell theWindow's contentView()'s addSubview_(colorWell1) (* NSTextField (Positin Text)の作成 *) set theRect to current application's NSMakeRect(35, 42, 50, 20) set aTextField to current application's class "NSTextField"'s alloc()'s ¬ initWithFrame_(theRect) -- TextFieldのインスタンス作成 tell aTextField setSelectable_(false) -- 選択可能か(不可) setEditable_(false) -- 編集可能か(不可) setBordered_(false) -- 枠線を表示(非表示) setDrawsBackground_(false) -- バックグラウンドを表示(非表示) setStringValue_("Position") -- 表示テキスト setFont_(current application's class "NSFont"'s systemFontOfSize_(11.0)) -- フォント設定 end tell theWindow's contentView()'s addSubview_(aTextField) set theRect to current application's NSMakeRect(80, 46, 60, 18) set textField1 to current application's class "NSTextField"'s alloc()'s ¬ initWithFrame_(theRect) tell textField1 setSelectable_(true) -- 選択可能(可) setEditable_(false) -- 編集可能か(不可) setBordered_(true) -- 枠線を表示(表示) setDrawsBackground_(true) -- バックグラウンドを表示(表示) setStringValue_("-, -") setFont_(current application's class "NSFont"'s systemFontOfSize_(11.0)) setAlignment_(2) -- [macOS, Mac Catalyst]NSTextAlignmentCenter テキストの中央寄せ(=2) end tell theWindow's contentView()'s addSubview_(textField1) (* NSTextField (Color Text)の作成 *) set theRect to current application's NSMakeRect(35, 21, 50, 20) set aTextField to current application's class "NSTextField"'s alloc()'s ¬ initWithFrame_(theRect) tell aTextField setSelectable_(false) setBordered_(false) setDrawsBackground_(false) setStringValue_("Color") setFont_(current application's class "NSFont"'s systemFontOfSize_(11.0)) end tell theWindow's contentView()'s addSubview_(aTextField) set theRect to current application's NSMakeRect(80, 25, 60, 18) set textField2 to current application's class "NSTextField"'s alloc()'s ¬ initWithFrame_(theRect) tell textField2 setEditable_(true) setBordered_(true) setDrawsBackground_(true) setStringValue_("#FFFFFF") setFont_(current application's class "NSFont"'s systemFontOfSize_(11.0)) end tell theWindow's contentView()'s addSubview_(textField2) (* NSButtonの作成 *) set screenShotKey to "s" set theRect to current application's NSMakeRect(30,0,56,24) set actionButton1 to current application's class "NSButton"'s alloc()'s ¬ initWithFrame_(theRect) -- Buttonのインスタンス作成 tell actionButton1 setBezelStyle_(1) -- ベゼルスタイル(基本的な立体) setButtonType_(0) -- ボタンタイプ(基本的な長丸) setTitle_(("Rec:" & screenShotKey) as string) -- ボタンの表示テキスト setFont_(current application's class "NSFont"'s systemFontOfSize_(10.0)) setTarget_(me) -- 対象となるスクリプト setAction_("action1:") -- 対象ハンドラ名(":"と"_"の扱いに注意。ハンドラでは区別されないが、ここでは":"で設定するのみ) setKeyEquivalent_(screenShotKey) -- ボタンクリックの代わりになるキーボードの設定 end tell theWindow's contentView()'s addSubview_(actionButton1) (* NSButton(チェックボタン)の作成 *) set theRect to current application's NSMakeRect(90,0,52,24) set actionButton2 to current application's class "NSButton"'s alloc()'s ¬ initWithFrame_(theRect) tell actionButton2 setButtonType_(3) -- ボタンタイプ(チェックボックス) setState_(1) -- ボタンの状態(ON:1, Off:0, Mix:-1) setTitle_("Run") -- ボタンの表示テキスト setTarget_(me) -- 対象となるスクリプト setAction_("action2:") end tell theWindow's contentView()'s addSubview_(actionButton2) (* スクリーンショットの画像キャッシュが保存できるのか確認 *) try do shell script ("sudo ; screencapture -x -R 0,0,10,10 ~/Documents/ssImage.png") -- 画像保存をシステムが許可しているかの確認 do shell script ("rm ~/Documents/ssImage.png") on error display dialog ("保存作業が、セキュリティーに阻まれました。" & return & ¬ "キャッシュやログで使用しますので、アクセシビリティを有効にしてください") buttons {"Quit"} quit me return end try -- set {oldMouseX, oldMouseY} to {-1, -1} -- マウス座標比較用 my timerSwitch() -- タイマー始動ハンドラへ end applicationWillFinishLaunching_
on action1_(sender) (* 日時の取得 *) set {year:yyyy, month:MM, day:dd, hours:ho, minutes:m, seconds:s} to current date set currentDate to (yyyy as text) & "年" & (MM as integer) & "月" & ¬ (text -2 thru -1 of ("0" & dd)) & "日 " set currentDate to currentDate & ho & ":" & (text -2 thru -1 of ("0" & m)) & ":" ¬ & (text -2 thru -1 of ("0" & s)) (* 保存データ作成 *) set theData to text 1 thru 12 of (((textField1's stringValue()) as text) & " ") set ans to currentDate & tab & theData & tab & ((textField2's stringValue()) as text) do shell script ("sudo ; echo '" & ans & "' >> $HOME/Desktop/getcolor.txt ;") end action1_
on action2_(sender) my timerSwitch() end action2_
on timerSwitch() set flg to actionButton2's state() --チェックボックスの状態取得(ONなら1、Offなら0) if flg = 1 then log "タイマー起動" set updateTimer to current application's class "NSTimer"'s ¬ scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_¬ (0.2, me, "timerMain", missing value, true) else log "タイマー停止" try tell updateTimer to invalidate() end try end if end timerSwitch
on timerMain() set mouseLoc to current application's class "NSEvent"'s mouseLocation() -- マウスポインタ座標取得 set mouseX to (x of mouseLoc) as integer set mouseY to desktopHeight - ((y of mouseLoc) as integer) -- デスクトップスクリーンとマウスポインターの座標が上下逆なので補正 set colorText to "#" if {oldMouseX, oldMouseY} /= {mouseX, mouseY} then set {oldMouseX, oldMouseY} to {mouseX, mouseY} set aText to (mouseX as text) & "," & mouseY if (count of aText) < 8 then textField1's setFont_(current application's class "NSFont"'s ¬ systemFontOfSize_(11.0)) else textField1's setFont_(current application's class "NSFont"'s ¬ systemFontOfSize_(9.0)) end if textField1's setStringValue_(aText) (* スクリーンショットを撮ります(ファイルで保存) *) set sScript to ("screencapture -x -R " & (mouseX - 13) & "," & (mouseY - 13) ¬ & ",27,27 ~/Documents/ssImage.png") do shell script sScript (* 撮影した画像をイメージとして読み込みます *) set add to do shell script "echo $HOME/Documents/ssImage.png" set aImage to current application's class "NSImage"'s alloc()'s ¬ initWithContentsOfFile_(add) imageView1's setImage_(aImage) -- try set aRawimg to current application's class "NSBitmapImageRep"'s ¬ imageRepWithData_(aImage's TIFFRepresentation()) set origColor to aRawimg's colorAtX_y_(13, 13) set aColor to origColor's colorUsingColorSpace_(current application's ¬ class "NSColorSpace"'s deviceRGBColorSpace) set aRed to aColor's redComponent() set aGreen to aColor's greenComponent() set aBlue to aColor's blueComponent() set colorText to "#" & (my hexnamber(aRed * 255)) & ¬ (my hexnamber(aGreen * 255)) & (my hexnamber(aBlue * 255)) end try if colorText /= "#" then textField2's setStringValue_(colorText) set theColor to current application's class "NSColor"'s ¬ colorWithCalibratedRed_green_blue_alpha_(aRed,aGreen,aBlue,1.0) colorWell1's setColor_(theColor) end if end if return end timerMain
on hexnamber(n) --|10進数>16進数| set hexCharacter to "0123456789ABCDEF" set hexAns to "" repeat set hexAns to (character ((n mod 16) + 1) of hexCharacter) & hexAns set n to n div 16 if n = 0 then exit repeat end repeat set hexAns to text -2 thru -1 of ("0" & hexAns) --強制二桁 return hexAns end hexnamber
on applicationShouldTerminateAfterLastWindowClosed_(sender)
return true end applicationShouldTerminateAfterLastWindowClosed_
on applicationShouldTerminate_(sender) return current application's NSTerminateNow end applicationShouldTerminate_ end script |
コメント 0