center window of a specific app

is there a way to center a window of specific app?

I am using a combo of 2 NSWorkSpace notifcations: DidUnhideApplication and AppDidActivate. (i messed with Didactivate as well.)

Basiclly i want the window be centered on the current monitor (i have 2-3) where the mouse is.

With the solution I have below the window erradicately jumps back and forth to the mouse location then centers the window on the display where the mouse currently is. I tried having the window move before unhiding using NSworkspace. But i am curious if there is simple more elegant solution.

How i am currently doing it:

NSWorkspaceDidUnhideApplicationNotification for the app to run this applescript:

use framework "AppKit"

-- Get coordinates from bottom left corner
set |mouseLocation| to current application's NSEvent's mouseLocation()

-- Get current screen resolution
tell application "Finder" to set theDesctopBounds to bounds of window of desktop


-- Get coordinates from top left corner
set |mouseLocation| to {|mouseLocation|'s x, (item 4 of theDesctopBounds) - (|mouseLocation|'s y)}


--set the first windows position based on the current mouse position
tell application "System Events"
	set position of first window of application process "MacPass" to |mouseLocation|
end tell

then this trigger:

image

so possible found a bug. cause now BTT is crashing even though i removed the applescript trigger that crashed it the first time.
I noticed that triggers i have deleted or disabled continue to be triggered. So I deleted the old name triggers but it appears to be caching(?) them. I removed the one that crashed earlier and put back the working one. And now btt crashes every time even though I removed the old triggers as though that old trigger is still being used. i have attempted to restart BTT as well, but its still happening.