You are not logged in.

#1 2009-12-16 03:33:55

Xyne
Moderator/TU
Registered: 2008-08-03
Posts: 5,694
Website

mimeo: open files by mimetype or file name

http://xyne.archlinux.ca/projects/mimeo

This was mostly inspired by this thread and the fact that I had already written most of the code for ObFilebrowser. This is mostly intended for use with Mimeman and PCManFM but it should work with anything that creates MIMEtype associations the same way.

*edit*
I've added URL support via an associations file which matches regexes against the arguments so it can be used for other things as well.




Feel free to share your ideas. Feedback is welcome as always.*



* Before the "but there's already an app to do this" posts start showing up, yeah, I know. They're all a bit different though and this is written to specifically work with apps that I use and is based on code that I had already written. It suits my needs (well, I don't actually use it, but if I did use an opener, this would be it) and I share it in case someone else finds it useful. If you know of a better app, use it. smile

/semi-professional re-inventor of the wheel

Last edited by Xyne (2012-09-04 16:26:24)

Offline

#2 2009-12-17 02:06:15

Heller_Barde
Member
Registered: 2008-04-01
Posts: 245

Re: mimeo: open files by mimetype or file name

well, I'll have to say: Thank you! it's a great wheel to reinvent, because the implementations there already are seem cryptic and inconsistent to me (as far as I could tell...)

Offline

#3 2009-12-18 12:09:14

Dirk Sohler
Member
From: Hamburg, Germany
Registered: 2009-10-03
Posts: 109

Re: mimeo: open files by mimetype or file name

Heller_Barde wrote:

… cryptic and inconsistent to me (as far as I could tell...)

mime handling in Linux IS cryptic and inconsistent!

Offline

#4 2010-01-25 13:57:25

tzervo
Member
From: Athens
Registered: 2009-04-03
Posts: 86

Re: mimeo: open files by mimetype or file name

I tried to use mimeo in conkeror and in mutt's mailcap and it fails to open any file. My guess is that I miss an environment variable? Or something else? Should I post more info? If I use it from the command line it works properly (eg. for a pdf file) ...

Offline

#5 2010-01-25 14:13:41

Xyne
Moderator/TU
Registered: 2008-08-03
Posts: 5,694
Website

Re: mimeo: open files by mimetype or file name

@tzervo
I don't use conkeror or mutt so I can't easily debug this. Have you actually configured mimetype associations for the files that you're trying to open? If not, you need to create them with mimeman (or pcmanfm).

I've just tested using mimeo from gmrun and that works, so I would expect it to work with other applications that invoke it.

Offline

#6 2010-01-25 15:21:58

tzervo
Member
From: Athens
Registered: 2009-04-03
Posts: 86

Re: mimeo: open files by mimetype or file name

I have checked the file /usr/share/applications/mimeinfo.cache for the default application I want to run (eg. xpdf for pdf files) and it exists. I believe that running mimeo from the console (which indeed opens my pdf with xpdf) confirms that it is ok? Is there another check I should do regarding my mime configuration? Is the output of xdg-mime/xdg-open relevant? (I have no DE, just musca with a bunch of terminals mostly)

Offline

#7 2010-01-26 10:23:56

tzervo
Member
From: Athens
Registered: 2009-04-03
Posts: 86

Re: mimeo: open files by mimetype or file name

mimeo is fine, I think the problem is with conkeror (it also has issues with rox and pcmanfm - or I am doing something wrong). By the way, excellent app (both this and mimeman), it is DE-agnostic and does just what it is expected to do. I like it!

Offline

#8 2010-01-26 12:08:19

Xyne
Moderator/TU
Registered: 2008-08-03
Posts: 5,694
Website

Re: mimeo: open files by mimetype or file name

I asked about the associations in case you were trying to open other types of files via conkeror (i.e. not pdfs, which obviously work if  you can open them directly with mimeo). The output of xdg-{mime,open} is irrelevant afaik.

I don't know how conkeror invokes applications, but you can try invoking this script to see which arguments it passes (or if it even runs at all):

#!/usr/bin/env python2
from sys import argv
print 'invoked script with ' + ', '.join(argv)

Sorry that I can't be of more help but I agree that this seems to be a conkeror problem.


Thanks for the feedback. smile

Offline

#9 2010-01-27 04:45:34

tzervo
Member
From: Athens
Registered: 2009-04-03
Posts: 86

Re: mimeo: open files by mimetype or file name

