Score:2

How can I create a shortcut to open a selected file with a particular Application?

it flag

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.

br flag
What is the file manager?
vanadium avatar
cn flag
Yes, depending on the desktop environment and the file manager you use.
it flag
I am using `nautilus-folder-handler.desktop`
Jacob Vlijm avatar
by flag
On right click, you could of course simply use a nautilus script.
Jacob Vlijm avatar
by flag
Aaargh, Jason, I forgot the time interval to prevent the virtual keypress (Ctrl-c) to interfere with the "real" one. Fixed, please try again.
Score:3
by flag

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.

it flag
Your 2nd method i have tried and it works. But it has the problem that i still have to right click and then there is a option called "scripts" where "open with okular" is listed. I don't want to go through these choices manually so many times. 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.
it flag
In your 2nd method will it be possible to create a shortcut for the newly created "open with okular" option that appears when i right click on the file. I think that will solve the issue.
Jacob Vlijm avatar
by flag
@Jason Liam it should work ad it is, but I am not at home. Can you open a terminal, type 'sleep 4 && /path/to/open_with_okular' then return and immediately select the pdf file?
vanadium avatar
cn flag
The option will be under a Scripts item in your right-click menu. Name script as "Open with _Ocular": o then becomes menu hotkey. Then script is easily accessible by keyboard: Shift+10 s You can also assign single hotkeys to nautilus scripts: https://askubuntu.com/questions/680016/keyboard-shortcut-for-open-terminal-nautilus-3-16/696901#696901 Note that python option that uses xdotool will not work on Wayland
Score:1
br flag

This is a two step process.

Step 1 This is exactly the same as answered by @Jacob Vlijm. That is

  1. Create (if necessary) the directory ~/.local/share/nautilus/scripts
  2. 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

  1. Create the file(if required) ~/.config/nautilus/scripts-accels

  2. 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.

Jacob Vlijm avatar
by flag
Just a guess, are you Jason? The answer was accepted before time passed to even try the answer.
Anoop Rana avatar
br flag
Sorry didn't understand what you're asking? The answer might be accepted so fast because only the 2nd part of the answer was new. The 1st part is exactly the same as yours. So the OP could have just skipped the 1st part and check if the 2nd part works.
Score:0
gr flag

Settings>Devices>Keyboard
Then locate custom shortcuts and set your desired shortcut ....
The command field should be

okular <path-to-your-pdf>/name-of-file.pdf

For example, if your pdf file is "notes.pdf" in Downloads, then the command would be

okular ~/Downloads/notes.pdf  

Hope it helps !

P.S.: Don't hesitate to ask any doubts if necessary

it flag
This is very close to what i want but not what i asked. 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.
karthik nair avatar
gr flag
I got what you want but i'm afraid if that's even possible
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.