How to switch consistently to main (first) window of an app with applescript?
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.