0

I need to consistently focus on the first window of an app, since it has two open and their names change thus wondering if this is possible, without navigating through the apps menu?

Currently the below doesn't work if the second window was previously active (over the main window), as they need to be triggered always in the first window.

tell application "System Events" to tell application process "Microsoft Teams"
    set frontmost to true
    key code 44 using {command down}
    keystroke "dnd"
    key code 76
end tell

EDIT: AXRaise of first (or last) window doesn't seem to work neither as it is just the first in the list which changes according to which window was the last active from what I understand. Hopefully there is another way?

perform action "AXRaise" of first window of process "Microsoft Teams"

FYI - I was also looking for a solution for the opposite targeting the last (second) window but couldn't find a solution without using the menu (or dock menu).

EDIT: as user3439894 suggested I may be able to use different properties attribute if window id is not provided. The only differentiator I see is the title in the below list where the first window always seems to end with "| Microsoft Teams [QSP]"

{
{minimum value:missing value, orientation:missing value, position:{4088, 45}, class:window, accessibility description:missing value, role description:"standard window", focused:false, title:"testing | Microsoft Teams", size:{1350, 844}, help:missing value, entire contents:{}, enabled:missing value, maximum value:missing value, role:"AXWindow", value:missing value, subrole:"AXStandardWindow", selected:missing value, name:"testing | Microsoft Teams", description:"standard window"},

{minimum value:missing value, orientation:missing value, position:{4068, 25}, class:window, accessibility description:missing value, role description:"standard window", focused:false, title:"Calendar | Microsoft Teams [QSP]", size:{1095, 645}, help:missing value, entire contents:{}, enabled:missing value, maximum value:missing value, role:"AXWindow", value:missing value, subrole:"AXStandardWindow", selected:missing value, name:"Calendar | Microsoft Teams [QSP]", description:"standard window"} }

Markus
  • 152
  • 10
  • 1
    I do not have Microsoft Teams so I have no idea what you mean by " main (first) window" or "second window" as to their differences. Is this "main (first) window" a different type/style of window then the "second window" and does the "main (first) window" have a static name? I really think you need to provide more details than you have as this question is no clearer than your original question. What does the AppleScript code you show do? Additionally, as you are only dealing with two windows in the same application, why can't you just use ⌘` to switch between the two? – user3439894 Oct 14 '21 at 20:38
  • Thanks @user3439894 basically the app has two windows open and I always need to focus/raise the second window. (Re Teams the first window is where one mainly works in while the second is only when engaged in an online meeting/call). I tried AXRaise see updated OP but that cycles through the last active window so no help. I can't target the window by names as they change. – Markus Oct 14 '21 at 20:43
  • the same could probably practised with two Terminal windows. I know there is a another similar post but not with a solution for always focusing on the first window consistently https://apple.stackexchange.com/questions/39204/script-to-raise-a-single-window-to-the-front – Markus Oct 14 '21 at 20:58
  • @user3439894 your answer is deleted... you mentioned I can use otherwise some different property attribute if ID doesn't work/is not available? Do you know how I could retrieve them? I tried 'tell application "Microsoft Teams" to get the properties of every window' but that resulted in "Can’t get every property of every window.". – Markus Oct 15 '21 at 11:13
  • @user3439894 sorry managed to get them now via tell application "System Events" to tell application process "Microsoft Teams" to get the properties of every window and updated the OP's, anything we can use as per your suggestion? – Markus Oct 15 '21 at 11:17
  • ok i think I got it now after formatting the long export, it's only the title that's different but i tested some more and it seems the first window title is always ending with "| Microsoft Teams [QSP]". If I can target based on partial text of the title than we probably can do it similar to how you proposed it? – Markus Oct 15 '21 at 11:32
  • @user3439894 i tested after restart and it's back working again. if you re-publish your answer I'll accept it and will do some more testing,thanks. – Markus Oct 15 '21 at 13:01

2 Answers2

1

As I do not have Microsoft Teams, so I'm offering this as a test to see if it works.

Example AppleScript code:

tell application "System Events"
    tell application process "Microsoft Teams"
    if name of window 1 does not end with "| Microsoft Teams [QSP]" then
        set windowName to name of window 2
    else
        set windowName to name of window 1
    end if

    perform action "AXRaise" of window windowName
    set frontmost to true
    key code 44 using {command down}
    keystroke "dnd"
    key code 76

end tell

end tell

Notes:

The example AppleScript code assumes that the two windows will have unique names.



Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

user3439894
  • 58,676
  • Thanks the updated answer worked! Wondering if I can do this without set frontmost to true so key code can work in the window without raising it to the front? – Markus Oct 15 '21 at 11:58
  • 1
    @Markus, RE: "Wondering if I can do this without set frontmost to true so key code can work in the window without raising it to the front?" -- System Events can only send keystroke and key code to the _frontmost_window. – user3439894 Oct 15 '21 at 12:19
  • ok now in my testing it stopped working and I don't see anything different. The main window still ends with | Microsoft Teams [QSP] yet set frontmost to true is not applied to it but to the second window, very strange. I put a log message and it seems to trigger the first if condition set windowName to name of window 2 – Markus Oct 15 '21 at 12:49
0

I managed to create a solution via app menu searching for name string but if there is one using the properties name I'd be interested in that too which "user3439894 suggested. Also wondering if that may be more efficient. The below works.

tell application "System Events" to tell application process "Microsoft Teams"
    click (menu items of menu 1 of menu bar item "Window" of menu bar 1 whose name contains "QSP")
end tell
Markus
  • 152
  • 10