How can I create a shortcut to open a selected file with a particular Application?
I am trying to find a way to create a shortcut for opening a pdf file with a particular application. This is what i do currently(manually):
Step 1. I have a pdf file say MyBook.pdf. I right click on this pdf so that a popup window appears with many options like:
Cut
Paste
Copy
Open with other application
Move to trash
Step 2. I chose/click Open with other application
from the above menu and a new popup menu opens up with different options like:
Document Viewer
Google Chrome
Text Editor
Sublime Text
Okular
Step 3. I chose Okular
from the above menu and the pdf opens up using Okular.
What i want is that these 3 manual steps should be encapsulated in keyboard shortcut like Ctrl + O + P
Is this possible in Ubuntu 18.04 ?
PS: I am using nautilus-folder-handler.desktop on Ubuntu 18.04
Summary
I want to be able to select any arbitrary pdf by single left click and then press a custom shortcut key that will open that selected pdf using a particular application such as okular.
I noticed that there is options like Ctrl+C for copying Ctrl+X for moving, Ctrl+I for checking the properties of a file just by a single left click and then pressing the corresponding shortcut. I want to do exactly this. That is the user left clicks one time on a file and then press the shortcut which will open the file with a given application.
Solution 1:
1. A shortcut
You can use a selected file as argument to run a script (from this answer):
Setup
- run
sudo apt install python3-pyperclip xdotool
to install dependencies. - Copy the script below into an empty file, save it as
open_with_okular
, and make it executable.
#!/usr/bin/env python3
import subprocess
import pyperclip
import time
time.sleep(1)
subprocess.call(["xdotool", "key", "Control_L+c"])
subject = pyperclip.paste()
subprocess.Popen(["okular", subject])
- Create a shortcut to run the script. It will copy the (path + name of) the file, feed it to the script as argument.
2. Using nautilus scripts
Not literally what you are asking, but the easiest and cleanest is to use a nautilus script.
Since you are using nautilus, you can add a custom action:
-
Create (if necessary) the directory
~/.local/share/nautilus/scripts
-
Create a tiny script in the directory:
#!/bin/bash okular "$1"
call it
open with okular
or something, and make it executable.
Now opening the file is directly possible from rightclick -> Open with Okular.
The file will be passed as the argument to the script.
Solution 2:
This is a two step process.
Step 1 This is exactly the same as answered by @Jacob Vlijm. That is
- Create (if necessary) the directory
~/.local/share/nautilus/scripts
- Create a file with executable permission and name it whatever you like. For example, create a file named
openwithokular
and put the following content inside it:
#!/bin/bash
okular "$1"
The result of step 1 is that if you right click on any file now then a script menu option will appear which will contain the option openwithokular
and when you click on the openwithokular
option the file will open with okular.
Step 2
-
Create the file(if required)
~/.config/nautilus/scripts-accels
-
Add the following content inside it and save it:
F4 openwithokular
Note: Make sure to restart your pc otherwise it won't work.
The result of step 2 is that now there will be a keyboard shortcut associated with the menu option openwithokular
inside the scripts
menu option obtained from step 1. And this is what we wanted. Now you can just left-click on any file and press the shortcut you have set and the file will open with okular(or any app that you have set). This method is tested to work on Ubuntu 18.04 and Ubuntu 20.04.