Skip to content

Instantly share code, notes, and snippets.

@debxp
Created January 11, 2020 19:59
Show Gist options
  • Save debxp/aada42c872ccc09975a5c25b9a7921da to your computer and use it in GitHub Desktop.
Save debxp/aada42c872ccc09975a5c25b9a7921da to your computer and use it in GitHub Desktop.
A script to add launchers to Xfce panel
#!/usr/bin/env bash
# NOTE: Xfce panel position scheme -------------------------------------------
# (6)--(11)---(9)--(11)--(2)
# | |
# (5) (1)
# | |
# (7) (0) (3)
# | |
# (5) (1)
# | |
# (8)--(12)--(10)--(12)--(4)
# l10n -------------------------------------------------------------------------
# Text relative to each position number (0-12)...
anchor[0]="fora das bordas"
anchor[1]="borda direita"
anchor[2]="ancorado acima à direita"
anchor[3]="ancorado no meio à direita"
anchor[4]="ancorado abaixo à direita"
anchor[5]="borda esquerda"
anchor[6]="ancorado acima à esquerda"
anchor[7]="ancorado no meio à esquerda"
anchor[8]="ancorado abaixo à esquerda"
anchor[9]="ancorado no centro acima"
anchor[10]="ancorado no centro abaixo"
anchor[11]="borda superior"
anchor[12]="borda inferior"
# General l10n...
l10n_file_word="Arquivo"
l10n_path_word="Caminho"
l10n_contents_word="Conteúdo"
l10n_panel_word="Painel"
l10n_choose_panel="Escolha um painel"
l10n_single_panel="O lançador será inserido no painel"
l10n_no_panel="
Não existem painéis na sua área de trabalho!
Crie ao menos um painel e tente novamente.
Saindo...
"
l10n_set_desktop_file="
Agora vamos criar um novo lançador.
Os campos marcados com (*) são obrigatórios!
"
l10n_launcher_name="Nome do lançador (*): "
l10n_launcher_comment="Descrição: "
l10n_launcher_exec="Comando a ser executado (*): "
l10n_launcher_icon="Caminho ou nome do ícone (*): "
l10n_launcher_path="Caminho de execução: "
l10n_launcher_terminal="Executar no terminal (true/false): "
l10n_launcher_startup="Notificação de lançamento (true/false): "
# Functions --------------------------------------------------------------------
# xfconf-query default command...
xfconf-panel() {
command xfconf-query -c xfce4-panel -p /panels"$@"
}
# Print string whitout leading and trailing spaces...
trim() {
local str="$@"
shopt -s extglob
str="${str##*( )}"
str="${str%%*( )}"
shopt -u extglob
echo "$str"
}
# Read required entries...
read_required() {
while [[ -z $var ]]; do
read -p "$1" var
var=$(trim "$var")
done
echo "$var"
}
# Expand user home dir in path...
expand_home() {
local path="$@"
path=${path/\$HOME/$HOME}
path=${path/\~/$HOME}
echo "$path"
}
text_to_slug() {
echo "${@,,}" | tr -d '[:punct:]' \
| sed -e "s/ /-/g" \
-e "s/[áàãâä]/a/g" \
-e "s/[éêä]/e/g" \
-e "s/[íÍï]/i/g" \
-e "s/[óõôö]/o/g" \
-e "s/[úü]/u/g" \
-e "s/[ç]/c/g"
}
# Existent panels menu (sets var 'panel_id')...
panels_menu() {
echo ""
for p in ${panels[@]}; do
echo "$p) $l10n_panel_word $p (${anchor[${positions[$p]}]})"
done
echo ""
read -p "$l10n_choose_panel (Default: ${panels[0]}): " panel_id
[[ ! " ${panels[@]} " =~ " $panel_id " ]] && panel_id=${panels[0]}
}
# Get the single existent panel ID (sets var 'panel_id')...
single_panel() {
echo -e "\n$l10n_single_panel ${panels[0]}.\n"
panel_id=${panels[0]}
}
# Exit with status 1 if there's no panel...
no_panel() {
echo "$l10n_no_panel"
exit 1
}
# New desktop file contents (sets var 'desktop_file_contents')...
set_desktop_file() {
# Header message...
echo "$l10n_set_desktop_file"
# Required...
lname=$(read_required "$l10n_launcher_name")
# Optional, default is empty...
read -p "$l10n_launcher_comment" lcomment
lcomment=$(trim "$lcomment")
# Required...
lexec=$(read_required "$l10n_launcher_exec")
# Required...
licon=$(read_required "$l10n_launcher_icon")
# Optional, default is empty...
read -p "$l10n_launcher_path" lpath
lpath=$(trim "$lpath")
# Optional, default is false...
read -p "$l10n_launcher_terminal" lterminal
lterminal=$(trim "$lterminal")
# Optional, default is false...
read -p "$l10n_launcher_startup" lstartup
lstartup=$(trim "$lstartup")
echo ""
desktop_file_contents="\
[Desktop Entry]
Version=1.0
Type=Application
Name=$lname
Comment=$lcomment
Exec=$(expand_home $lexec)
Icon=$(expand_home $licon)
Path=$(expand_home $lpath)
Terminal=${lterminal:-false}
StartupNotify=${lstartup:-false}"
desktop_file_name="$(text_to_slug "$lname").desktop"
desktop_file_path="$HOME/.config/xfce4/panel/launcher-$next_plugin"
echo "\
$l10n_file_word: $desktop_file_name
$l10n_path_word: $desktop_file_path
$desktop_file_contents
"
}
# Panels and plugins info ------------------------------------------------------
# panels - array with panels IDs
# positions - array with panel (index) positions
# panel_plugins - array with pluign IDs for each panel (index)
# plugin_ids - array with all current plugin IDs
# next_plugin - next plugin ID available
# Existing panel IDs...
panels=($(xfconf-panel | sed 1,2d))
# Plugin IDs...
for p in ${panels[@]}; do
# Get positions...
positions[$p]=$(xfconf-panel "/panel-$p/position" | sed -n "s/p=\([^;]*\);.*/\1/p")
# Get plugin IDs by panel...
panel_plugins[$p]=$(xfconf-panel "/panel-$p/plugin-ids" 2> /dev/null | sed 1,2d)
# Get all current plugins IDs...
plugin_ids+=(${panel_plugins[$p]})
done
# Get next plugin ID available...
IFS=$'\n'
sorted=($(sort -n <<< "${plugin_ids[*]}"))
unset IFS
next_plugin=$((${sorted[@]: -1} + 1))
# Main procedures --------------------------------------------------------------
# Message or menu to display based on number of panels to get var 'panel_id'...
if [[ ${#panels[@]} -eq 1 ]]; then
single_panel
elif [[ ${#panels[@]} -gt 1 ]]; then
panels_menu
else
no_panel
fi
# Build the new launcher...
set_desktop_file
read -s -N 1 -p "Confirma a criação do arquivo acima (S/n)? " opt
if [[ ${opt,,} = "n" ]]; then
echo -e "\n\nSaindo...\n"
exit 0
else
# Create new launcher folder...
mkdir -p "$desktop_file_path"
# Create new launcher file...
echo "$desktop_file_contents" > "$desktop_file_path/$desktop_file_name"
# Add new launcher to xfconf plugins entry...
xfconf-panel -c xfce4-panel -p /plugins/plugin-${next_plugin} -t string -s "launcher" --create
# Add new desktop file to new plugin entry...
xfconf-query -c xfce4-panel -p /plugins/plugin-${next_plugin}/items -t string -s "${desktop_file_name}" -a --create
# Clear plugin IDs from selected panel...
xfconf-query -c xfce4-panel -p /panels/panel-${panel_id}/plugin-ids -rR
# Recreate plugin IDs array for selected panel...
id_list=""
if [[ ${#panel_plugins[$panel_id]} -ne 0 ]]; then
for id in ${panel_plugins[$panel_id]}; do
id_list+="-t int -s $id "
done
fi
id_list+="-t int -s $next_plugin"
xfconf-query -c xfce4-panel -p /panels/panel-$panel_id/plugin-ids -n $id_list -a
# Restart panels...
xfce4-panel -r
fi
echo -e "\nSaindo...\n"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment