マウスの座標を取得しよう [AppleScript辞書はつくれるか?]
マウスポインターの位置座標を取得しようと思いスクリプトを作成。
ついでにその座標を原点にした128×128ピクセルのキャプチャ画像を取り込んでみることにした。
さらにそのキャプチャー画像を利用してマウスポインターの座標の色を取得した。
キャプチャ画像を作るのは
do shell script "screencapture -Rx,y,w,h Filespath"
で問題無かった。
そこから色を取得するのは
tell class "NSImage" to set aImage to alloc()'s initWithContentsOfFile:(inputImagePath)
set aRawimg to current application's NSBitmapImageRep's imageRepWithData:(aImage's TIFFRepresentation())
set origColor to (aRawimg's colorAtX:x y:y)
set srgbColSpace to current application's NSColorSpace's deviceRGBColorSpace
set aColor to (origColor's colorUsingColorSpace:srgbColSpace)
set aRed to (aColor's redComponent()) * 255
set aGreen to (aColor's greenComponent()) * 255
set aBlue to (aColor's blueComponent()) * 255
set getColor to {aRed as integer, aGreen as integer, aBlue as integer}
で問題なくできたが、
マウスポインターの位置を求めるのが難しかった。
マウスの座標データは
set {x:mouseX, y:mouseY} to current application's class "NSEvent"'s mouseLocation()
で求められるのですが、
画面左上を原点にしてではなく、左下が原点の座標が取得されてしまうのです。上下逆なのです。
キャプチャーやクリックなど他の命令は左上が基本なのに、これだけはなぜ?
しかたがないので、デスクトップの大きさを調べて割り出せばいけそうなので
tell application "Finder" to set {|左上x座標|, |左上y座標|, |右下x座標|, |右下y座標|} to bounds of window of desktop
set mouseY to |右下y座標| - mouseY
として、左上の座標は[x=0, y=0]の原点になるから、右下のy座標を求めてマウスのy座標を引いてやれば、下からの座標が上からに変換できるのでは?
......と思ってた頃もありました。
これは、使用しているモニタが1つしかないという場合だけに限定できるのであれば使えます。
しかし複数のモニタを使用している場合は、原点の位置とデスクトップの大きさがおぼろげになります。
上記の "bounds of window of desktop" では、第1(プライマリ)のデスクトップと第2(セカンダリ)のデスクトップを並べた全体の大きさが求められるため、|右下y座標|を使ってもズレが生じてしまいます。しかも画面の並べ方によっては原点が左上ではなく、どこに行くかわからないのす。
結論として、ずれる量を求めて差し引いてあげる方法でやってみるのがいいかなと、作ってみました。
--Applescript ここから
use scripting additions
use framework "Foundation"
global aRawimg, desktopHeight, hoseiX, hoseiY
--ここからメインルーチン
delay 2 --|開始まで2秒間待ちます、この間に測定したい位置にマウスポインタを移動してください|
(* (1) マウスポインターの位置を取得します *)
set {x, y} to my mousePosition()
log ("マウスポインターの位置:x=" & x & " y=" & y) --|取得したマウスポインタの先の座標をメッセージエリアに表示|
(* (2) ついでにマウスポインタの位置から128*128の大きさでキャプチャー画像を作ります *)
set {windowx, windowY, windowW, windowH} to {x, y, 128, 128}
set outputImage to do shell script "echo $HOME/Desktop/captureimage.png"
--> シェルスクリプトの$HOMEはユーザーフォルダの相対アドレス。ログインユーザーフォルダのデスクトップに、captureimage.pngというファイル名で保存先を指示
my screencapture(outputImage, windowx, windowY, windowW, windowH)
(* (3) キャプチャ画像をビットマップイメージに変換 *)
tell class "NSImage" to set aImage to alloc()'s initWithContentsOfFile:(outputImage)
(* (4) 画像の原点座標の色をRGBで取得 *)
set aRawimg to current application's NSBitmapImageRep's imageRepWithData:(aImage's TIFFRepresentation())
set {r, g, b} to my imagePositionColor(0, 0)
log ("Color " & "r=" & r & " g=" & g & " b=" & b) --|取得した取得した色をメッセージエリアに表示|
--ここからサブルーチン
(* 現在のマウスポインターの位置を取得(マルチモニタ対応版) *)
on mousePosition()
--use scripting additions
--use framework "Foundation"
--global desktopHeight, hoseiX, hoseiY
set mouseLocate to current application's class "NSEvent"'s mouseLocation() --|現在のマウス位置を記録|
try --|デスクトップの高さの変数desktopHeightに数値が設定されていなければ、再設定をする |
desktopHeight as integer
on error
set desktopHeight to (word 4 of (do shell script "/usr/sbin/system_profiler SPDisplaysDataType | grep Resolution | tail -n 1")) as integer
end try
try --|マウス位置の数値上のズレを記録した変数が設定されていなければ、再設定する|
{hoseiX as integer, hoseiY as integer}
on error
(* ポインタを測定原点位置に移動してから、位置を測定しズレを確認。補正変数に記録します *)
set pt to {x:100, y:100}
tell current application to CGPostMouseEvent(pt, 1, 1, 0)
set mouseLoc to current application's class "NSEvent"'s mouseLocation()
set x1 to (x of mouseLoc) as integer
set y1 to desktopHeight - ((y of mouseLoc) as integer)
set pt to {x:x1, y:y1}
tell current application to CGPostMouseEvent(pt, 1, 1, 0)
set mouseLoc to current application's class "NSEvent"'s mouseLocation()
set x2 to (x of mouseLoc) as integer
set y2 to desktopHeight - ((y of mouseLoc) as integer)
set {hoseiX, hoseiY} to {(x2 - x1), (y2 - y1)}
end try
(* マウスポインターの位置を補正を反映して元の位置に戻します *)
set mouseX to ((x of mouseLocate) as integer) - hoseiX
set mouseY to desktopHeight - ((y of mouseLocate) as integer) - hoseiY
set pt to {x:mouseX, y:mouseY}
tell current application to CGPostMouseEvent(pt, 1, 1, 0)
return {mouseX, mouseY}
end mousePosition
(* (今回は使用しませんが)指示された座標でクリックする動作 *)
on clickAction(clickX, clickY)
set pt to {x:clickX, y:clickY}
tell current application
CGPostMouseEvent(pt, 1, 1, 1) --|指定の位置でマウスの左ボタンを押す(擬似動作)|
CGPostMouseEvent(pt, 1, 1, 0) --|指定の位置でマウスの左ボタンを離す(擬似動作)|
end tell
return pt
end clickAction
(* マウスポインターを指定された位置に移動 *)
on movepointer(clickX, clickY)
set pt to {x:clickX, y:clickY}
tell current application to CGPostMouseEvent(pt, 1, 1, 0) --|指定された位置でマウスアップ(マウスの左ボタンを離す擬似動作)|
-- |マウスのボタンを押してもいない状態からのマウスアップ指示には全く意味がなく、位置が移動するだけの動作で利用|
return pt
end movepointer
(* 指定された原点(x,y)から、左右の指定された大きさ(w,h)でキャプチャを撮影 *)
on screencapture(capturefile, x, y, w, h)
set captureCommand to "screencapture -R" & (x as text) & "," & y & "," & w & "," & h & " '" & capturefile & "'"
do shell script captureCommand
return
end screencapture
(* 画像ファイルから指定のポイントの色をRGBで取得 *)
on imagePositionColor(x, y) --(aImage, x, y)
--use scripting additions
--use framework "Foundation"
--set aRawimg to current application's NSBitmapImageRep's imageRepWithData:(aImage's TIFFRepresentation()) --|外出しました|
set origColor to (aRawimg's colorAtX:x y:y)
set srgbColSpace to current application's NSColorSpace's deviceRGBColorSpace
set aColor to (origColor's colorUsingColorSpace:srgbColSpace)
set aRed to (aColor's redComponent()) * 255
set aGreen to (aColor's greenComponent()) * 255
set aBlue to (aColor's blueComponent()) * 255
--set aAlpha to (aColor's alphaComponent()) * 255 --|Alpha値は使わないので外し|
set getColor to {aRed as integer, aGreen as integer, aBlue as integer} -- & {aAlpha as integer}
return getColor
end imagePositionColor
--Applescript ここまで
コメント 0