在Gnome中的不同工作区打开应用程序

18

由于我的懒惰,我尝试编写一个Bash脚本,可以在不同的桌面同时打开一些常用应用程序。这个脚本应该可以在Gnome环境下运行。到目前为止,我已经写了如下内容:

#!/bin/bash
firefox &
thunderbird &
/usr/bin/netbeans --locale en &
amsn &
gnome-terminal &
sleep 2
wmctrl -r firefox -t 0 && wmctrl -r netbeans -t 1 && wmctrl -r gnome-terminal -t 2 && wmctrl -r amsn -t 6 && wmctrl -r thunderbird -t 7

...但是它不起作用。我的应用程序打开了,但它们没有被分配到我指定的桌面 :(。

我将sleep的值更改为15.,但只有firefox和netbeans被正确地分配;其余的在我执行脚本的工作区中打开。


@Rolf 我之前遇到了类似的问题,直到我注意到你的 sleep 15。那时我才意识到,在使用 wmctrl 操纵窗口之前,需要给应用程序一些时间来启动。 - Serge Stroobandt
4个回答

10

多亏了 Akira 的评论,我终于成功让它工作了(脚本在启动时运行得很好)。以下是新代码:

#!/bin/bash
wmctrl -n 8

firefox &
thunderbird &
/usr/bin/netbeans --locale en &
amsn &
gnome-terminal &
sleep 15

wmctrl -r firefox -t 0
wmctrl -r netbeans -t 1 
wmctrl -r terminal -t 2 
wmctrl -r amsn -t 6 
wmctrl -r thunderbird -t 7

#focus on terminal
wmctrl -a terminal 

你如何区分同一进程的多个实例?假设你想要在工作区1上有一个gnome-terminal实例,而在工作区2上有另一个不同的实例?使用“-r”参数似乎不支持这种情况。 - Cerin
我正在使用GNOME Wayland,并且wmctrl -r命令无法正常工作。 - muhammad tayyab

2
在dconf-editor中:
org->gnome->shell->extensions->auto-move-windows
here is what it should look like:
['firefox.desktop:1','pidgin.desktop:2']

此外,必须启用auto-move-windows扩展。 - gkcn
1
我正在寻找一种将应用程序固定到所有工作区的方法。有一种未记录的功能可以实现这一点。只需将工作区编号设置为0即可。我是偶然发现了这个功能。也许这不是一个功能,而是一个错误 - 但我喜欢这个错误 :-)。如果您想在所有桌面上使用Pidgin,请设置['firefox.desktop:1','pidgin.desktop:0']。 - Martin Edlman

2

推荐使用DevilsPie,它可以监测窗口的创建并根据预先设置的规则进行相应操作。

Devil's Pie可以配置为检测窗口的创建,并将窗口与一组规则匹配。如果窗口符合规则,则可以对该窗口执行一系列操作。例如,我可以使由X-Chat创建的所有窗口出现在所有工作区中,而主Gkrellm1窗口不会出现在分页器或任务列表中。

或者您可以使用内置此功能的窗口管理器,例如fluxbox


1
这个脚本将检查是否需要更改工作区,切换到该工作区,启动应用程序,等待窗口创建并切换回原始命名空间。 由于它使用wmctrl -l来检查是否创建了新窗口,因此可以处理快速或慢速启动的应用程序,而无需等待静态秒数。 我将此脚本称为start-on-workspace
用法:start-on-workspace <Workspace> <command> [argument...
#!/bin/sh -e
# get the target ns
target=$(($1 - 1))
shift

# get the current ns
current=$(wmctrl -d | grep '*' | cut -d' ' -f1)
if [ $current != target ]; then
    # switch to target ns
    wmctrl -s $target
fi

# get a checksum of currently running windows
a=$(wmctrl -l | cut -d' ' -f1 | sha1sum | cut -d' ' -f1)
b=$a

# start the app
$@ &

# wait until there is a change on the window list
while [ $a = "$b" ]; do
    a=$(wmctrl -l | cut -d' ' -f1 | sha1sum | cut -d' ' -f1)
    sleep 0.1
done

# switch back to the origin namespace if needed
if [ $current != target ]; then
    wmctrl -s $current
fi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接