为每个应用程序分配默认键盘语言

gro*_*gor 13 keyboard keyboard-layout unity language 15.04

是否可以为特定应用程序分配默认键盘语言?我知道有一个选项可以始终使用默认键盘语言启动应用程序,但这不是我想要的。

我正在寻找类似于“固定工作区”的东西,您可以使用 CompizConfig 进行设置(您将 Chrome 设置为始终在 X1Y1 处打开,在 X2Y1 处设置终端等)。我会设置 Chrome 的地方:捷克语,终端:英语,Spotify:英语,......

Ser*_*nyy 6

介绍

下面的脚本根据该语言在语言菜单中的位置为每个用户定义的程序设置语言。例如,如果我的订单是:英语(1)、中文(2)和俄语(3),我可以将 Firefox 设置为语言 2,终端设置为语言 1,LibreOffice 设置为语言 3。

脚本分为两部分:第一部分是完成工作的实际脚本,第二部分用作控制元素。这个想法是将语言设置脚本作为启动应用程序运行,并且每当您需要手动更改语言时 - 双击控制器脚本的快捷方式。

先决条件

  1. 安装wmctrl程序sudo apt-get install wmctrl

脚本

#!/bin/sh
# Author: Serg Kolo
# Date: August 4, 2015
# Description: This script forces assigned input languages
#              for specific windows
# Version:2

# Use this part to set programs and their respective languages
# PROG_CLASS or a running window can be found with the
# wmctrl -lx command
# If you want to add another program to the list, 
# follow the template PROG_CLASS_num=window.Class
# and bellow that $LANGnum=num


PROG_CLASS_1=gedit.Gedit
LANG1=2

PROG_CLASS_2=gnome-terminal-server.Gnome-terminal
LANG2=0

PROG_CLASS_3=Navigator.Firefox
LANG3=1


# While loop below gets the job done. 
# If you need to send languages for more programs -  copy  
# the first entry and replace  $PROG_CLASS_1 with $PROG_CLASS_num
# where num is respective number of a program
# Replace $LANGnum with the respective language number. After the "="
# post positional number of the language you want to use. 
# Remember the count starts from 0

while [ 1 ];do
  WM_CLASS=$(wmctrl -lx | awk -v search=$(printf %x $(xdotool getactivewindow)) '{ if($1~search) print $3 }' )
  CURRENT=$(gsettings get org.gnome.desktop.input-sources current| awk '{print $2}')
    case  $WM_CLASS in

      $PROG_CLASS_1)  
        if [ $CURRENT -ne  $LANG1 ];then
          gsettings set org.gnome.desktop.input-sources current $LANG1
        fi
      ;;

      $PROG_CLASS_2) 
        if [ $CURRENT -ne  $LANG2 ];then
          gsettings set org.gnome.desktop.input-sources current $LANG2
        fi  
      ;;

       $PROG_CLASS_3) 
         if [ $CURRENT -ne  $LANG3 ];then
           gsettings set org.gnome.desktop.input-sources current $LANG3
         fi  
        ;;
    esac

    sleep 0.250

done
Run Code Online (Sandbox Code Playgroud)

控制器脚本

#!/bin/sh
# set -x
# Author: Serg Kolo
# Date: August 8, 2015
# Description: Controller script for set-lang.sh script
# Allows pausing and resuming execution of set-lang.sh
STATUS=$(ps -o stat -p $(pgrep -o  set-lang.sh) | awk '{getline;print }')

case $STATUS in
    T) kill -CONT $(pgrep set-lang.sh) 
       notify-send 'RESUMED'
    ;;

    S) kill -STOP $(pgrep set-lang.sh)
       notify-send 'STOPED'
    ;;

    *) exit ;;
esac 
Run Code Online (Sandbox Code Playgroud)

set-lang.sh 脚本的启动器 (.desktop) 文件

[Desktop Entry]
Name=set-lang.sh
Comment=Script to set languages
Exec=/home/yourusername/bin/set-lang.sh
Type=Application
StartupNotify=true
Terminal=false
Run Code Online (Sandbox Code Playgroud)

