Open a file with a bash script from gnome

3

I wrote a bash script that processes a video file and I'd like to open videos into it without having to navigate to them in terminal.

$ ~/script.sh video.mp4 is how it's run in from terminal.

I've tried opening a video with gnome-terminal -e ~/home/user/script.sh but it doesn't seem to be capturing the filename/path. I'm using Nemo file manager.

I haven't been able to come up with anything while searching for solutions.

In case it's relevant, this is how I'm passing the filename argument to the script. (likely not the best way)

#!/bin/bash
args=("$@")
in_file=${args[0]}

Update: I've created this .desktop file.

[Desktop Entry]
Comment=
Terminal=true
Name=fixvideo
Exec=/home/user/fixvideo.sh %f
Type=Application
Icon=/usr/share/icons/gnome/48x48/apps/gnome-settings-theme.png
Encoding=UTF-8

If I drop a video file on the desktop icon, it works properly. Using "Open With" in Nemo (or Nautilus) doesn't work, though.

leetwanker

Posted 2016-02-15T04:09:22.610

Reputation: 695

Have you tried %u or %U instead? – Tom Yan – 2016-02-15T12:04:50.190

I tried %u and it also worked. I wasn't sure of the difference between a filename with path and a url so I stuck with %f for filename. I don't think my script would appreciate being thrown multiple urls so I didn't try %U. – leetwanker – 2016-02-16T19:06:23.320

@leetwanker Do you mean with %u "Open With" works or is it only the drag and drop too? – Tom Yan – 2016-02-16T21:26:34.007

%u and %f seemed to work the same for both as far as I could tell. – leetwanker – 2016-02-18T05:50:43.560

Oh so Using "Open With" in Nemo (or Nautilus) doesn't work, though. because I had to "Reset to System Defaults" in order to add this to the list of "Recommended Applications" for .mp4 files.? – Tom Yan – 2016-02-18T08:06:03.000

Open with wasn't working initially because I wasn't editing the .desktop file that gnome was using, I was editing the one I'd made on my desktop. Not being able to add the program to the recommended apps list I think was some other sort of problem with Gnome (and only with .mp4's, .mkv's worked fine) which was fixed when I used Reset to System Defaults. It allowed me to do so after that. – leetwanker – 2016-02-19T09:41:49.567

Answers

2

I think you probably want to write a .desktop file, which can be associated to video files ("open with..."), for the bash script: https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html

https://developer.gnome.org/integration-guide/stable/desktop-files.html.en:

Place this file in the /usr/share/applications directory so that it is accessible by everyone, or in ~/.local/share/applications if you only wish to make it accessible to a single user.

Edit by leetwanker: Here's the .desktop file I ended up making. I'd have preferred to keep it in my own post but apparently that's frowned upon by the mods here.

I've created this fixvideo.desktop file in ~/.local/share/applications/

[Desktop Entry]
Comment=
Terminal=true
Name=fixvideo
Exec=/home/user/fixvideo.sh %f
Type=Application
Icon=/usr/share/icons/gnome/48x48/apps/gnome-settings-theme.png
Encoding=UTF-8
Hidden=false
NoDisplay=false
Categories=AudioVideo;Player;Recorder;
MimeType=video/dv;video/mpeg;video/x-mpeg;video/msvideo;video/quicktime;video/x-anim;video/x-avi;video/x-ms-asf;video/x-ms-wmv;video/x-msvideo;video/x-nsv;video/x-flc;video/x-fli;video/x-flv;video/vnd.rn-realvideo;video/mp4;application/mp4;video/mp4v-es;video/mp2t;application/ogg;application/x-ogg;video/x-ogm+ogg;audio/x-vorbis+ogg;audio/ogg;video/ogg;application/x-matroska;audio/x-matroska;video/x-matroska;video/webm;audio/webm;audio/x-mp3;audio/x-mpeg;audio/mpeg;audio/x-wav;audio/x-mpegurl;audio/x-scpls;audio/x-m4a;audio/x-ms-asf;audio/x-ms-asx;audio/x-ms-wax;application/vnd.rn-realmedia;audio/x-real-audio;audio/x-pn-realaudio;application/x-flac;audio/x-flac;application/x-shockwave-flash;misc/ultravox;audio/vnd.rn-realaudio;audio/x-pn-aiff;audio/x-pn-au;audio/x-pn-wav;audio/x-pn-windows-acm;image/vnd.rn-realpix;audio/x-pn-realaudio-plugin;application/x-extension-mp4;audio/mp4;audio/amr;audio/amr-wb;x-content/video-vcd;x-content/video-svcd;x-content/video-dvd;x-content/audio-cdda;x-content/audio-player;application/xspf+xml;x-scheme-handler/mms;x-scheme-handler/rtmp;x-scheme-handler/rtsp;

I had to "Reset to System Defaults" in order to add this to the list of "Recommended Applications" for .mp4 files.

Tom Yan

Posted 2016-02-15T04:09:22.610

Reputation: 4 744