Created
September 1, 2023 02:14
-
-
Save johnlindquist/e350346c302e18267357cbcd99685570 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Name: Move Mouse to Active App | |
// Background: true | |
import "@johnlindquist/kit" | |
import { getFrontmostApp } from "@johnlindquist/mac-frontmost" | |
import { shakeCursor } from "@johnlindquist/mac-cursor-effects" | |
let windowID = "" | |
let windowTitle = "" | |
let recentlyClicked = false | |
let timeoutId = null | |
onClick(() => { | |
log("clicked") | |
recentlyClicked = true | |
if (timeoutId) { | |
clearTimeout(timeoutId) | |
} | |
timeoutId = 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