The basic concept is simple enough, though there are quirks in the implementation. The following script should do what you ask for in (A), with the caveat that it can't distinguish how an app is opened. It will operate whenever an app becomes active — through cmd-tab, a click on the dock icon, a double-click on the app icon, etc. — and will fail silently if there are no open windows or some other error condition occurs.
Copy the following script into the Script Editor, and save it as an stay-open application (select 'Application' from the File Format pulldown menu, and click the 'stay open after run handler' checkbox). You'll need to give it Accessibility permissions in security preferences, and you'll have to toggle those permissions off and on again every time you edit the script application.
use AppleScript version "2.4"
use framework "AppKit"
use scripting additions
property NSWorkspace : class "NSWorkspace"
on run
set workSp to NSWorkspace's sharedWorkspace()
set notifCent to workSp's notificationCenter()
tell notifCent to addObserver:me selector:"activeAppHasChanged:" |name|:"NSWorkspaceDidActivateApplicationNotification" object:(missing value)
end run
on idle
-- we don't use the idle loop, so tell the system let the app sleep. this comes out of idle once an hour
return 3600
end idle
on activeAppHasChanged:notif
set targetApp to (notif's userInfo's valueForKey:"NSWorkspaceApplicationKey")
set targetAppName to (targetApp's localizedName) as text
tell application "System Events"
tell process targetAppName
try
set {xpos, ypos} to position of first window
set {w, h} to size of first window
my mouseMove(xpos + w / 2, ypos + h / 2)
end try
end tell
end tell
end activeAppHasChanged:
on mouseMove(newX, newY)
do shell script "
/usr/bin/python <<END
from Quartz.CoreGraphics import CGEventCreateMouseEvent
from Quartz.CoreGraphics import CGEventPost
from Quartz.CoreGraphics import kCGMouseButtonLeft
from Quartz.CoreGraphics import kCGHIDEventTap
from Quartz.CoreGraphics import kCGEventMouseMoved
import time
def mouseEvent(posx, posy):
theEvent = CGEventCreateMouseEvent(None, kCGEventMouseMoved, (posx,posy), kCGMouseButtonLeft)
CGEventPost(kCGHIDEventTap, theEvent)
mouseEvent(" & newX & "," & newY & ");
END"
end mouseMove
Launch the script and leave it running in the background. Every time you switch apps, it will center the cursor in the first open window of the application. If you prefer option (B) — the keyboard shortcut route — that's also doable, but it's a bit different approach. Ask if you need help implementing it.