Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automate folder's icons assignation #189

Open
hasecilu opened this issue Oct 2, 2024 · 0 comments
Open

Automate folder's icons assignation #189

hasecilu opened this issue Oct 2, 2024 · 0 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@hasecilu
Copy link
Contributor

hasecilu commented Oct 2, 2024

A while ago I made a script meant to assign icons automatically after a new installation.

So, maybe someone (a distrohopper) is interested on having a similar script to avoid doing manually all the times.

I adapted it to use the MoreWaita place icons.

image

#!/usr/bin/env bash

# --------------------------------------------------------------------------------------------------
#
# Automate change of folder icons using MoreWaita icon theme with gio command.
#
# Using gio to set a custom icon:
#       gio set $HOME/Documents/C metadata::custom-icon \
#       	file:///usr/share/icons/MoreWaita/places/scalable/folder-c.svg
#
# Then you can confirm the change showing all the attributes with the command:
#       gio info $HOME/Documents/C
# Or showing only custom-icon attribute:
#       gio info --attributes="metadata::custom-icon" $HOME/Documents/C
# To delete the custom-icon attribute use -d flag:
#       gio set $HOME/Documents/C metadata::custom-icon -d
#
# Dependencies:
# 	gio
# 	MoreWaita
#
# Created by @hasecilu
#
# License: WTFPL        (https://en.wikipedia.org/wiki/WTFPL)
#
# --------------------------------------------------------------------------------------------------

GREEN='\033[0;32m'
YELLOW='\033[0;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

MOREWAITA_DIR=/usr/share/icons/MoreWaita/scalable/places
# MOREWAITA_DIR=$XDG_DATA_HOME/icons/MoreWaita/scalable/places

function ci() {              # Simplify command
	if [[ "$2" == "-d" ]]; then # Delete custom-icon attribute
		gio set "$1" metadata::custom-icon -d
	else # Assign value to custom-icon attribute
		gio set "$1" metadata::custom-icon file://"$2"
	fi
}

function custom-icon() {
	# First argument ($1) is the folder path, ex: /home/user/C
	# Second argument (optional) ($2) is the picture path, ex: /usr/share/icons/MoreWaita/places/scalable/folder-c,svg
	# Ex: ./custom-icon /home/user/C /usr/share/icons/MoreWaita/places/scalable/folder-c.svg
	# If there is no second argument the icon will take the basename of the path and will be
	# 	looked for in the MOREWAITA_DIR path.
	# Ex: ./custom-icon /home/user/C

	if [ "$del" -eq 1 ]; then # Delete custom-icon attribute
		[ -d "$1" ] && ci "$1" -d &&
			echo -e "${GREEN}$1 metadata::custom-icon attribute deleted${NC}"
	else
		if [ $# -eq 1 ]; then # Icon has the same name as the folder
			i=$(basename "$1")
			i=${i,,}
			icon="$MOREWAITA_DIR/folder-${i}.svg"
			[ -d "$1" ] && [ -f "$icon" ] && ci "$1" "$icon" &&
				echo -e "${GREEN}$1 metadata::custom-icon attribute updated to $i.svg${NC}"

			[ -d "$1" ] && [ ! -f "$icon" ] &&
				echo -e "${RED}$icon does not exist ${NC}"
		elif [ $# -eq 2 ]; then # Icon and folder names differ
			[ -d "$1" ] && [ -f "$2" ] && ci "$1" "$2" &&
				echo -e "${GREEN}$1 metadata::custom-icon attribute updated to $(basename "$2") ${NC}"

			[ -d "$1" ] && [ ! -f "$2" ] && echo -e "${RED}$(basename "$2") does not exist ${NC}"
		fi
		[ ! -d "$1" ] && echo -e "${YELLOW}$1 folder does not exist ${NC}"
	fi
}

# In this case we pass a "1 argument" list because the "place icon"
# uses the same name as the basename of the folder
for i in Arduino FreeCAD KiCad OpenSCAD; do custom-icon "$HOME/$i"; done

# In this case 2-element tuples are used to link folders with "place icons",
# used when folders don't match the icon name
for i in py,python games,godot; do
	IFS="," read -r folder icon <<<"$i"
	custom-icon "$HOME/$folder" "$MOREWAITA_DIR/folder-${icon}.svg"
done
@somepaulo somepaulo added the enhancement New feature or request label Oct 15, 2024
@somepaulo somepaulo added the help wanted Extra attention is needed label Nov 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants