擬似クリックイベント(できるだけ高速版) [AppleScript辞書はつくれるか?]
from Quartz.CoreGraphics import *;
ここの*(ワイルドカード)で、Quartz.CoreGraphicsの中身を全部読み込んでいたことで時間がかかっていました。
知ってました。知っていたんです。でも、必要な部分だけを読み込むやり方がわからなかったのです。
そして、やっとわかりました。
from Quartz.CoreGraphics import CGEventCreateMouseEvent;
from Quartz.CoreGraphics import CGEventPost;
from Quartz.CoreGraphics import CGPointMake;
この3行に書き直せば万事解決です。
以下、作り直しました。
(* [使用例] my movePointer(100, 120) -- |移動|
my singleClick(10, 10) -- |クリック|
my doubleClick(80, 80) -- |ダブルクリック(注意:なぜかうまく機能しません)|
--|複数まとめてクリック| set multiplePoint to {{15, 10}, {40, 38}} set interval to 1.0 --|間隔(秒)| my multipleClick(multiplePoint, interval) *)
my movePointer(10, 10)
(* ポインター(矢印カーソル)の移動 *) on movePointer(x, y) set codeText to "import sys; from Quartz.CoreGraphics import CGEventCreateMouseEvent; from Quartz.CoreGraphics import CGEventPost; from Quartz.CoreGraphics import CGPointMake; x = float(" & x & "); y = float(" & y & "); po = CGPointMake(x, y); event = CGEventCreateMouseEvent(None, 2, po, 0); CGEventPost(0, event);" do shell script "python -c " & quoted form of codeText return true end movePointer
(* シングルクリック *) on singleClick(x, y) set codeText to "import sys; from Quartz.CoreGraphics import CGEventCreateMouseEvent; from Quartz.CoreGraphics import CGEventPost; from Quartz.CoreGraphics import CGPointMake; x = float(" & x & "); y = float(" & y & "); po = CGPointMake(x, y); event = CGEventCreateMouseEvent(None, 1, po, 0); CGEventPost(0, event); event = CGEventCreateMouseEvent(None, 2, po, 0); CGEventPost(0, event);" do shell script "python -c " & quoted form of codeText return true end singleClick
(* ダブルクリック - *) on doubleClick(x, y) set codeText to "import sys; from Quartz.CoreGraphics import CGEventCreateMouseEvent; from Quartz.CoreGraphics import CGEventPost; from Quartz.CoreGraphics import CGPointMake; x = float(" & x & "); y = float(" & y & "); po = CGPointMake(x, y); import time; " set codeText to codeText & "x = float(" & x & "); y = float(" & y & "); po = CGPointMake(x, y); event = CGEventCreateMouseEvent(None, 1, po, 0); CGEventPost(0, event); event = CGEventCreateMouseEvent(None, 2, po, 0); CGEventPost(0, event); time.sleep(0.01); " set codeText to codeText & "event = CGEventCreateMouseEvent(None, 1, po, 0); CGEventPost(0, event); event = CGEventCreateMouseEvent(None, 2, po, 0); CGEventPost(0, event); " do shell script "python -c " & quoted form of codeText return true end doubleClick
(*複数まとめてクリック*) on multipleClick(inputList, interval) if (class of inputList) is not list then return false set codeText to "import sys; from Quartz.CoreGraphics import CGEventCreateMouseEvent; from Quartz.CoreGraphics import CGEventPost; from Quartz.CoreGraphics import CGPointMake; " if interval > 0 then set codeText to codeText & "import time; " repeat with p in inputList set codeText to codeText & "x = float(" & (item 1 of p) & "); y = float(" & (item 2 of p) & "); po = CGPointMake(x, y); event = CGEventCreateMouseEvent(None, 1, po, 0); CGEventPost(0, event); event = CGEventCreateMouseEvent(None, 2, po, 0); CGEventPost(0, event); " if interval > 0 then set codeText to codeText & "time.sleep(" & interval & "); " end repeat log codeText do shell script "python -c " & quoted form of codeText return true end multipleClick |
https://stackoverflow.com/questions/281133/how-to-control-the-mouse-in-mac-using-python
http://nullege.com/codes/search/Quartz.CoreGraphics
コメント 0