Skip to content

Instantly share code, notes, and snippets.

@johnlindquist
Created August 24, 2023 20:44
Show Gist options
  • Save johnlindquist/bfd72e6eb864decc25466080f9ce24d2 to your computer and use it in GitHub Desktop.
Save johnlindquist/bfd72e6eb864decc25466080f9ce24d2 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 { UiohookKeyboardEvent, UiohookKey, UiohookMouseEvent, uIOhook } = pkg
let bundleIdentifier = ""
let windowTitle = ""
let recentlyClicked = false
uIOhook.on("click", event => {
recentlyClicked = true
setTimeout(() => {
recentlyClicked = false
}, 1000)
})
let exclusions = ["zoom"]
setInterval(async () => {
let frontmost = getFrontmostApp()
let idCheck = frontmost.bundleIdentifier !== bundleIdentifier
let titleCheck = frontmost.windowTitle !== windowTitle
// log({
// idCheck,
// titleCheck,
// })
if (exclusions.includes(frontmost.bundleIdentifier)) {
return
}
if (frontmost.windowTitle && (idCheck || titleCheck) && !recentlyClicked) {
bundleIdentifier = frontmost.bundleIdentifier // add title
windowTitle = frontmost.windowTitle
// get center from x and width
let x = frontmost.x + frontmost.width / 2
let y = frontmost.y + frontmost.height / 2
let rightX = frontmost.x + frontmost.width
let bottomY = frontmost.y + frontmost.height
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
}
log({
bundleIdentifier: frontmost.bundleIdentifier,
windowTitle: frontmost.windowTitle,
x,
})
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