Skip to content

Instantly share code, notes, and snippets.

@deanishe
Created August 21, 2016 12:13
Show Gist options
  • Save deanishe/c232c89c5c9a1b9d67d54458ca363d01 to your computer and use it in GitHub Desktop.
Save deanishe/c232c89c5c9a1b9d67d54458ca363d01 to your computer and use it in GitHub Desktop.
JXA: Move mouse to centre of frontmost window
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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment