Created
August 21, 2016 12:13
-
-
Save deanishe/c232c89c5c9a1b9d67d54458ca363d01 to your computer and use it in GitHub Desktop.
JXA: Move mouse to centre of frontmost window
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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