Skip to content

Instantly share code, notes, and snippets.

@andrewjbennett
Created August 14, 2018 07:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewjbennett/338f7a1fc14d527c7304e9d848c810c6 to your computer and use it in GitHub Desktop.
Save andrewjbennett/338f7a1fc14d527c7304e9d848c810c6 to your computer and use it in GitHub Desktop.
Set up some sensible default applications for an XFCE4 desktop. Intended for new programmers working on the CSE lab systems.
#!/bin/sh
# Set up some sensible defaults:
# firefox - web browser
# Thunar - file manager
# xfce-terminal - terminal
# gedit - C source files
# eog - bitmaps
# Relevant files where config settings are stored
XFCE_HELPERS="$HOME/.config/xfce4/helpers.rc"
CONFIG_MIMEAPPS="$HOME/.config/mimeapps.list"
# What applications to use
WEB_BROWSER="firefox"
FILE_MANAGER="Thunar"
TERMINAL_EMULATOR="Terminal"
C_SOURCE_CODE="text/x-csrc=gedit.desktop"
IMAGE_BITMAP="image/bmp=eog.desktop"
echo "Setting up default applications (if they aren't already set)"
echo "-----------------------------------------------------------"
# usage:
# add_default_application text/x-csrc=gedit.desktop
add_default_application() {
APP_TYPE=$(echo $1 | cut -d'=' -f1)
APP_NAME=$(echo $1 | cut -d'=' -f2)
if grep -q $APP_TYPE $CONFIG_MIMEAPPS; then
echo -n "Not setting default for $APP_TYPE to $APP_NAME, already set to "
echo "$(grep $APP_TYPE $CONFIG_MIMEAPPS | cut -d'=' -f2 | head -1)"
else
sed -i.bak "/\[Default Applications\]/a $1" $CONFIG_MIMEAPPS &&
sed -i.bak "/\[Added Associations\]/a $1" $CONFIG_MIMEAPPS &&
echo "Setting default for $APP_TYPE to $APP_NAME" &&
rm $CONFIG_MIMEAPPS.bak
fi
}
# Creates a blank mimeapps config file, to add settings to.
create_blank_mimeapps_config() {
echo "Creating $CONFIG_MIMEAPPS"
touch $CONFIG_MIMEAPPS
cat <<EOF > $CONFIG_MIMEAPPS
[Default Applications]
[Added Associations]
EOF
}
# usage:
# set_defaults_to WebBrowser firefox "web browser"
set_defaults_to() {
APP_TYPE=$1
APP_NAME=$2
DESCRIPTION=$3
if grep -q $APP_TYPE $XFCE_HELPERS; then
echo -n "Not setting default $DESCRIPTION to $APP_NAME, already set to "
echo "$(grep $APP_TYPE $XFCE_HELPERS | cut -d'=' -f2)"
else
echo "Setting default $DESCRIPTION to $APP_NAME"
echo "$APP_TYPE=$APP_NAME" >> $XFCE_HELPERS
fi
}
#####################
## XFCE helpers.rc ##
#####################
# If the directory doesn't exist, this isn't xfce...
if [ ! -d $(dirname $XFCE_HELPERS) ]; then
echo "Are you running xfce4?" && exit 1
fi
touch $XFCE_HELPERS 2>/dev/null
set_defaults_to WebBrowser firefox "web browser"
set_defaults_to FileManager Thunar "file manager"
set_defaults_to TerminalEmulator Terminal "terminal emulator"
#####################
## mimeapps.list ##
#####################
if [ ! -f $CONFIG_MIMEAPPS ]; then
create_blank_mimeapps_config
fi
# .c source files -- gedit
add_default_application "$C_SOURCE_CODE"
# .bmp bitmap images -- eog
add_default_application "$IMAGE_BITMAP"
echo "-----------------------------------------------------------"
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment