Share your file-opener script - Programming On Unix

Users browsing this thread: 1 Guest(s)
seninha
Long time nixers
Most of us have an handy script for opening files and/or links given as arguments. This is, sometimes, one of our most used scripts. Use this thread to share your file-opener or link-opener scripts and steal ideas from others.

Here is mine, I call it xopen.

Code:
#!/bin/sh
# xopen: open file with proper command; use file in $MIMEFILE as rules file
# this file in public domain

usage() {
    echo "usage: xopen [-c mimefile] filename" >&2
    exit 1
}

# get mimetype of a file using the file(1) utility
getfilemime() {
    file -ib "$@" | cut -d';' -f1
}

# get lowercase extension of a file
getextension() {
    echo "${1##*.}" | tr '[:upper:]' '[:lower:]'
}

# get mimetype of a url
geturlmime() {
    protocol=$(printf "%s" "$@" | sed 's/^\([a-zA-Z]\+\):.*/\1/')

    case "$protocol" in
    http|https)
        echo text/html
        ;;
    magnet)
        echo application/x-bittorrent
        ;;
    irc)
        echo x-scheme-handler/irc
        ;;
    esac

    unset protocol
}

# get command to open file or url
getcmd() {
    while read -r line
    do
        line=${line%%#*}                # remove comments
        [ -z "$line" ] && continue      # ignore blank lines

        pattern="$(echo $line | cut -s -d: -f1 | sed -E 's/[     ]*$//; s/\|/ /g')"
        cmd="$(echo $line | cut -s -d: -f2 | sed -E 's/^[     ]*//')"
        cmd="$(eval echo $cmd)"

        # using case for matching patterns to avoid forking
        for i in $pattern
        do
            case $1 in
            $i) echo "$cmd" && return ;;
            esac
        done
    done < "$MIMEFILE"
}

# execute command $1 to open file or url $2
xopen() {
    trap "" 1 15
    echo xopen: opening $search with $1 >&2
    exec $1 "${2}" &
}

while getopts 'c:' c
do
    case "$c" in
    c)
        MIMEFILE="$OPTARG"
        ;;
    *)
        usage
        ;;
    esac
done
shift $((OPTIND -1))

[ "$#" -ne 1 ] && usage

# test whether MIMEFILE is not set
[ -z "$MIMEFILE" ] && echo 'xopen: $MIMEFILE is not set' >&2 && exit 1

# get mimetype and file extension
if [ -e "$@" ];
then            # it's a file
    mime=$(getfilemime "$@")
    extension=$(getextension "$@")
else            # it's a url
    mime=$(geturlmime "$@")
fi
[ -z "$extension$mime" ] && echo "xopen: unknown file or url type" >&2 && exit 1

# get command to open file or url
cmd=$(getcmd "$extension")              # try first to open by file extension
[ -z "$cmd" ] && cmd=$(getcmd "$mime")  # then try to open by mimetype
[ -z "$cmd" ] && echo "xopen: cannot find an command to open $@ ($mime)" >&2 && exit 1

# open file or url with $cmd
xopen "$cmd" "$@"

It gets its configuration from a file specified in the environment variable $MIMEFILE (or after the -c option). Each line in this file is composed by a mimetype or a file extension followed by a colon, followed by the command that opens a file with that mimetype or extension. Here is my $MIMEFILE:

Code:
# web
text/html:  palemoon

# text
text/*:                     $TERMCMD -e $EDITOR
application/x-empty:        $TERMCMD -e $EDITOR
application/x-shellscript:  $TERMCMD -e $EDITOR
inode/x-empty:              $TERMCMD -e $EDITOR
inode/directory:            $TERMCMD -e fm

# Docs
application/pdf:        zathura
application/postscript: zathura
application/epub*:      zathura
image/*djvu:            zathura

# media
application/octet-stream: mpv
video/*:                  mpv
audio/*:                  mpv

# image
image/*:                  sxiv

# Man pages
0|1|2|3|4|5|6|7: $TERMCMD -e man

Now share yours!


Messages In This Thread
Share your file-opener script - by seninha - 30-05-2020, 07:40 PM
RE: Share your file/link-opener script - by z3bra - 31-05-2020, 03:55 AM
RE: Share your file/link-opener script - by venam - 31-05-2020, 04:12 AM
RE: Share your file-opener script - by jkl - 10-02-2021, 03:40 PM
RE: Share your file-opener script - by neeasade - 13-02-2021, 12:16 PM