Skip to content

Instantly share code, notes, and snippets.

@johnlindquist
Created August 30, 2023 20:22
Show Gist options
  • Save johnlindquist/c310b3c5cd03b04f190c10366b0e0ce9 to your computer and use it in GitHub Desktop.
Save johnlindquist/c310b3c5cd03b04f190c10366b0e0ce9 to your computer and use it in GitHub Desktop.
// Name: Move Mouse to Active App
// Background: true
import "@johnlindquist/kit"
import { getFrontmostApp } from "@johnlindquist/mac-frontmost"
import { shakeCursor } from "@johnlindquist/mac-cursor-effects"
import pkg from "uiohook-napi"
const { uIOhook } = pkg
let windowID = ""
let windowTitle = ""
let recentlyClicked = false
uIOhook.on("click", () => {
recentlyClicked = true
setTimeout(() => {
recentlyClicked = false
}, 1000)
})
let exclusions = ["zoom"]
setInterval(async () => {
let frontmost = getFrontmostApp()
let windowChanged = frontmost.windowID !== windowID
if (windowChanged && !recentlyClicked) {
for (let exclusion of exclusions) {
if (frontmost.localizedName.toLowerCase().includes(exclusion)) {
return
}
}
log(`Previous: ${windowID} -> ${frontmost.windowID}`)
windowID = frontmost.windowID // add title
if (windowTitle == frontmost.windowTitle || !frontmost.windowTitle) {
return
}
windowTitle = frontmost.windowTitle
log(`${windowID}: ${frontmost.localizedName} -> ${windowTitle}`)
// get center from x and width
let padding = 40
let x = frontmost.x + frontmost.width / 2 - padding
let y = frontmost.y + frontmost.height / 2 - padding
let rightX = frontmost.x + frontmost.width + padding
let bottomY = frontmost.y + frontmost.height + padding
let mousePosition = await getMousePosition()
// If the mouse is already inside the bounds of the app, don't move it
if (
mousePosition.x > frontmost.x &&
mousePosition.x < rightX &&
mousePosition.y > frontmost.y &&
mousePosition.y < bottomY
) {
return
}
if (x && y) {
await mouse.setPosition({
x,
y,
})
await wait(150)
shakeCursor(3, 0.1)
}
}
}, 250)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment