1

I am looking for a way to switch to the first tab in a Mail window. It seems like there is no universal way to address tabs in macOS programmatically. I have tried Apple scripting with and without using "System Events", but with no luck. And most scripts on the net are regarding Safari.

These do not work:

tell application "System Events" to tell process "Mail" to tell window 1 to tell radio button 1 to click
tell window 1 of application "Mail" to set current tab to tab 1

It seems like Mail handles the tabs as if they were windows. But this does not help since the indexes of the windows/tabs shuffle around depending on how they are set up, whichever tab was selected last etc., and I always want to address tab #1 independent of the window index number and name/contents.

Tab example and windows problem from Mail.app

I have traversed the Applescript Dictionary but cannot find anything suitable. Any hints would be appreciated. It would also be nice to know if it can be solved using Shell since I am going to use it for a Keyboard Maestro action.

3
  • 1
    The id properties of the window elements confer the order in which they were opened: the greater the id number, the more recently the window was created. New tabs are created to the right of all existing tabs, so provided you don't manually rearrange the order of the tabs, you can infer the first tab as being the window with the smallest id value.
    – CJK
    Sep 28, 2022 at 9:18
  • Interestingly, whilst the currently selected tab will always have its window assigned an index value of 1, the other tabs are assigned index values also based on the order they were created. So window index 2 represented the leftmost tab, unless the leftmost tab is currently selected, in which case it will correlate to window index 1. Again, same caveat as before: rearranging the order of the tabs will nullify this rule (although rearranging doesn't overwrite any window properties, so window index 2, when not selected, will be wherever you moved the leftmost tab to).
    – CJK
    Sep 28, 2022 at 9:23
  • Thank you @CJK. I figured the same. It is a bit of a hack using the lowest id value because, as you say, it all depends on the arrangement of the tabs. But if it is impossible to be independent of that I guess that is what I should use. Hopefully, Apple will implement a cohesive way of doing this across apps since this is a relatively common widespread UI element.
    – max1mvs
    Sep 29, 2022 at 13:27

1 Answer 1

0

This is not optimal, but as a "hack", and if you do not rearrange the tabs after the initial setup, you can use the smallest id value to determine the first tab of Mail's window and do the switch. A thank you to CJK for the simplification of this script:

tell application "Mail"
    if (id of window index 2) < (id of window index 1) then set index of window index 2 to 1
end tell
2
  • 1
    Probably easier to switch to window index 2, since that is also the one with the lowest id value, unless you're already in the first tab. In this instance, I doubt you'd be triggering the script, but to cover all bases takes but a single comparison to check if (id of window index 2) < (id of window index 1) then set index of window index 2 to 1 . This spares having to perform an algorithm to find the lowest id window, and will consequently be quicker if you have lots of tabs open. (We can infer that the value of M in your script will always be 2, except when it's 1.)
    – CJK
    Oct 1, 2022 at 22:41
  • Thank you @CJK. You are right, this is much simpler. I have modified the script accordingly.
    – max1mvs
    Oct 3, 2022 at 10:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .