Run linux open with

How does ‘Open with’ a custom executable in Linux work?

I have a file called foo.txt . I want to associate my own program with the mime-type .txt so that my program opens a terminal and shows the contents of foo.txt as standard output. I would prefer Ruby, but BASH scripting will also be OK. An working example: I can open an HTML file with firefox . I want to open txt files with my own executable the same way. I can’t figure out how can I actually get it working? Example 2: I can open a .txt file with Geany/Mousepad/Atom/Code etc. Let’s suppose I have made a tool just like mousepad. How should my program handle the .txt mimetype? So far I have made a small GUI program with Ruby and made it executable and tried to open foo.txt with my program (I used the Nemo file manager). I have captured arguments, and stdins in my Ruby program so it will show the Argument and STDINs if any. But my program doesn’t even show up the window if I open a .txt file with it! How am I supposed to achieve the result?

To be clear, you have right clicked on the file and selected Open With > Other Application and selected your custom application and that does not work? What happens specifically? When you use your custom application can you open files with it?

Yes. I am using a simple Ruby GUI program (with Ruby2D) The custom command is a GUI application that shows the arguments given, the base name of the script. It runs normally from the terminal, but doesn’t open when I ‘open as’ with that. Also, I have proper permissions, and I am using a single user.

What operating system are you using? You may need to change the values or import them from ~/.local/share/applications/mimeapps.list to have Nemo recognize the change.

Your Ruby GUI program is able to open up files, its Nemo not opening said files using your choice of filetype association? Is that the issue? Or is the issue that your Ruby program does not open up files? If the latter is the issue this question would be better served on Stack Overflow.

If «Opens a terminal and shows the contents of foo.txt as standard output» are all done inside your program, and your program support command line parameters like program filename , then you can simply create a desktop file for your program, and set mime association through your DE settings.

2 Answers 2

Introduction

When a file is opened with an application, the file is just passed to the application as an argument.

So when you open firefox-nightly located in /bin/ with p.html located in /home/user/ , it’s basically similar to run /bin/firefox-nightly /home/user/p.html .

Читайте также:  Linux изменить свой доступ

Creating an Executable

As mentioned in the question:

So far I have made a small GUI program with Ruby and made it executable and tried to open foo.txt with my program (I used the Nemo file manager).

Let us create a Ruby program as asked by the OP that will copy the contents of a file passed as an argument to /tmp/tempfile-# . Note that any programming language will work if it can accept command line arguments.

#!/usr/bin/env ruby # Errors ERR_NO_FILE = 2 FILE = $*[0] begin tempfile, counter = File.join(%W(/ tmp tempfile)), 0 tempfile.replace(File.join(%W(/ tmp tempfile-#))) while File.exist?(tempfile) IO.write(tempfile, IO.read(FILE)) rescue Errno::ENOENT exit!(ERR_NO_FILE) rescue Interrupt, SystemExit, SignalException exit!(0) rescue Exception abort($!.backtrace.join(?\n)) end if FILE 

And let’s call our program copycat.rb, and move it to /tmp/ directory.

We can surely run the program on a terminal like this:

A this will copy all the contents of /tmp/AFile to /tmp/tempfile-# Where # is the count in case any duplicate tempfile exists.

Creating an Application Entry

Now to open this with our program from the file manager, we need to create an Application entry. To do that, we have 2 options. The first options is:

Create a file called copycat.desktop in $HOME/.local/share/applications , with the following content:

[Desktop Entry] Version=1.0 Type=Application Name=CopyCat Comment=Copy the contents of a file to /tmp/tempfile# Exec=/tmp/copycat.rb %u Icon=edit-copy Path= Terminal=false StartupNotify=false Actions= Categories=Copy;Utility 

Don’t forget to add the %u option to the line starts with Exec.

Testing

Well, to test, let’s create a simple file called with the content ‘hello world’ or anything you want. Open your file manager, and click your secondary mouse button on the file, and select «Open With» or similar option. Because it’s GUI related, I will add some sample pictures.

  1. Nautilus, «Open With Other Application»: Nautilus,
  2. Nautilus, «View All Applications»: Nautilus,
  3. «CopyCat»: Run linux open with
  4. When done, you can see the tempfile-# created in /tmp/ enter image description here

The file manager I used here is Nautilus, but this should work with other file managers as well, just the text might differ.

The second option is to make the application available for all users. To do that, you have to move the file from $HOME/.local/share/applications/copycat.desktop to /usr/share/applications and change the ownership to root.

This is how the open with a custom executable works in Linux. A similar GUI app can be created and opened in the same way.

Источник

Adding A Custom ‘Open With’ Program In Ubuntu 20.04

This article is a quick summary of how to add a custom ‘Open With’ program that can be associated with files of a certain type in Ubuntu 20.04. In particular, we’ll review an example of how you can get files to automatically open with a AppImage executable of your choosing.

Custom 'Open With' Ubuntu

I decided to write an article on this subject because the last few times that I tried to complete this seemingly simple task, it has taken me at least a couple hours due to forgetting numerous odd details and having to piece it together again from google searches. My overall goal was to get ‘.kdenlive’ project files to open with the Kdenlive AppImage.

Читайте также:  Linux mint windows 1251

Step 1) Locate The ‘.desktop’ Directory

