在Bash函数中设置gnome-keybindings

时间:2019-06-05 09:23:28

标签: bash

我正在编写一个Bash脚本来自动为Gnome桌面设置键盘绑定。更改已经存在的快捷方式没问题。

问题出在服装按键上。我可以对这些命令进行硬编码,并且可以完美运行,但是我无法使用Bash函数设置服装按键绑定。

一个很难编写的脚本是这样的:

gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom4/ name "vscode"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom4/ command "code"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom4/ binding "<Super>v"
gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom4/']"

就像我提到的那样,这将起作用。

所以我试图为此编写一个Bash函数:

# a function to create costume keybindings
# first arg: a number assigned to keybinding
# second arg: name of the keybinding
# third arg: command for the keybinding
# forth arg: the keyboard shortcut for the keybinding
function key {
    gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$1/ name $2
    gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$1/ command $3
    gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$1/ binding $4
    gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$1/']"
}

我试图按如下方式使用它:

key 0 "poweroff" "poweroff" "<Shift><Super>s"
key 1 "reboot" "reboot" "<Shift><Super>r"
key 2 "keepassx" "keepassx" "<Super>k"
key 3 "calculator" "gnome-calculator" "<Super>c"
key 4 "vscode" "code" "<Super>v"
key 5 "toggle_touchpad" "/home/ali/p/os/scripts/gnome_toggle_touchpad.sh" "<Super>t"

除了最后一个toggle_touchpad以外,以上所有键绑定均无效。

我怀疑toggle_touchpad可能有用,因为我为其提供了完整的地址而不是命令。所以我创建了一个简单的脚本,如下所示:

#!/bin/bash
code

此脚本有效,但是当我将其放入键盘绑定脚本中时,它不起作用:

key 4 "vscode" "/home/ali/p/os/scripts/code.sh" "<Super>v"

我不确定我的脚本出了什么问题,但是我认为它与最后一行功能有关,因为前三行的配置出现在dconf中:

图形dconf界面,显示与服装键绑定相关的设置
enter image description here

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

最可能的问题是您没有引用变量:

function key {
    gsettings set "org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$1/" name "$2"
    gsettings set "org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$1/" command "$3"
    gsettings set "org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$1/" binding "$4"
    gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$1/']"
}