Jump to content

Recommended Posts

Hi,

 

Does anyone know if there's an Alfred workflow to move the mouse to the active window. This would be very useful for my 3 monitor setup, as I can easily use the application switcher to get to desired window but it takes (what feels like ages) to get the mouse there.

 

My google searches are polluted by "focus-follows-mouse" content... which is the opposite of what I'm looking for! I also see a bunch of options for Windows users through AutoHotKey scripts, so I know that this is a commonly desired functionality.

 

So, has anyone made this for Alfred? If not, is it possible and what tools and resources are available for it?

 

Thanks in advance!

Link to comment

This script moves the mouse cursor to the centre of the frontmost window. Put it in a Run Script action with Language = /usr/bin/osascript (JS).

 

I have no idea if it works with multiple monitors.
 

ObjC.import('stdlib')
ObjC.import('CoreGraphics');

// Move mouse cursor to specified position
function moveMouse(x, y) {
  var pos = $.CGPointMake(x, y);
  var event = $.CGEventCreateMouseEvent(null, $.kCGEventMouseMoved, pos, $.kCGMouseButtonLeft);
  $.CGEventPost($.kCGHIDEventTap, event);
}


// Run script
function run() {
  var app = Application.currentApplication();
  app.includeStandardAdditions = true;

  var win = app.windows[0],
    bounds = win.properties().bounds;

  // Calculate centre of window
  var x = Math.trunc(bounds.x + (bounds.width / 2)),
    y = Math.trunc(bounds.y + (bounds.height / 2));

  console.log('centre = ' + x + 'x' + y);
  moveMouse(x, y);
}
Edited by deanishe
Link to comment
  • 5 years later...

Try this instead:

 

ObjC.import('stdlib')
ObjC.import('CoreGraphics');

// Move mouse cursor to specified position
function moveMouse(x, y) {
  let pos = $.CGPointMake(x, y);
  let event = $.CGEventCreateMouseEvent(null, $.kCGEventMouseMoved, pos, $.kCGMouseButtonLeft);
  $.CGEventPost($.kCGHIDEventTap, event);
}


// Run script
function run() {
  let appName = Application('System Events').processes.whose({frontmost: {'=': true}})[0].name();
  console.log(`frontmost application = ${appName}`);
  let app = Application(appName);
  app.includeStandardAdditions = true;

  let win = app.windows[0],
    bounds = win.properties().bounds;

  // Calculate centre of window
  let x = Math.trunc(bounds.x + (bounds.width / 2)),
    y = Math.trunc(bounds.y + (bounds.height / 2));

  console.log('centre = ' + x + 'x' + y);
  moveMouse(x, y);
}

 

I think it’s a massive bug in the original. I was using Application.currentApplication(), which will be the application running the script, not the frontmost application.


I suspect that it works for Vítor and me because we’re running the script in Script Editor, which has windows, and is both the frontmost app and the app running the script. It won't work in Alfred because it will look for Alfred's window (not the active application's), which is probably not visible.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...