Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

This post suggests that the command tell application “System Events” to key code 37 in AppleScript should move the mouse pointer one pixel down and to the left if run when MouseKeys are enabled. In OS X 10.7.2, however, it has the effect of generating a lowercase l, and not moving the mouse at all.

Similarly, I had hoped tell application “System Events” to key code 34 using control down would sent a control click at the current point of the mouse cursor, but that does not seem to be the effect.

Does anyone know a way that works in Lion on a mid-2011 MacBook Air to generate a click or control-click at the current mouse location using AppleScript? It is okay if it relies on MouseKeys and/or UI scripting.

I would prefer if it did not involve installing compiled command line utilities, but if such utilities exist and are Lion compatible, that is far better than nothing.

share|improve this question

2 Answers 2

up vote 5 down vote accepted
+100

There are tons of command line utilities to do this... and I'd reckon that going this route's your best bet... One benefit? is that these solutions should work, irrespective of whether or not "mouse keys" are on, right? One of my particular faves is cliclick

Cliclick” is short for "Command-Line Interface Click". > It is a a tiny shell / Terminal application that will emulate mouse clicks or series of mouse clicks (including doubleclicks and control-clicks) at arbitrary screen coordinates.

Hence, it is a tool for automating / scripting things that cannot otherwise be scripted (for instance, using AppleScript). Other than GUI Scripting, it does not require "Access for assistive devices" to be activated in the System Preferences.

Usage:

$ cliclick c m

will "control-click" the current mouse location, but you can also specify the coordinates, shift-click, double-click, etc. It can also return the coordinates of the mouse with a -q argument, and return the mouse to the original location (when specifying coordinates) with the -r argument.

In your case.. I guess you'd just...

do shell script "cliclick c m"

from inside your AppleScript / Workflow / whatever...

There's a GREAT post on this subject on MacWorld's site (née Mac OS X Hints) - that I've gleaned lots of good info from, over the years. If you don't care for my answer you will surely find a solution you DO like, there... there's hacks to make everyone smile - from an Automator freak = to a grumpy C diehard.

Update:

There do seem to be some ways - to "do it" in AppleScript.. but they all seem to require knowing where your mouse is... which i assume you don't. So in an embarrassingly nerdy play for some rep... I wrote this..

do shell script "python -c 'import sys, time; from Quartz.CoreGraphics import *;

def mouseEvent(type, posx, posy):
  theEvent = CGEventCreateMouseEvent(None, type, (posx,posy), kCGMouseButtonLeft);
  CGEventPost(kCGHIDEventTap, theEvent);

def mouseclickdn(posx,posy):
  mouseEvent(kCGEventLeftMouseDown, posx,posy);

def mouseclickup(posx,posy):
  mouseEvent(kCGEventLeftMouseUp, posx,posy);
  ourEvent = CGEventCreate(None);
  currentpos = CGEventGetLocation(ourEvent);
  print currentpos[0];
  print currentpos[1];
  mouseclickdn(currentpos.x, currentpos.y);
  mouseclickup(currentpos.x, currentpos.y)'
"

What the hell is it?

  1. AppleScript calls 'do shell script'.
  2. Which then calls Python - with a command line argument (-c) = that's been escaped and cajoled = to cooperate with A/S.
  3. Python then "imports" CoreGraphics/Quartz, etc.
  4. And defines some Cocoa-API/PyObjC methods for "dealing with" the mouse.
  5. Python "figures out" where the mouse is.
  6. And clicks! (down, then up)
  7. As a bonus, it returns the coordinates of the mouse, 'if you were interested_.

Works 100% and isn't even THAT slow. Hope you like it!

Edit:

Here is the BASH one-liner, for purists / A/S haters.

echo -e "import sys, time; from Quartz.CoreGraphics import *\ndef mouseEvent(type, posx, posy): theEvent = CGEventCreateMouseEvent(None, type, (posx,posy), kCGMouseButtonLeft); CGEventPost(kCGHIDEventTap, theEvent);\ndef mouseclickdn(posx,posy): mouseEvent(kCGEventLeftMouseDown, posx,posy);\ndef mouseclickup(posx,posy): mouseEvent(kCGEventLeftMouseUp, posx,posy);\nourEvent = CGEventCreate(None); currentpos = CGEventGetLocation(ourEvent); print currentpos[0]; print currentpos[1]; mouseclickdn(currentpos.x, currentpos.y);\nmouseclickup(currentpos.x, currentpos.y);" | python

share|improve this answer
    
Python! Wow, I hadn't thought of that. I'm eager to try this out. If I s/Left/Right/g, will it right click? –  Daniel Lawson Feb 19 '12 at 5:24
    
Yes, you just need to swap left with right in the TWO kCGEvents. You can also move it with something akin to def mousemove(posx,posy): mouseEvent(kCGEventMouseMoved, posx,posy); –  alex gray Feb 19 '12 at 5:32
    
This is the best spending of 100 rep I've ever done. Wonderful, wonderful answer. +125 (10 for the upvote, 15 for the accept, and 100 for the bounty). –  Daniel Lawson Feb 19 '12 at 5:46
    
@alexgray I made some formatting / whitespacing fixes for this post, mostly for sanity. To the best of my knowledge it is still syntactically correct for python. If this is not the case, please hit rollback on the edit history page. –  Jason Salaz Feb 19 '12 at 7:17
    
@Jason Salaz You're probably right.. The reason it was so crazy is that prior to "translating that to Applescript... I had tested it as a BASH one-liner.. I will edit the post to also show that implementation for people that prefer to script via traditional UNIX tools vs. AppleScript. –  alex gray Feb 19 '12 at 17:57

Here is the script that I use to generate mouse clicks

tell application "System Events" to key code 88
tell application "System Events" to key code 87

By way of explanation I found I had to create a small mouse movement(88) before the mouse clicking(87) would work consistently. This relies on MouseKeys.

share|improve this answer
    
What sort of keyboard do you have attached to your machine? What OS version? This doesn't work for me on a MacBook Air running Lion. –  Daniel Lawson Feb 15 '12 at 0:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.