Skip to content

Instantly share code, notes, and snippets.

@arthurdapaz
Forked from deanishe/MoveMouse.js
Created July 16, 2021 01:29
Show Gist options
  • Save arthurdapaz/af03a8ade5dada83cf6bdab2d62f4949 to your computer and use it in GitHub Desktop.
Save arthurdapaz/af03a8ade5dada83cf6bdab2d62f4949 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