A .desktop file scrubber and launcher for dmenu

Hello all

I have been using dmenu lately and one of the main things I was missing from rofi, was .desktop file launching. So I wrote this, it works quite nice on my end and does the job (it’s a touch slow, might work on that).

Copy to: /usr/bin/dmenu_drun

#!/usr/bin/env bash

# parses known locations for .desktop files
# written by Nathaniel Maia, June 2018

# associative array to store paths as basename values
declare -A PATHS

# check we have required software
if ! hash awk find grep dmenu &>/dev/null; then
    echo -e "ERROR: This script requires: awk find grep dmenu" && exit 1
fi

main() {
    # base file name storage
    local names=""
    while read -r f; do
        n="$(basename "$f" | awk -F'.' '{print $1}')"
        names="$names
$n"
        PATHS["$n"]="$f"
    done <<< "$(find -L /usr/share/applications/ -type f -name '*.desktop' | sort -r)"

    local ans="$(dmenu "$@" <<< "$names")"
    [[ $? != 1 && -n $ans ]] && run "$ans" || return 1
}

run() {
    local cmd
    cmd="$(grep '^Exec' "${PATHS["$1"]}")"
    cmd="${cmd/Exec=/}"
    cmd="${cmd/ %*/}"
    echo -e "$cmd"
    { [[ $cmd ]] && "$cmd" || return 1; }
    if [[ $? != 0 ]]; then
        echo "ERROR: Something went wrong when trying to run: $cmd"
        return 1
    fi
    return 0
}

main "$@" &

exit 0
2 Likes

Good stuff man. Cheers for sharing.

I need to try dmenu sometime soon.

1 Like

Jesus you’re fast, couldn’t even fix my title in time before you responded, thanks though mate :smiley:

1 Like

I am poised like a tiger.

2 Likes

Well yes that … and @Dobbie03 also lives in the future-time.

He pretty much knew I was going to type this before I even woke up.

This.

You all know it is true.