You are not logged in.

#1 2008-09-28 12:08:47

m3tr0g33k
Member
From: Staffordshire, UK
Registered: 2008-05-04
Posts: 22
Website

Automatic file view by magic number?

Hi,

Has anyone got a CLI script that will check the type of a file and then open it in the correct/required viewer?

In know file mangers do this when you 'click' on a file: vi for text files, gv for postscript etc...

The specific issue is opening a hyperlink from a mindmap in freemind. The default command to opena link is to call mozilla with the filename. I want to call a script that will decide which viewer or editor to open the file in. To maintain a high level of granularity, this should be by file magic number, not by a '.extension' in the filename.

Any ideas, or shall I start writing a bash script?

___________________
mg


___
Change is inevitable; progress less so.

Offline

#2 2008-09-28 12:43:43

rine
Member
From: Germany
Registered: 2008-03-04
Posts: 217

Re: Automatic file view by magic number?

You could put something like this in your .bashrc:

myopen () {
    if [ -f $1 ]; then
        case $1 in
            *.jpg) feh $1;;
            *.avi) mplayer $1;;
            *)     echo "$1 has an unknown file type"
        esac
    else
        echo "$1 does not exist"
    fi
}

Of course you would have to fill the list once.

Offline

#3 2008-09-28 13:14:17

lucke
Member
From: Poland
Registered: 2004-11-30
Posts: 4,018

Re: Automatic file view by magic number?

Have a look at zsh-mime-setup.

Offline

#4 2008-09-28 13:51:32

ezzetabi
Member
Registered: 2006-08-27
Posts: 947

Re: Automatic file view by magic number?

or you can use `file -b -i' output.

Edit: typo

Last edited by ezzetabi (2008-09-28 15:54:10)

Offline

#5 2008-09-28 14:28:47

m3tr0g33k
Member
From: Staffordshire, UK
Registered: 2008-05-04
Posts: 22
Website

Re: Automatic file view by magic number?

Thanks, all.

A combination of these would be great.

ezzetabi: the file -b -i <file> gives the description I want to work from - not all my files have windoze type extensions to describe their contents.

I could script as the .bashrc example (thankyou rine) using file -b -i and a case for each type.

lucke: The zsh example looks just right! It might take some time for me to migrate, though.

If only there were a try-catch loop around every bash execution from the command line so that you could catch the "command not found" return from bash and try the command through a file-type handler as well.

Of course this could be done by putting a script around the login call to bash, so that the top-level shell is actually a subshell, with an exit error handler...

Thanks for the info - I shall think some more how to combine these approaches. Perhaps a patch to bash 'exec' including a file-type handler catch at the end? Hmmm patching bash sad

Last edited by m3tr0g33k (2008-09-28 14:36:00)


___
Change is inevitable; progress less so.

Offline

#6 2008-09-28 15:33:18

ezzetabi
Member
Registered: 2006-08-27
Posts: 947

Re: Automatic file view by magic number?

what about a C++ program?
You read a map from a file, key = mime, value = program to execute.
file (the package who owns /usr/bin/file) provides magic.h to detect the mime with easy.
Use magic.h's functions to detect the file type, read from the map the program to execute, execv() and you are done!

Offline

#7 2008-09-28 17:56:24

m3tr0g33k
Member
From: Staffordshire, UK
Registered: 2008-05-04
Posts: 22
Website

Re: Automatic file view by magic number?

OK then.
Here is a working (rough v0.1) bash script.
I have put this in my /usr/local/bin, so it works whoever (whatever) calls it.

Also useful sources for this work are the mailcap file and the xdg-mime utility from freedesktop.org.

#!/bin/bash
NO_ARGS=0 
OPTERROR=65

if [ $# -eq "$NO_ARGS" ]                        # Script invoked with no command-line args?
then
        echo "Usage: `basename $0` <file>"
  echo "        `basename $0` -h for help" #does nothing for now, but will use getopts for this
  exit $OPTERROR                                  # Exit and explain usage, if no argument(s) given.
fi  

shift $(($OPTIND - 1))                            #this not used, but good practice for if you reference any cl args later

#check for the presence (not validity) of a file and launch handler
if [ -f $1 ]; then
        case `file -b -i $1` in
            text/plain*)        gvim $1;;
            application/pdf*)   xpdf $1;;
#add other file mime types and handlers in here
            *)                  echo "$1 has an unknown file type";;
        esac
else
        echo "$1 does not exist"
fi

I can now put this into freemind's preferences and files attached by a hyperlink to a branch will be opened by their chosen handler application.

This is probably all my own fault for not using gnome/kde/m$win in the first place - but just one Sunday afternoon with Arch and openbox and - PROBLEM SOLVED smile)

Thanks for the pointers, guys!


___
Change is inevitable; progress less so.

Offline

Board footer

Powered by FluxBB