Setting up an operator to center the TOPBARMT_MENU on the active workspace tab bar region when it changes seems hard. The horizontal scroll position of the top menu bar is not available via bpy, at least not obviously to me or according to my searches so far. Is there an object or reference in bpy that would provide this info?
The following python code tries to mitigate, but does not solve, tab horizontal scroll position and tab position detection via bpy. With the bpy code here, when the active workspace changes via mouse click, its tab placement moves to the leftmost position.
However, Simulation.topbar_scroll_left() uses pyautogui to simulate mouse hover and mouse scroll wheel actions at the topbar to show the now-activated first tab – otherwise it may remain out of sight.
# Via bpy:
# Not via bpy, not certain to put the first tab into
# a visible position if there are many tabs:
Simulation.topbar_scroll_left(how_much=100) # uses pyautogui
# Activates for input "workspace tab left mouse click".
# Not active for default Key Maps:
# Ctrl+PageUp, Ctrl+PageDown in User Preferences, Key Map,
# Screen, Screen (Global), screen.workspace_cycle([direction
# for direction in 'prev', 'next'])
handle = object()
args=(), # Callback args
pyautogui is not available to Blender’s Python by default. Blender ran in this case with PYTHONPATH=/usr/lib/python3.9 from bash with --python-use-system-env.
# self was here but I took it out
def move_mouse(x=50, y=50, t=0.2): # must be float(x), float(y), float(t)
pyautogui.moveTo(x, y, t, pyautogui.easeOutQuint)
return pyautogui.scroll( how_much )
return pyautogui.scroll( -how_much )
# pyautogui.hotkey("Ctrl", "PageDown")
# does the action but does not detect it.