Copy a window to multiple workspaces

Is there a way to copy a window to more than one workspace. E.g. if chrome is open on the desktop of Workspace 1, instead of moving it, I would like to copy it to Workspace X, such that, when I switch to Workspace X, chrome window is present there as well.

Suggestions don't need to be limited to OS only solutions. If someone can suggest an easily acquirable free program, that would really help as well.

Current OS Version: Ubuntu 14.04-amd64


Kubuntu:

showing a window on multiple destops works out of the box. Just right click on the app icon in the task bar

screenshot

select "Move to Desktop" >> "All Desktops"

This way the window is displayed on every "Desktop" (the wording "move" in the context menu is a bit misleading).

Gnome

As it turns out this works out of the box as well

Right click on the Window Title and select "Always on visible Workspace"

enter image description here


I created the following bash script that I think approximates what the OP wants. It doesn't copy the selected window on the selected workspaces. It rather automatically moves the selected window on the selected workspaces when the latter change.

#!/bin/bash
# get id of window
id=$(xwininfo -id $(xdotool getwindowfocus) | grep "Window id" | cut -d' ' -f4)
# get pid of script
script_pid=$(echo $$)

# file for temporarily storing id-pid pairs
temp="/tmp/ids-pids.txt"
# create the file if it does not exist
touch $temp

# check if window id already exists in temp
# if yes, kill the script and remove the window id entry
# if no, add a window id-script pid entry
# this is used as a toggle for turning the script on/off with the same keystroke
if grep -q $id $temp; then
    kill $(grep $id $temp | cut -d' ' -f2)
    sed -i "/$id/d" $temp 
    kill $$
else
    echo $id $script_pid >> $temp
fi

# enter the workspaces numbers that the window can appear on as an array
# input is space-delimited
IFS=' ' read -r -a array < <( zenity --entry \
                --width 400 \
                --title="Window on multiple workspaces" \
                --text="Window on workspaces:" )

# if the user cancels text entry or inputs nothing, then exit and kill the script
if [ ${#array[@]} -eq 0 ]; then
    sed -i "/$id/d" $temp 
    kill $$
fi

while true
do
    # get current workspace
    workspace=$(wmctrl -d | grep \* | cut -d' ' -f1)
    # move window to the workspace if it is in the allowed workspaces array
    if [[ "${array[@]}" =~ "${workspace}" ]]; then 
        wmctrl -i -r $id -t $workspace
    fi

    # check if window is closed
    # if yes, remove its id-pid entry and kill the script 
    if echo $(xwininfo -id $id 2>&1) | grep -q "X Error:"; then
        sed -i "/$id/d" $temp
        kill $(grep $id $temp | cut -d' ' -f2)
        kill $$
    fi

    # sleep is used so that the command won't run continuously
    sleep 1
done

How to use

  • First, make sure you have the programs that are used in this script installed:

    sudo apt install xdotool wmctrl zenity
    
  • Copy and paste the script in an .sh file in your computer, for example in your Home folder (/home/user/window_workspaces.sh).

  • Make it executable:

    chmod u+x /home/user/window_workspaces.sh
    
  • Assign the script to a shortcut. In GNOME, open Applications OverviewSettingsDevicesKeyboard → click the "+" at the bottom and enter a shortcut name, the path to the script and the shortcut you wish to use, as shown in the picture below. I have chosen Super+Z as the script's shortcut.

    GNOME shortcut

  • On your focused window, press Super+Z. A window will pop up where you can enter the workspaces you wish your window to appear on, separated by space, as shown below.

    Workspaces entry

  • Press Super+Z again while having the same window focused and it will return back to normal, i.e. only appear in one workspace.

The script should work in all desktop environments.

Feel free to suggest any changes!