Events 在鼠标位置单击AppleScript

Events 在鼠标位置单击AppleScript,events,click,applescript,system,mouse,Events,Click,Applescript,System,Mouse,此代码用于单击屏幕上的特定位置,但如何单击光标的坐标 tell application "System Events" click at {10, 10} end tell 你绝对应该抓住AppleScript工具箱。它有脚本命令来获取鼠标位置和鼠标点击 下面是applescript中python的一个示例,python自然地存在于您的系统中,不需要计划安装 set x to 30 set y to 5 set l to 50 -- click same location 50 t

此代码用于单击屏幕上的特定位置,但如何单击光标的坐标

tell application "System Events"
    click at {10, 10}
end tell

你绝对应该抓住AppleScript工具箱。它有脚本命令来获取鼠标位置和鼠标点击

下面是applescript中python的一个示例,python自然地存在于您的系统中,不需要计划安装

set x to 30

set y to 5

set l to 50 -- click same location 50 times



do shell script "

/usr/bin/python <<END

import sys

import time

from Quartz.CoreGraphics import *

def mouseEvent(type, posx, posy):

          theEvent = CGEventCreateMouseEvent(None, type, (posx,posy), kCGMouseButtonLeft)

          CGEventPost(kCGHIDEventTap, theEvent)

def mousemove(posx,posy):

          mouseEvent(kCGEventMouseMoved, posx,posy);

def mouseclick(posx,posy):

          mouseEvent(kCGEventLeftMouseDown, posx,posy);

          mouseEvent(kCGEventLeftMouseUp, posx,posy);

ourEvent = CGEventCreate(None);

currentpos=CGEventGetLocation(ourEvent);             # Save current mouse position

for x in range(0, " & l & "):

          mouseclick(" & x & "," & y & ");

mousemove(int(currentpos.x),int(currentpos.y));      # Restore mouse position

END"
将x设置为30
将y设置为5
将l设置为50--单击同一位置50次
执行shell脚本“

/usr/bin/python您也可以在applescrit中使用命令工具cliclick

dowload https://github.com/BlueM/cliclick

unzip cliclick-master.zip

in a Terminal

cd  /Users/yourname/Downloads/cliclick-master

make

now you have in /Users/yourname/Downloads/cliclick-master/

cliclick , command tool for using a mouse in applescript with do shell script

copy cliclick in /usr/local/bin folder

in a Terminal

cp -f /Users/yourname/Downloads/cliclick-master/cliclick  /usr/local/bin/

    in applescript for example

    do shell script "/usr/local/bin/cliclick  " & quoted form of "c:12,34"

    will click at the point with x coordinate

              12 and y coordinate 34.

    in a Terminal for LIST OF COMMANDS

    cliclick -h

 LIST OF COMMANDS

  c:x,y   Will CLICK at the point with the given coordinates.
          Example: “c:12,34” will click at the point with x coordinate
          12 and y coordinate 34. Instead of x and y values, you may
          also use “.”, which means: the current position. Using “.” is
          equivalent to using relative zero values “c:+0,+0”.

  dc:x,y  Will DOUBLE-CLICK at the point with the given coordinates.
          Example: “dc:12,34” will double-click at the point with x
          coordinate 12 and y coordinate 34. Instead of x and y values,
          you may also use “.”, which means: the current position.

  dd:x,y  Will press down to START A DRAG at the given coordinates.
          Example: “dd:12,34” will press down at the point with x
          coordinate 12 and y coordinate 34. Instead of x and y values,
          you may also use “.”, which means: the current position.

  du:x,y  Will release to END A DRAG at the given coordinates.
          Example: “du:112,134” will release at the point with x
          coordinate 112 and y coordinate 134.

  kd:keys Will trigger a KEY DOWN event for a comma-separated list of
          modifier keys. Possible keys are:
          “alt”, “cmd”, “ctrl”, “fn”, “shift”
          Example: “kd:cmd,alt” will press the command key and the
          option key (and will keep them down until you release them
          with another command)

  kp:key  Will emulate PRESSING A KEY (key down + key up). Possible keys are:
          “arrow-down”, “arrow-left”, “arrow-right”, “arrow-up”, “delete”, “end”,
          “esc”, “f1”, “f2”, “f3”, “f4”, “f5”, “f6”, “f7”, “f8”, “f9”, “f10”, “f11”,
          “f12”, “f13”, “f14”, “f15”, “f16”, “fwd-delete”, “help”, “home”, “mute”,
          “page-down”, “page-up”, “return”, “space”, “tab”, “volume-down”, “volume-up”
          Example: “kp:return” will hit the return key.

  ku:keys Will trigger a KEY UP event for a comma-separated list of
          modifier keys. Possible keys are:
          “alt”, “cmd”, “ctrl”, “fn”, “shift”
          Example: “ku:cmd,ctrl” will release the command key and the
          control key (which will only have an effect if you performed
          a “key down” before)

  m:x,y   Will MOVE the mouse to the point with the given coordinates.
          Example: “m:12,34” will move the mouse to the point with
          x coordinate 12 and y coordinate 34.

  p[:str] Will PRINT the given string. If the string is “.”, the
          current MOUSE POSITION is printed. As a convenience, you can skip
          the string completely and just write “p” to get the current position.
          Example: “p:.” or “p” will print the current mouse position
          Example: “p:'Hello world'” will print “Hello world”

  tc:x,y  Will TRIPLE-CLICK at the point with the given coordinates.
          Example: “tc:12,34” will triple-click at the point with x
          coordinate 12 and y coordinate 34. Instead of x and y values,
          you may also use “.”, which means: the current position.

  t:text  Will emulate typing the given text into the frontmost application.
          If the text includes space(s), it must be enclosed in quotes.
          Example: “type:Test” will type “Test” 
          Example: “type:'Viele Grüße'” will type “Viele Grüße”

  w:ms    Will WAIT/PAUSE for the given number of milliseconds.
          Example: “w:500” will pause command execution for half a second

