0

When a file is double clicked in a file manager it is automatically opened using the application associated with the type of the file.

How can I do the same from code. I am open to any programming language. I have a little experience with Python and C.

My target operating systems are Ubuntu Linux and OSX. However, ideally I would like to find a solution that works with Windows, OSX, and Ubuntu linux.

  • This isn't a simple question unfortunately. What program do you use to open a `.pdf`, for example? There are hundreds if not thousands of `.pdf` readers out there, and multiple ways it could be opened on your comnputer. One feasible way of doing this would be hardcoding the application with which to open certain files, but even that could result in a list that's hundreds of entries long – touch my body Jun 24 '16 at 17:19
  • I think you need two things: 1. Check the MIME type of the file (more info [here](http://stackoverflow.com/questions/43580/how-to-find-the-mime-type-of-a-file-in-python), and 2. Know which application you'll use to open the file (I think this could be possible, but I don't know how) – Barranka Jun 24 '16 at 17:19
  • 4
    See [this question](http://stackoverflow.com/questions/434597/open-document-with-default-application-in-python) – solarc Jun 24 '16 at 17:20
  • You don't to worry about the file type, somethong like `xdg-open` will do that for you on linux – Padraic Cunningham Jun 24 '16 at 17:25
  • 1
    "*The purpose of the program is so users do not have to go clicking through folders to find files. Instead they can type in the name in the program i want to write and the program will open the file. This way the user dose not need to know where the file is just how to open it.*" - users who don't want to learn general purpose tools like file manager search, or find in the CLI, will love learning this tool! – TessellatingHeckler Jun 24 '16 at 17:29

1 Answers1

2

For MAC os x

In your terminal in os x you can use the command open to open files with the default application. In python you can use the os module to immitate shell commands. So you should try this code:

os.system("open foo.doc")

Or if you want to force a window to open when there already is an instance of that window:

os.system("open -n foo.doc")

os.system("command") just executes a command from the command line. And open opens a file with its default application.

For linux

use xdg-open. For examples refer to this article.

Community
  • 1
  • 1
Tristan
  • 1,633
  • 16
  • 29
  • Could the one that gave me a downvote please explain why. – Tristan Jun 24 '16 at 17:24
  • I think it might be because `open` in your case isn't doing what the author wants to do. I think the author wants to launch an application but after reading the `open` man page it doesn't sound like it does that – touch my body Jun 24 '16 at 17:27
  • 1
    There's no `open` command on my Windows, but just `os.system(file)` does find the file's default program and opens it. So this answer is correct for my Windows :-). Except OP is on *nix... – handle Jun 24 '16 at 17:27
  • http://apple.stackexchange.com/questions/60835/how-can-i-open-a-file-in-its-default-application-from-the-command-line-in-os-x – Tristan Jun 24 '16 at 17:28
  • Also the OP said `my target operating systems are linux and mac` so Windows compatibility shouldn't be an issue. I upvoted you just because I dislike elitist downvoters – touch my body Jun 24 '16 at 17:29
  • @handle, that will not work at all on linux – Padraic Cunningham Jun 24 '16 at 17:29
  • @PadraicCunningham OK, I didn't say so. Note solarc's comment to the question for an in-depth answer (duplicate). – handle Jun 24 '16 at 17:32
  • 1
    `os.system` for windows, `open` for osx and `xdg-open` for linux (or `gvfs-open` and `gnome-open` for older, Gnome based, linux desktops). – solarc Jun 24 '16 at 17:32
  • Another possible reason for the downvote is that you've answered a question that sure looks like a duplicate. – Wayne Werner Jun 24 '16 at 17:45
  • Thank you for all the help. The os.system code worked on my mac. I am still new to writing code and learning python. This was a big help. Any good books or sites for learning python. My local library has a few books but the books only have python 2. Thanks again. – William Welch Jun 30 '16 at 15:20