Action to switch to window with name

Is it possible to switch to a window by name? I use Google Meet at work. Sometimes I loose the window for Google Meet and would like to set up a keybinding to bring to the front. I may have multiple Windows open in the given browser, but I'd like to find the one that contains the string "Meet" and focus it. Any help would be greatly appreciated. Thanks.

I don't use Google Meet, but maybe this AppleScript will help:

set theTitle to "Meet"

tell application "System Events"
	repeat with theProcess in ((every process) where background only is false)
		tell theProcess
			repeat with theWindow in windows
				if name of theWindow contains theTitle then
					tell theWindow to perform action "AXRaise"
					set frontmost of theProcess to true
					return
				end if
			end repeat
		end tell
	end repeat
end tell

It is much faster if you specify also the application name:

set theTitle to "Meet"
set theAppName to "Google Chrome"

tell application "System Events"
	repeat with theProcess in ((every process) where background only is false and name is theAppName)
		tell theProcess
			repeat with theWindow in windows
				if name of theWindow contains theTitle then
					tell theWindow to perform action "AXRaise"
					set frontmost of theProcess to true
					return
				end if
			end repeat
		end tell
	end repeat
end tell

Thank you for pointing me in this direction. I am more familiar with JavaScript, so I went with JXA:

var edge = Application('Microsoft Edge');
edge.activate();
edge.includeStandardAdditions = true

edge.windows().some((window, windowIndex) => {
	let tabIndex = null;
	
	const foundTab = window.tabs().some((tab, index) => {
		if (tab.title().includes('Meet')) {
			tabIndex = index;
			return true;
		} else {
			return false;
		}
	});
	
	if (foundTab) {
		edge.windows[windowIndex].activeTabIndex = tabIndex + 1;
		edge.windows[windowIndex].index = 1;
	}
	
	return foundTab;
});

I cleaned it up a bit. I went with JXA because the email from this forum only showed the first solution. When I tried it, it was slow. So then I thought maybe JS is faster.
When I came back to post my solution, I saw the rest of your reply where you mentioned it would be faster if I knew the app ahead of time. Thanks again.

Cleaned up version:

var edge = Application('Microsoft Edge');
edge.activate();
edge.includeStandardAdditions = true

edge.windows().some((window, windowIndex) => {	
	let foundTab = false;
	const tabIndex = window.tabs().findIndex(tab => tab.title().includes('Meet'));
	
	
	if (tabIndex > -1) {
		window.activeTabIndex = tabIndex + 1;
		window.index = 1;
		foundTab = true;
	}
	
	return foundTab;
});

Your script is the best solution for your problem, because it changes the tab at the same time.
My first script is helpful if you searching for a window system wide.
I also optimized it again and found that it might be helpful to use an additional variable instead a reference. The difference is significant. It should be the same in JXA and the Script-Language not make a difference.

set theTitle to "Meet"

tell application "System Events"
	set theProcesses to every process where background only is false
	repeat with theProcess in theProcesses
		tell theProcess
			repeat with theWindow in windows
				if name of theWindow contains theTitle then
					tell theWindow to perform action "AXRaise"
					set frontmost of theProcess to true
					return
				end if
			end repeat
		end tell
	end repeat
end tell
1 Like
Imprint | Privacy Policy