我使用Swift编程语言在Xcode中创建了一个命令行工具,以生成一个可执行文件:

    // Location of Mouse pointer
    var ml = NSEvent.mouseLocation
    ml.y = NSHeight(NSScreen.screens[0].frame) - ml.y
    let location = CGPoint(x: ml.x, y: ml.y)

    // Single mouse click.
    var e = CGEvent(mouseEventSource: nil, mouseType: .leftMouseDown, mouseCursorPosition: location, mouseButton: .left)!
    e.post(tap: .cghidEventTap)
    e = CGEvent(mouseEventSource: nil, mouseType: .leftMouseUp, mouseCursorPosition: location, mouseButton: .left)!
    e.post(tap: .cghidEventTap)

    e = CGEvent(mouseEventSource: nil, mouseType: .leftMouseDown, mouseCursorPosition: location, mouseButton: .left)!
    e.setIntegerValueField(.mouseEventClickState, value: 1)
    e.post(tap: .cghidEventTap)

    e = CGEvent(mouseEventSource: nil, mouseType: .leftMouseUp, mouseCursorPosition: location, mouseButton: .left)!
    e.setIntegerValueField(.mouseEventClickState, value: 1)
    e.post(tap: .cghidEventTap)
do shell script "/Users/bobby/Documents/AppleScripts/Mouse/Executables/Click"
然后,我在脚本编辑器中创建了一个shell脚本来触发可执行文件:

    // Location of Mouse pointer
    var ml = NSEvent.mouseLocation
    ml.y = NSHeight(NSScreen.screens[0].frame) - ml.y
    let location = CGPoint(x: ml.x, y: ml.y)

    // Single mouse click.
    var e = CGEvent(mouseEventSource: nil, mouseType: .leftMouseDown, mouseCursorPosition: location, mouseButton: .left)!
    e.post(tap: .cghidEventTap)
    e = CGEvent(mouseEventSource: nil, mouseType: .leftMouseUp, mouseCursorPosition: location, mouseButton: .left)!
    e.post(tap: .cghidEventTap)

    e = CGEvent(mouseEventSource: nil, mouseType: .leftMouseDown, mouseCursorPosition: location, mouseButton: .left)!
    e.setIntegerValueField(.mouseEventClickState, value: 1)
    e.post(tap: .cghidEventTap)

    e = CGEvent(mouseEventSource: nil, mouseType: .leftMouseUp, mouseCursorPosition: location, mouseButton: .left)!
    e.setIntegerValueField(.mouseEventClickState, value: 1)
    e.post(tap: .cghidEventTap)
do shell script "/Users/bobby/Documents/AppleScripts/Mouse/Executables/Click"

根据保存可执行文件的位置更改位置。

我现在遇到了完全相同的问题。是否有办法“获取”鼠标坐标?目前我能找到的最好方法是:Objec.C version option:Hello下面是一个例子,在applescript中有python,python自然地存在于您的系统中,无需计划安装。导入Quartz.CoreGraphics的速度非常慢。在我的机器上,从Applescript调用它的每个脚本鼠标操作都需要10-15秒的延迟。您好,是的,导入到Applescript中使用的python需要花费大量时间来执行。这就是为什么在Applescript中使用Carsten Blüm的cliclick要容易得多的原因。这取决于您对脚本的需要。f https:/github.com/BlueM/clickah,谢谢,是的,我看到了,但我正在尝试最小化依赖性,我希望有一个本机解决方案。版本3.0.3,发布于2014年10月29日作者:Carsten Blüm,贡献者列表:网站:www.BlueM.net/jump/click/Version 4.0.1198年4月10日作者:Carsten Blüm,错误修复:在4.0版中,无法使用kd操作同时按下多个键。在内置帮助(cliclick-h)中添加了缺少的选项。不工作,我收到“错误”系统事件错误:脚本编辑器不允许辅助访问。”电话-25211“然后打开辅助通道,@AntonMalmygin!仅仅因为你不了解错误的原因就否决一个答案是不合适的。寻求帮助,我很乐意提供。Hmm.。这是一个延伸,但是基础框架可以用来模拟鼠标点击和拖动而不是点击吗?还是只读的?我找到了苹果的AppKit文档,但由于我不是一个ObjC程序员,我发现它太难应用到我的as中。在什么地方有更清晰的文档吗@安东·马尔梅金他回答了最高职位,而且回答得很好。你不应该仅仅因为你自己不理解原始问题中讨论的问题就否决别人提出的好答案。如果你的问题比最上面的问题更广泛,你应该单独发布。@MichaelKupietz它肯定可以使用Quartz Core图形框架用AS ObjC模拟鼠标点击。无论出于何种原因,这已经不可能了。基础框架中的<代码> NSEVENT /代码>类我认为没有必要的方法来桥接Apple脚本的使用。佩奇提供了一些关于实施的线索;答案是教育性的。我希望这最终是可能的。