22

Suppose, for example, I have a chrome window active with gmail opened somewhere among its tabs (but not necessarily the active tab).

Question: Is there a way to activate the gmail tab from the command line?

7 Answers 7

8

Yes. I currently do it like this:

1) you'll need to install jq to mess around with tab data you'll get fed when invoking the script of currently open tabs in chromium. also you'll need to install chrome-remote-interface to do the work of activating the tabs from background -> foreground of chromium.


2) chromium needs to be running with remote debugging turned on. close all instances of chromium, and run this command instead of just chromium in terminal otherwise this process will not work:

/usr/bin/chromium --remote-debugging-port=9222 &


3) use this base script as an example and save it as act, chmod +x it and place it in your user's bin directory. I use bash script for example, it can be ported over if you're a zsh user:

#!/usr/bin/env bash

TABS_JSON=$(chrome-remote-interface list | sed -e "s/^'//" -e "s/'$//" | jq -r 'map(select(.type == "page") | {id: .id, title: .title})')

if [[ -z $@ ]]; then
    TAB_NAMES=$(echo "$TABS_JSON" | jq -r 'map(.title) | .[]')

    echo "$TAB_NAMES"
else
    TAB=$*

    TAB_ID=$(echo "$TABS_JSON" | jq -r "map(select(.title | match(\"${TAB}\";\"i\")) | .id) | .[]")

        chrome-remote-interface activate "$TAB_ID" >/dev/null

    #you might need wmctrl if window does not activate. 
    #wmctrl -a chromium
fi

navigate to cnn.com and a couple of other tabs and keep cnn.com in the background, switch to terminal and try out the command like so:

act cnn

It should activate chromium window and switch to cnn tab. You are free to further integrate this with your rofi menu or fzf variant in your workflow, and even throw in the list of open tabs into the global pool of active windows.

1
6

The following script works for me, but it requires that you only have one Chrome window, and that window to be active. It should be easy to tweak it or improve it.

WINID=$(xdotool search --name 'Google Chrome' | head -n 1)
WINID_HEX=$(printf "0x%x" $WINID)

while true
do
        xwininfo -id $WINID_HEX | grep Gmail
        if [ "$?" -ne 0 ]
        then
                xdotool key --window $WINID ctrl+Tab
        else
                break
        fi
        sleep 2
done

There are probably many other ways to do this:

  • Using Chromium and modify its source code
  • Writing an extension for Chrome that would switch to the right tab when a certain condition is met
  • Using tools like xmacro
  • Etc.
2
  • 2
    Note: xwininfo will accept a decimal $WINID.
    – meuh
    Oct 21, 2015 at 17:31
  • 1
    The solution needs code to prevent infinite loop when Gmail tab is not open.
    – Gonki
    Sep 4, 2019 at 3:55
1

I tweaked Geoffroy's answer a bit since it didnt work for me.

  • wmctrl -lx is used to get all windows and their titles. The current tab title is inside the windows tab name.
  • wmctrl -ia is used to activate the chrome window

Here is the result,

function ctab() {

  # Activate the chrome window
  wmctrl -ia "$(wmctrl -lx | grep google-chrome | awk '{ print $1}')"

  # Create a while loop that continues until it finds the correct tab.
  # Note that this will go on forever if the tab is not found
  while true; do
    
    # Check the current windows title (which includes the tab title)
    result="$(wmctrl -lx | grep google-chrome | grep "@gmail.com")"
    
    # if the title contains @gmail then exit
    [ -n "$result" ] && break 

    # If it is the incorrect title then change to the next tab
    WINID=$(xdotool search --name 'Google Chrome' | head -n 1)
    xdotool key --window $WINID ctrl+Tab

  done
}
0

In linux (Debian) I use google-chrome URL (example: google-chrome google.com). A new tab would be open.

Type google-chrome --help for more detailed description.


I found this for mac (but didn't try): chrome-cli open <url> -n command.

It depends of https://github.com/prasmussen/chrome-cli

0

This perl code works for me: e.g., findTab.pl Chrome Gmail. It makes the screen flip a bunch and can change tabs in other windows (it checks 12 times on the theory that this should leave your window on the original tab if it doesn't find anything, as long as you have <5 tabs in the window).

It does not work on my Ubuntu system if you don't use windowactivate before key; something to do with very old security protocols.

I don't think there's any guarantee what it will find if there's more than one thing to find; I suspect based on other xdotool experience that it is likely to find different things when used repeatedly.

perl is probably not a good choice for this script, but I don't know bash.

#! /usr/bin/perl
## findTab.pl
## usage findTab appName tabName
use strict;

my $maxTabs=12;

my ($app, $tab) = @ARGV;

my @wins = split /\s+/, `xdotool search --desktop 0 --name $app`;

foreach my $win (@wins){
    my $title;
    for (my $i=0;$i<$maxTabs;$i++){
        system ("xdotool windowactivate $win");
        $title = `xwininfo -id $win | grep $tab`;
        last if $title;
        system("xdotool key --window $win ctrl+Page_Down");
    }
    last if $title;
}
0

Well, my gmail tab is pinned and stays first. This little script helps me

#!/bin/bash                                                                                                              

wmctrl -a $1                                                                                                             
xdotool search --onlyvisible --class $1 key Alt+1

./activate_mail.sh Chromium
./activate_mail.sh Firefox

It works on my i3wm environment.

0

If you just want to do the command, you can do ctrl+Page_Down a number of times to page through your tabs until you get to the desired tab using the xdotool key command:

xdotool key "ctrl+Page_Down"

This assumes that Google Chrome is currently in focus.


You can also use ctrl+1, ctrl+2 . . . , ctrl+n to switch to the nth tab.

For example if gmail is on your 4th tab, you could use the following xdotool command:

xdotool key "ctrl+4"

As a script, you could set the nth tab as an input variable:

#!/bin/bash
xdotool key "ctrl+$1"

If the script is named tab, you would run the following to switch to the 6th tab

./tab 6

or if you want to page through tabs you could do the following instead

./tab Page_Down

or

./tab Page_Up

You must log in to answer this question.

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