OK, I found it. The problem with both mutt and conkeror is that they delete the temporary file (ie. the file I try to open) as soon as the external command terminates. In our case the external command is mimeo and not xpdf. So once mimeo does its job, the /tmp/*.pdf file is deleted and xpdf finds nothing to open. In conkeror the workaround is either to set

delete_temporary_files_for_command = false;

in conkerorrc, so that the file is still there (although manual cleaning is required afterwards) or another way to stall mimeo after it has called the application to open the file (a clever usage of "mimeo -c"; I am still working on it). I will return if I have something new either for this or for mimeo with mutt!!

EDIT

`mimeo -c %s` works in mutt's mailcap. It forks the actual command instead of mimeo so the file is there until the command terminates. I am beginning to like it veeeery much! big_smile

Last edited by tzervo (2010-01-27 04:59:46)

Offline

#10 2010-01-27 05:11:58

Xyne
Moderator/TU
Registered: 2008-08-03
Posts: 5,694
Website

Re: mimeo: open files by mimetype or file name

I actually had to check the help message of mimeo to see what "-c" does.  lol

If conkeror works the same way, then maybe you could use this script:

#!/bin/bash
$(mimeo -c $1)

Btw, thanks for effectively documenting this. smile

Last edited by Xyne (2010-01-27 05:12:27)

Offline

#11 2010-01-27 05:27:29

tzervo
Member
From: Athens
Registered: 2009-04-03
Posts: 86

Re: mimeo: open files by mimetype or file name

The script worked perfectly. Thanks a lot. (I named it "mimeoc" !!  :-p)

Offline

#12 2010-01-27 19:52:39

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 1,959
Website

Re: mimeo: open files by mimetype or file name


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

#13 2010-01-27 20:17:22

Xyne
Moderator/TU
Registered: 2008-08-03
Posts: 5,694
Website

Re: mimeo: open files by mimetype or file name

Probably nothing. Try them both and draw your own conclusions. I wrote this to complement mimeman and obfilebrowser, and I wrote those to be compatible with mimetype handling in pcmanfm. As with most things I post, I personally find it useful and I share it in case others do (which, however few, some do).

Btw, phrasing your post the way you did is the same as if you had posted "what's wrong with Awesome?" in an Xmonad thread.

Offline

#14 2010-01-27 20:21:05

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 1,959
Website

Re: mimeo: open files by mimetype or file name

Xyne wrote:

Btw, phrasing your post the way you did is the same as if you had posted "what's wrong with Awesome?" in an Xmonad thread.

Not really. Both are well-established projects with big communities and I know the differences.
Your project is new, whereas xdg-mime is well-established. And I have no clue what would be the difference between them.


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

#15 2010-01-27 21:25:41

tzervo
Member
From: Athens
Registered: 2009-04-03
Posts: 86

Re: mimeo: open files by mimetype or file name

From what I have understood (although I am quite new to this), xdg-mime is a way for the big DE's (gnome, XFCE and KDE) to use the mime associations for their openers and their databases. If one does not use these DE's, one must "mask" his environment as one of these to be able to even use xdg-{mime,open}. Mimeo just uses mimeinfo.cache (both global and local) and can be used in any possible environment setup (according always to the freedesktop specs). Since I don't use KDE, gnome or XFCE I could not use xdg-{open,mime} (no gconf or kde-something here) whereas mimeo just read my mimeinfo.cache and worked out of the box (well, almost). I found out that this was the most KIS solution. Correct me if I understood something the wrong way...

Offline

#16 2010-01-27 22:11:06

ataraxia
Member
From: Pittsburgh
Registered: 2007-05-06
Posts: 1,537

Re: mimeo: open files by mimetype or file name

xdg-open doesn't make use of the data created by xdg-mime. If it did, there probably wouldn't be much use for mimeo, but since it doesn't, this is very valuable.

Offline

#17 2010-01-27 22:34:05

tzervo
Member
From: Athens
Registered: 2009-04-03
Posts: 86

Re: mimeo: open files by mimetype or file name

Even if it did... In my example above, I have associated xpdf to pdf files. This is registered in mimeinfo.cache. Even then, "xdg-mime query default application/pdf" gives me no output. I have looked into xdg-mime (it is a bash script) and in line 447 it has a function for checking out which DE I have. What if I don't?

I tend to think of mimeo as pcmanfm without the file manager (it just parses mimeinfo.cache and opens the file, nothing more)!!! It is good for the command line, good for conkeror (pcmanfm had issues when I used it for handling MIME) and good for mutt. Before that, I used ugly ad-hoc patches and environmental variables (like BROWSER,PDF_READER etc) to have something like centralised mime handling/opening.

@Dieter: wouldn't it also be good as a file opener for using in conjunction with uzbl? Just one more small script that joes one job well?

Last edited by tzervo (2010-01-27 22:40:56)

Offline

#18 2010-02-05 23:15:27

Acecero
Member
Registered: 2008-06-21
Posts: 1,373

Re: mimeo: open files by mimetype or file name

I'm having a problem loading mimeo manually with video/x-matroska related files and having it freeze without the process being killed.

I noticed I got this output with Ctrl-C:

Traceback (most recent call last):
  File "/usr/bin/mimeo", line 245, in <module>
    main()
  File "/usr/bin/mimeo", line 241, in main
    launch( *getCmd(args[0]) )
  File "/usr/bin/mimeo", line 125, in getCmd
    mimetype = getMimetype(fpath)
  File "/usr/bin/mimeo", line 117, in getMimetype
    output = proc.communicate()[0]
  File "/usr/lib/python2.6/subprocess.py", line 681, in communicate
    stdout = self.stdout.read()
KeyboardInterrupt

EDIT: Actually since the file I was trying to load had escaped spaces, I put quotes around it and I got this error:

error: no mimetype association for troska

Last edited by Acecero (2010-02-05 23:37:27)

Offline

#19 2010-02-06 01:30:04

Xyne
Moderator/TU
Registered: 2008-08-03
Posts: 5,694
Website

Re: mimeo: open files by mimetype or file name

I think I've fixed the error due to spaces in the name.

Use mimeman or pcmanfm to create an association for troska files and then mimeo should detect it.

Last edited by Xyne (2010-02-06 04:59:01)

Offline

#20 2010-02-06 04:57:55

Acecero
Member
Registered: 2008-06-21
Posts: 1,373

Re: mimeo: open files by mimetype or file name

The fixed update solved my problem. I also switch to pcmanfm to be safe.

Thanks Xyne.

Offline

#21 2010-02-10 21:31:00

lman
Member
From: CZ
Registered: 2007-12-18
Posts: 252

Re: mimeo: open files by mimetype or file name

I just found out about this great piece of software.
Thanks! it's great!

Offline

#22 2010-02-23 14:07:54

tzervo
Member
From: Athens
Registered: 2009-04-03
Posts: 86

Re: mimeo: open files by mimetype or file name

Is there a way to make it open multiple files (like pcmanfm?) I tried mimeo *mp3 on a bunch of mp3 files while having defined as application this simple script:

#! /bin/sh
xterm -e mplayer "$@"

The script by itself works but it seems that mimeo takes only the first file as an argument...

pcmanfm on the same example works.

Offline

#23 2010-02-23 14:57:09

Xyne
Moderator/TU
Registered: 2008-08-03
Posts: 5,694
Website

Re: mimeo: open files by mimetype or file name

@tzervo
I've updated mimeo to support multiple arguments. It will group them so that each unique command is called once with all the matching arguments. Let me know if it works as expected.

Offline

#24 2010-02-23 15:35:17

tzervo
Member
From: Athens
Registered: 2009-04-03
Posts: 86

Re: mimeo: open files by mimetype or file name

It works excellent if there are no spaces in the absolute path of the files. With spaces it fails (mimeo -c "something with spaces"...)

EDIT

Another question: how do you determine the MIME type in mimeo? From what I've seen, xdg-open uses the command file, so it is impossible for me to define new types by extension for text files in $HOME/.mime.types. (I wanted to assosiate vtk files with paraview). Is there a way/suggestion to define text MIME by extension and then bind them to mimeo? (sorry if this is slightly off-topic)

EDIT EDIT

Since I suspect that there is no standard-conforming way to discriminate between the text/* files, I will probably point mimeo to a script that opens only text files by extension. (I wonder where $HOME/.mime.types could be used though....). Again thanks for this very useful app!

Last edited by tzervo (2010-02-23 18:10:54)

Offline

#25 2010-02-23 23:11:13

Xyne
Moderator/TU
Registered: 2008-08-03
Posts: 5,694
Website

Re: mimeo: open files by mimetype or file name

The only problem that I found with spaces was that it quoted arguments when launching applications, which it shouldn't have done. I've corrected that, but I don't understand what the problem with "mimeo -c ..." is/was.

Mimeo uses Python's mimetypes module to determine mimetypes. If that fails, it falls back on "file --mime-type". If you know of a way to get the mimetype by extension, I will consider implementing it.

Offline

Board footer

Powered by FluxBB