In order for your application to show up in the ‘Open With’ list, you need to add a ‘.desktop’ entry for it. To do this, you’ll first need to figure out which directory to put it in. Most likely, you’ll choose one of the following two folders:

  • Inside the global folder for all users at ‘/usr/share/applications/’
  • Inside a specific user’s folder at ‘/home/THE_USER/.local/share/applications/’

In my case, I’m going to use the ‘/usr/share/applications/’ directory to make this option globally accessible. I can also see that in this directory, there are already tons of files that end in ‘.desktop’, so you may be able to use them as a reference. If you’re reading this article in a far distant future where the folder has been changed, or you’re on a different system other than Ubuntu with different paths, search for a folder on your system with lots of ‘*.desktop’ entries in it.

Step 2) Add The New Desktop Entry

For the ‘.desktop’ entry, I’m going to name it ‘my_custom_thingy.desktop’, but you can name it whatever you want as long as it doesn’t clash with another existing entry and it ends with ‘.desktop’. Now, let’s add an entry:

sudo vi /usr/share/applications/my_custom_thingy.desktop

And here is what we’ll put inside of the file ‘my_custom_thingy.desktop’:

[Desktop Entry] Exec=/usr/bin/gedit %f Name=My Custom Thingy Terminal=false Type=Application 

The above example will cause files to be opened in ‘gedit’. Simply change ‘/usr/bin/gedit’ to the path of the executable that you want to open files with. The text ‘My Custom Thingy’ is whatever you want the name in the ‘Open With’ list to be. The ‘%f’ will become the one or more filenames that will be passed to the program. If you use ‘%F’, that represents only a single filename. The format strings and desktop entry names are all documented on this page title ‘Desktop Entry Specification’.

Doing the above is enough to make the item appear as an option in ‘Open With’ on Ubuntu 20.04 without refreshing/relogging or running anything else as it appears to get picked up automatically. I just tested these instructions with a test file called ‘something.abc’ that contained only the text ‘Hello World!’. Immediately after adding the .desktop file above I was able to right-click on my ‘something.abc’ file and get it to open with ‘My Custom Thingy’ and it opened in gedit.

Step 3) Setting Up A Custom MIME Type (Optional)

One disadvantage of the approach shown above is that after the first time you open your file this way, it’ll keep the ‘Open With’ option that you selected as the default option for all files of that ‘type’. If your system identified the ‘file type’ as something very general, like ‘text files’ or ‘binary files’, then every time you try to open one of those general file types, it’ll use the program you last associated with it. In order to get a better solution that’s targeted at specific file formats/extensions, you need set up a custom ‘MIME type’ Adding A Custom MIME Type & Icon In Ubuntu 20.04.

Читайте также:  Отключение запроса пароля linux

Troubleshooting

Here are some of the things that have caused the new ‘Open With’ to not show up, or otherwise caused me to waste a lot of time on this task:

  • Not realizing that I spelled ‘Exec’ wrong.
  • Forgetting to make the target program executable (this prevents it from showing up in the list).
  • Not including the ‘%f’ part which actually passes the name of the file being opened.
  • Re-logging in/restarting multiple times because I thought that was necessary, only to later find that I had a typo in the file.
  • Spending a lot of time running ‘sudo update-desktop-database’ and reading bug reports on this program (it has some weird bugs that require environment flags to be set or something? idk. ) because I thought it was necessary to get the entry to show up. For this use case, it wasn’t necessary. Maybe it was necessary in the past? Maybe it’s only necessary if MIME types are involved? Not sure.

During my google searches, I read that there is a ‘correct’ way to perform this task which makes use of this program:

I never tried it though, and nothing bad happened.

Open With Kdenlive Appimage

Now, let’s finally get to the task that I actually wanted to perform before writing this article: Setting up an ‘Open With’ option that I can use for .kdenlive project files to open them up directly using the Kdenlive AppImage. Here is the desktop entry I used:

[Desktop Entry] Exec=/home/robert/kdenlive-21.04.0-x86_64.appimage %f Name=Kdenlive 21.04 Appimage Terminal=false Type=Application 

Using the above desktop entry, I just need to add it to the required location:

sudo vi /usr/share/applications/my_custom_kdenlive_opener.desktop

and the new ‘Open With’ association is now available to use without relogging or restarting.

Open With Kdenlive Shell Script Wrapper & Logging

Ok, here is the real reason that I’m writing this article: What I actually wanted to be able to open Kdenlive automatically by clicking on project files, but I wanted to do it indirectly through a shell script. The reason being, that Kdenlive crashes sometimes, and when that happens I want to be able to go back and look at the logs to see what happened before the crash.

Here is the .desktop entry that I used for this:

[Desktop Entry] Exec=/home/robert/kdenlive_hook.sh %f Name=Kdenlive Logged Hook Terminal=false Type=Application 

And here is the ‘kdenlive_hook.sh’ wrapper script that launches kdenlive, but also sets up some log files in the /tmp directory:

#!/bin/bash cd $(dirname "$ ") env > "/tmp/kdenlive_environment_$(date).log" 2>&1 /home/robert/kdenlive-21.04.0-x86_64.appimage "$ " > "/tmp/kdenlive_output_$(date).log" 2>&1 

Once I put the above script into ‘~/kdenlive_hook.sh’, I’ll be able to log everything that happens while I’m using kdenlive, and then when it crashes, I’ll be able to submit high-quality bug reports!

Источник

Оцените статью
Adblock
detector