In the Microsoft Teams Windows Client I would like to be able to identify with AutoHotkey the current active meeting window.
Consider that you can have several Teams window opened (popped-out chats, main window, several meeting windows with some on hold,...)
(Background: I want to do this to be able to send meeting specific actions hotkeys.)
I have come to another approach based on FindText to exclude any user prompt.
It check in the Teams windows if a UI Element like Leave and Resume are available to 100% exclude the wrong window.
See detailed explanation in this post: https://tdalon.blogspot.com/2021/04/ahk-get-teams-meeting-window.html
with screencast.
And here the code in gist: https://gist.github.com/tdalon/d376a2ac395a41c5453904222cbcb529
Extract below:
Teams_GetMeetingWindow(useFindText:="" , restore := True){
; See implementation explanations here:
; https://tdalon.blogspot.com/2021/04/ahk-get-teams-meeting-window.html
; https://tdalon.blogspot.com/2020/10/get-teams-window-ahk.html
If (useFindText="")
useFindText := PowerTools_GetParam("TeamsMeetingWinUseFindText") ; Default 1
If (useFindText) {
If (restore)
WinGet, curWinId, ID, A
ResumeText:="|<>*138$51.zzzzzzzzw3zzzzzzzUDzzzzzzwtzzzzzzzbA64NU1kQ1423A04FUtUQNa8aAX0EXAlY1a9zUNaAbwt4Y0AlYHb4461aAkTzzzzzzzzU" ; FindText for Resume
LeaveText:="|<>*168$66.zzzzzzzzzzzzzzzzDzzzzzy01zzDzzzzzs00TzDzzzzzk00DzDkkFW3U7k7zDUG9YFUDk7zD6T9YlUTs7zD0E841kTs7zD7nAAzszwDz022AAHzzzzz0UECS3zzzzzzzzzzzU"
}
WinGet, Win, List, ahk_exe Teams.exe
TeamsMainWinId := Teams_GetMainWindow()
TeamsMeetingWinId := PowerTools_RegRead("TeamsMeetingWinId")
WinCount := 0
Select := 0
Loop %Win% {
WinId := Win%A_Index%
If (WinId = TeamsMainWinId) { ; Exclude Main Teams Window
;WinGetTitle, Title, % "ahk_id " WinId
;MsgBox %Title%
Continue
}
WinGetTitle, Title, % "ahk_id " WinId
IfEqual, Title,, Continue
Title := StrReplace(Title," | Microsoft Teams","")
If RegExMatch(Title,"^[^\s]*\s?[^\s]*,[^\s]*\s?[^\s]*$") or RegExMatch(Title,"^[^\s]*\s?[^\s]*,[^\s]*\s?[^\s]*\([^\s\(\)]*\)$") ; Exclude windows with , in the title (Popped-out 1-1 chat) and max two words before , Name, Firstname
Continue
If RegExMatch(Title,"^Microsoft Teams Call in progress*") or RegExMatch(Title,"^Microsoft Teams Notification*") or RegExMatch(Title,"^Screen sharing toolbar*")
Continue
If (useFindText) {
; Exclude window with no Leave element
WinActivate, ahk_id %WinId%
If !(ok:=FindText(,,,, 0, 0, LeaveText,,0)) {
Continue
}
; Final check - exclude window with Resume element = On hold meetings
If (ok:=FindText(,,,, 0, 0, ResumeText,,0)) {
Continue
}
}
WinList .= ( (WinList<>"") ? "|" : "" ) Title " {" WinId "}"
WinCount++
; Select by default last meeting window used
If WinId = %TeamsMeetingWinId%
Select := WinCount
} ; End Loop
If (WinCount = 0)
return
If (WinCount = 1) { ; only one other window
RegExMatch(WinList,"\{([^}]*)\}$",WinId)
TeamsMeetingWinId := WinId1
PowerTools_RegWrite("TeamsMeetingWinId",TeamsMeetingWinId)
return TeamsMeetingWinId
}
If (restore)
WinActivate, ahk_id %curWinId%
LB := WinListBox("Teams: Meeting Window", "Select your current Teams Meeting Window:" , WinList, Select)
RegExMatch(LB,"\{([^}]*)\}$",WinId)
TeamsMeetingWinId := WinId1
PowerTools_RegWrite("TeamsMeetingWinId",TeamsMeetingWinId)
return TeamsMeetingWinId
} ; eofun
I have tried to find it out by the Window Title, but Teams does not name Meeting windows in a particular way.
I could find with AccViewer that the Main Team Window has its name ending with "| Microsoft Teams, Main Window". So at least I can exclude this one.
Here is the best solution I have come to.
But it requires the user to confirm which window is the current meeting window (if not obvious).
The code can be found in this Gist: https://gist.github.com/tdalon/87590637e43479c90f355be90aff3842#file-teams_getmeetingwindow-ahk
Here is another solution (my preferred one) based on UIAutomation library - see https://tdalon.blogspot.com/2022/07/ahk-get-teams-meeting-win.html
Gist code https://gist.github.com/tdalon/5b3304559852d1d1942b7f1566de2a0c
Related
I want to write a script in autohotkey so that every time I open my dictionary application on PC, keys Windows+LeftArrow being pressed at the same time and as the result, it snaps the windows on the left side of monitor.
I tried this:
#IfWinActive Oxford Advanced Learner's Dictionary
Send, #{Left}
return
Also this one:
#IfWinActive Oxford Advanced Learner's Dictionary
Send, {LWinDown}{Left}{LWinup}
return
But for either of them noting happened when I opened the application.
EDIT:
As suggested by #Charlie Armstrong the real question is: How do I make a block of code run every time I start a certain program? So #IfWinActive might not be useful for.
One way is periodically check if new process/window is created and to check if that is a process/window we want to interact with.
This first example is based on COM notifications when a process has been created/destroyed.
; help for question: https://stackoverflow.com/q/66394326/883015
; by joedf (16:04 2021/02/28)
MyWatchedWindowTitle := "Oxford Advanced Learner's Dictionary"
NewProcess_CheckInterval := 1 ; in seconds
SetTitleMatchMode, 2 ;this might not be needed, makes the check for "contains" instead of "same" winTitle
hWnds := []
gosub, initialize_NewProcessNotification
return
; Called when a new process is detected
On_NewProcess(proc) {
global hWnds
global MyWatchedWindowTitle
; get the window handle, if possible
if (hwnd:=WinExist("ahk_pid " proc.ProcessID)) {
WinGetTitle, wTitle, ahk_id %hwnd%
; check if there is a visible window
if (wTitle)
{
; if so, check if it's a window we want to interact with
if (InStr(wTitle,MyWatchedWindowTitle))
{
; check if we've interacted with this specific window before
if (!ArrayContains(hWnds, hwnd)) {
; we havent, so we do something with it
hWnds.push(hwnd) ; keep in memory that we have interacted with this window ID before.
DoSomething(hwnd) ; the keys we want to send to it
}
}
}
}
}
DoSomething(hwnd) {
; size and move window to the left
SysGet, MonitorWorkArea, MonitorWorkArea
posY := 0
posX := 0
width := A_ScreenWidth // 2
height := MonitorWorkAreaBottom
WinMove, ahk_id %hwnd% ,,%posX%,%posY%,%width%,%height%
; multi-montitor support, more examples, and more complete snapping functions can be found here:
; https://gist.github.com/AWMooreCO/1ef708055a11862ca9dc
}
ArrayContains(haystack, needle) {
for k, v in haystack
{
if (v == needle)
return true
}
return false
}
initialize_NewProcessNotification:
;////////////////////////////// New Process notificaton ////////////////////////
; from Lexikos' example
; https://autohotkey.com/board/topic/56984-new-process-notifier/#entry358038
; Get WMI service object.
winmgmts := ComObjGet("winmgmts:")
; Create sink objects for receiving event noficiations.
ComObjConnect(createSink := ComObjCreate("WbemScripting.SWbemSink"), "ProcessCreate_")
ComObjConnect(deleteSink := ComObjCreate("WbemScripting.SWbemSink"), "ProcessDelete_")
; Set event polling interval, in seconds.
interval := NewProcess_CheckInterval
; Register for process creation notifications:
winmgmts.ExecNotificationQueryAsync(createSink
, "Select * from __InstanceCreationEvent"
. " within " interval
. " where TargetInstance isa 'Win32_Process'")
; Register for process deletion notifications:
winmgmts.ExecNotificationQueryAsync(deleteSink
, "Select * from __InstanceDeletionEvent"
. " within " interval
. " where TargetInstance isa 'Win32_Process'")
; Don't exit automatically.
#Persistent
return
; Called when a new process is detected:
ProcessCreate_OnObjectReady(obj) {
proc := obj.TargetInstance
/*
TrayTip New Process Detected, % "
(LTrim
ID:`t" proc.ProcessID "
Parent:`t" proc.ParentProcessID "
Name:`t" proc.Name "
Path:`t" proc.ExecutablePath "
Command line (requires XP or later):
" proc.CommandLine
)
*/
On_NewProcess(proc)
}
; Called when a process terminates:
ProcessDelete_OnObjectReady(prm) {
/*
obj := COM_DispGetParam(prm, 0, 9)
proc := COM_Invoke(obj, "TargetInstance")
COM_Release(obj)
TrayTip Process Terminated, % "
(LTrim
ID:`t" COM_Invoke(proc, "Handle") "
Name:`t" COM_Invoke(proc, "Name")
)
COM_Release(proc)
*/
}
This second example, which is perhaps a bit simpler, checks periodically for new windows that match the searched WinTitle.
; help for question: https://stackoverflow.com/q/66394326/883015
; by joedf (16:17 2021/02/28)
#Persistent
MyWatchedWindowTitle := "Oxford Advanced Learner's Dictionary"
SetTitleMatchMode, 2 ;this might not be needed, makes the check for "contains" instead of "same" winTitle
SetTimer, checkForNewWindow, 1000 ;ms
hWnds := []
return
checkForNewWindow() {
global hWnds
global MyWatchedWindowTitle
; first check if there is at least one window that matches our winTitle
if (hwnd:=WinExist(MyWatchedWindowTitle)) {
; get all window matches
WinGet, wArray, List , %MyWatchedWindowTitle%
; loop through all windows that matched
loop % wArray
{
hWnd := wArray%A_Index%
; check if we've interacted with this specific window before
if (!ArrayContains(hWnds, hwnd)) {
; we havent, so we do something with it
hWnds.push(hwnd) ; keep in memory that we have interacted with this window ID before.
DoSomething(hwnd) ; the keys we want to send to it
}
}
}
}
DoSomething(hwnd) {
; size and move window to the left
SysGet, MonitorWorkArea, MonitorWorkArea
posY := 0
posX := 0
width := A_ScreenWidth // 2
height := MonitorWorkAreaBottom
WinMove, ahk_id %hwnd% ,,%posX%,%posY%,%width%,%height%
; multi-montitor support, more examples, and more complete snapping functions can be found here:
; https://gist.github.com/AWMooreCO/1ef708055a11862ca9dc
}
ArrayContains(haystack, needle) {
for k, v in haystack
{
if (v == needle)
return true
}
return false
}
I think your biggest issue is that AHK doesn't seem to work well for snapping windows (according to my quick research and testing). What does work well, though, is WinMove.
I assume you're launching the program from a shortcut icon, but I would suggest using a keyboard shortcut that launches the program and then positions the window from the script. Here is some sample code that opens Notepad2.exe, waits for 200 milliseconds, and then moves the window and resizes it:
^+!n:: ; Control+Shift+Alt+N to Open Notepad
Run C:\Program Files\Notepad2\Notepad2.exe
sleep, 200
WinMove, Notepad2,, 10, 20, 800, 600
return
I'm a bit new to Autohotkey.
I have 3 Language installed, one of them I use very rarely. so I want to Toggle switch only between 2 languages (EN-RU) using WIN+Space.
I searched web found code which was short and logically close to my need, I done some modification to be more object like and personalised conditions.
Code: Select all - Expand View
#Space::
SetFormat, Integer, H
Lang := { "EN" : "0x4090409" , "RU" : "0x4190419", "HE": "0x40D040D" }
WinGet, WinID,, A
ThreadID:=DllCall("GetWindowThreadProcessId", "Int", WinID, "Int", "0")
InputLocaleID:=DllCall("GetKeyboardLayout", "Int", ThreadID)
if(InputLocaleID=Lang.RU OR InputLocaleID=Lang.HE)
SendMessage, 0x50,, % Lang.EN,, A
else if(InputLocaleID=Lang.EN)
SendMessage, 0x50,, % Lang.RU,, A
Exit
it works well with most of programs but with few of them it only get switch to RU.
Works Well: Chrome, notePad, notePad++, Notion, VScode, Android Studio, IntelliJ IDEA,
Doesn't Work: OneNote Windows 10 app (even so in Onenote 2016 it works fine)
If someone can help me with it I would Greatly appreciate it.
Thanks in Advance!
related answer Autohotkey Forum
#Space:: ; Switch keyboard (EN-RU)
Lang := { "EN": 0x4090409, "RU": 0x4190419 }
; For compatibility with UWP apps, get the thread of the focused
; control, not the active window. This is necessary because those
; apps are hosted within a window owned by a different process.
ControlGetFocus Focused, A
ControlGet CtrlID, Hwnd,, % Focused, A
; Using Ptr vs. Int vs. UInt won't matter in these cases
ThreadID := DllCall("GetWindowThreadProcessId", "Ptr", CtrlID, "Ptr", 0)
; HKL is a handle type (64-bit on x64)
InputLocaleID := DllCall("GetKeyboardLayout", "UInt", ThreadID, "Ptr")
if (InputLocaleID != Lang.EN)
SendMessage, 0x50,, % Lang.EN,, ahk_id %CtrlID%
else
SendMessage, 0x50,, % Lang.RU,, ahk_id %CtrlID%
Exit
WinGet, OutputVar, List can get a list of all unhidden windows. It shows way more than what user can see on the Windows taskbar.
How to limit the window numbers to those shown on the taskbar. Like the simple list in the Task Manager. (Fewer details mode)
Here is one method that is simple, I'm not sure how reliable it is.
It works by trying to minimize the window, if it gets minimized then it means it is available on the taskbar.
You can get the list of found/active windows in any form you need, here I use arrays.
getWindows:
activeWindowsId := []
activeWindowsExe := []
newlist := ""
WinGet, List, List ;get the list of all windows
Sendinput, #m ;mimimize all windows by using the windows shortcut win+m
sleep, 200
;Loop % List ;or use this loop to minimze the windows
;{
; WinMinimize, % "ahk_id " List%A_Index%
;}
Loop % List
{
WinGet, theExe, ProcessName, % "ahk_id " List%A_Index% ;get the name of the exe
WinGet, windowState, MinMax, % "ahk_id " List%A_Index% ;get the state of the window, -1 is minimized, 1 is maximized, 0 is neither
if (windowState == -1) ;if the window is minimized
{
activeWindowsId.push(List%A_Index%) ;add the window id (hwnd) to this array
activeWindowsExe.push(theExe) ;add the window exe to this array
WinRestore, % "ahk_id " List%A_Index% ;restore the window
}
}
;read the found active windows list from the array to a string and show it with a msgbox
For index, exe in activeWindowsExe
{
newList .= exe "`n"
}
msgbox, % newList
return
I often save files from Chrome to the Downloads folder or My Documents folder.
You have to look for these files later in the folder. Tell me whether it is possible to somehow make it more convenient using the ahk script. By pressing F3 for example, the last file or folder in the current folder is highlighted. How to implement it. Please help me, I'm a new
F3::
SetTitleMatchMode, 2
File := "" ; empty variable
Time := ""
Loop, Files, %A_MyDocuments%\Downloads\*.*, DF ; include files and folders
{
If (A_LoopFileTimeModified >= Time)
{
Time := A_LoopFileTimeModified ; the time the file/folder was last modified
File := A_LoopFileFullPath ; the path and name of the file/folder currently retrieved
}
}
; MsgBox, Last modified file in %A_MyDocuments%\Downloads is`n`n"%File%"
IfWinNotExist Downloads ahk_class CabinetWClass
Run % "explorer.exe /select," . File ; open containing folder and highlight this file/folder
else
{
SplitPath, File, name
MsgBox, Last modified file = "%name%"
WinActivate, Downloads ahk_class CabinetWClass
WinWaitActive, Downloads ahk_class CabinetWClass
; SelectExplorerItem(name) ; or:
SendInput, %name%
}
return
SelectExplorerItem(ItemName) { ; selects the specified item in the active explorer window, if present
; SelectItem -> msdn.microsoft.com/en-us/library/bb774047(v=vs.85).aspx
Window := ""
Static Shell := ComObjCreate("Shell.Application")
For Window In Shell.Windows
try IfWinActive, % "ahk_id " window.HWND
If (Item := Window.Document.Folder.ParseName(ItemName))
Window.Document.SelectItem(Item, 29)
Return (Item ? True : False)
}
https://autohotkey.com/docs/commands/LoopFile.htm
I'm using Firefox and I was looking for an autohotkey script which would enable me to skip the whole series of clicks when I bookmark a page in a specific folder and replace it with a single keyboard shortcut.
Although I've read forum threads on Autohotkey forum and here I still don't know how to make a working script that would reduce bookmarking a page to hitting a keyboard shortcut and the initial letter of the folder where I want to store that page. Using KeyWait command I've made it work for a single folder and don't know how to make it work for any letter or a number that I could possibly use as a name for a bookmark folder. Say I have a folder named XXXX, this script does send the webpage to the XXXX folder after hitting the assigned shortcut and the letter x (MouseClick command is needed to focus the window with folder in the Bookmark Dialog pane):
!+^w::
Send,^d
Sleep,400
MouseClick,Left,864,304
Sleep,400
KeyWait, x ,D
Sleep, 400
Send,^{Enter}
return
I don't know how to make this script work for any letter or number, not only for a single one. Also a big problem with this script is that it blocks the keyboard until I hit X key. If I have that page bookmarked already, hitting escape to remove the bookmark pane will block the keyboard and I can unblock it only if I rerun the autohotkey script.
I've also tried using Input command as the contributors the Autohotkey forum pages suggested, but it didn't work either, because I don't understand how the Input command works. I did make it work for a single letter as the above script with KeyWait, but that's the best I could do. This script also blocks the keyboard until the letter is hit:
!+^w::
Send,^d
MouseClick,Left,864,304
Sleep,400
Input, Character, L1
If Character = t
Send, t
Sleep,400
Send,^{Enter}
return
Hope someone can help me with this, it would be convenient simplifying the bookmarking process in Firefox this way.
I have a nice idea. This will open the add bookmark dialog and navigate you into the folder selection part and expand all folders.
All you need to do is enter the folders name (or a part of it) and it will get selected automatically. When you're done, just hit enter.
!+^w:: ;Hotkey: Ctrl+Alt+Shift+w
Send, ^d ;send Ctrl+d to open the add-bookmark dialog
Sleep, 500 ;wait for the dialog to open
Send, {Tab}{Tab}{Enter} ;navigate into the folder selection
Sleep, 300 ;wait to make sure we are there
Send,{Home} ;select the first item in the list
;The following line should expand all the folders so that you can just type the folders name to search for it
Loop, 100 { ;Increase the number if it doesn't expand all folders
SendInput, {Right}{Down} ;expand folders
}
Send, {Home} ;navigate to the first item in the list again
Input, L, V L1 T2 ;wait until you start typing a folder name (if you just wait 2 seconds, the bookmark won't be created)
If (ErrorLevel = "Timeout") {
Send, {Tab 5} ;press tab 5 times to navigate to the cancel button
Send, {Enter} ;cancel the bookmark
Return ;end of the hotkey
}
Loop { ;wait until you haven't typed a new letter for 0.4 seconds
Input, L, V L1 T0.4
If (ErrorLevel = "Timeout")
Break
}
Send, {Tab 4} ;press tab 4 times to navigate to the enter button
Send, {Enter} ;save the bookmark
Return
My variant.
Press f1 to create bookmark in folder test1.
Press f2 to create bookmark in folder test2.
SetBatchLines, -1
Folders := {F1: "test1", F2: "test2"}
#IfWinActive, ahk_class MozillaWindowClass
F1::
F2::
Folder := Folders[A_ThisHotkey]
Send, ^d
AccFirefox := Acc_ObjectFromWindow(WinExist("ahk_class MozillaWindowClass"))
AccElem := SearchElement(AccFirefox, ROLE_SYSTEM_LISTITEM := 0x22, Folder, "")
AccElem.accDoDefaultAction(0)
sleep 100
Send {Enter}
msgbox done
return
#IfWinActive
SearchElement(ParentElement, params*)
{
found := 1
for k, v in params {
(k = 1 && ParentElement.accRole(0) != v && found := "")
(k = 2 && ParentElement.accName(0) != v && found := "")
(k = 3 && ParentElement.accValue(0) != v && found := "")
}
if found
Return ParentElement
for k, v in Acc_Children(ParentElement)
if obj := SearchElement(v, params*)
Return obj
}
Acc_Init()
{
Static h
If Not h
h:=DllCall("LoadLibrary","Str","oleacc","Ptr")
}
Acc_ObjectFromWindow(hWnd, idObject = 0)
{
Acc_Init()
If DllCall("oleacc\AccessibleObjectFromWindow", "Ptr", hWnd, "UInt", idObject&=0xFFFFFFFF, "Ptr", -VarSetCapacity(IID,16)+NumPut(idObject==0xFFFFFFF0?0x46000000000000C0:0x719B3800AA000C81,NumPut(idObject==0xFFFFFFF0?0x0000000000020400:0x11CF3C3D618736E0,IID,"Int64"),"Int64"), "Ptr*", pacc)=0
Return ComObjEnwrap(9,pacc,1)
}
Acc_Query(Acc) { ; thanks Lexikos - www.autohotkey.com/forum/viewtopic.php?t=81731&p=509530#509530
try return ComObj(9, ComObjQuery(Acc,"{618736e0-3c3d-11cf-810c-00aa00389b71}"), 1)
}
Acc_Error(p="") {
static setting:=0
return p=""?setting:setting:=p
}
Acc_Children(Acc) {
if ComObjType(Acc,"Name") != "IAccessible"
ErrorLevel := "Invalid IAccessible Object"
else {
Acc_Init(), cChildren:=Acc.accChildCount, Children:=[]
if DllCall("oleacc\AccessibleChildren", "Ptr",ComObjValue(Acc), "Int",0, "Int",cChildren, "Ptr",VarSetCapacity(varChildren,cChildren*(8+2*A_PtrSize),0)*0+&varChildren, "Int*",cChildren)=0 {
Loop %cChildren%
i:=(A_Index-1)*(A_PtrSize*2+8)+8, child:=NumGet(varChildren,i), Children.Insert(NumGet(varChildren,i-8)=9?Acc_Query(child):child), NumGet(varChildren,i-8)=9?ObjRelease(child):
return Children.MaxIndex()?Children:
} else
ErrorLevel := "AccessibleChildren DllCall Failed"
}
if Acc_Error()
throw Exception(ErrorLevel,-1)
}