set-lang-controller.sh 的启动器 (.desktop) 文件

[Desktop Entry]
Name=lang-control
Comment=Shortcut to controlling script
Exec=/home/yourusername/bin/set-lang-control.sh
Type=Application
StartupNotify=true
Terminal=false
Run Code Online (Sandbox Code Playgroud)

使脚本工作

  1. 在您的主目录中创建一个名为bin. 您可以在文件管理器中执行此操作或mkdir $HOME/bin在终端中使用该命令
  2. bin文件夹中创建两个文件:set-lang.shset-lang-control.sh. 将脚本保存set-lang.shset-lang-control.sh. 使两个脚本都可执行sudo chmod +x $HOME/bin/set-lang-control.sh $HOME/bin/set-lang.sh
  3. 创建两个.desktop文件。一个是set-lang.desktop。必须放在隐藏.config/autostart目录下。第二个是set-lang-controller.desktop,可以放在你的bin文件夹里。接下来将set-lang-controller.desktop文件拖动并固定到启动器。这将成为临时停止和恢复脚本执行的快捷方式。

请注意Exec=必须更改该行以在脚本路径中包含您的实际用户名(因为那是您的实际主目录)。例如,我的将是Exec=/home/serg/bin/set-lang.sh

说明和定制:

脚本本身在无限循环中运行并检查当前活动窗口。如果当前活动窗口与案例结构中的选项之一匹配,我们将切换到适当的语言。为了避免常量设置,case 结构的每个部分都有 if 语句来检查语言是否已经设置为所需的值。

双击启动器set-lang-controller.sh将检查set-lang.sh脚本的状态;如果脚本正在运行 - 它将被暂停,如果脚本被暂停,它将被恢复。将显示带有适当消息的通知。

为了自定义脚本,您可以打开所需的应用程序,运行wmctrl -lx并注意第三列 - 窗口类。示例输出:

$ wmctrl -lx | awk '$4="***" {print}'                                                                                                            
0x02c00007 0 gnome-terminal-server.Gnome-terminal *** Terminal
0x03a0000a 0 desktop_window.Nautilus *** Desktop
0x04a00002 0 N/A *** XdndCollectionWindowImp
0x04a00005 0 N/A *** unity-launcher
0x04a00008 0 N/A *** unity-panel
0x04a0000b 0 N/A *** unity-dash
0x04a0000c 0 N/A *** Hud
0x012000a6 0 Navigator.Firefox *** unity - Assign default keyboard language per-application - Ask Ubuntu - Mozilla Firefox
Run Code Online (Sandbox Code Playgroud)

为每个程序选择合适的窗口类。接下来,转到允许自定义的脚本部分,并为 PROG_CLASS 和 LANG 添加两个条目。接下来在案例结构中添加适当的条目。

例如,如果我想添加 LibreOffice 的 Writer,我打开 LibreOffice Writer 窗口,转到终端并运行wmctrl -lx. 它会告诉我 Writer 窗口有 class libreoffice.libreoffice-writer。接下来我将转到脚本,在适当的区域添加PROG_CLASS_4=libreoffice.libreoffice-writerLANG4=3。注意匹配数字 4。接下来,转到案例结构,并在 last;;和之间添加以下条目esac

$PROG_CLASS_4) 
  if [ $CURRENT -ne  $LANG4 ];then
    gsettings set org.gnome.desktop.input-sources current $LANG4
  fi  
;;
Run Code Online (Sandbox Code Playgroud)

再次注意 $ 符号和匹配的数字 4。

此外,如果脚本作为自动启动项运行并且您想暂时停止它以自定义它,请使用pkill set-lang.sh并继续nohup set-lang.sh > /dev/null 2&>1 &

小提示:另一种找出程序窗口类的方法(在案例结构中单圆括号之前的东西)是使用 thisxpropawkoneliner :xprop | awk '/WM_CLASS/ {gsub(/"/," "); print $3"."$5}