10

I am posting this in order to provide an answer, as a way to have a simpler method than using nautilus-actions (launched with nautilus-actions-config-tool), while avoiding to write each time an entire action file from scratch.

On the other, while nautilus-actions-config-tool will not be used, nautilus-actions has to be installed for the actions to appear in the context menu.

  • Is there a common way to do this?... in the absence of nautilus?... what if the environment is KDE and it uses dolphin/konqueror as file managers... If I'm developing an installation script, Is there a simple generic way for me to tell Linux that I wish to add a context menu to the active filemanager application(nautilus/dolphin/konqueror/whatever it may have configured as active file manager) – Crystal Paladin Sep 05 '17 at 06:33
  • I think not, this looks like application specific. Dolphin and Thunar have GUI tools for this, Nautilus does not, therefore I have posted this... –  Sep 06 '17 at 07:26

1 Answers1

9

Reminder:

While with this solution the nautilus-actions will not be used in order to add the new context menu actions, it has to be installed for Nautilus to even have the actions feature at all, and for the actions to appear in the context menu.


The idea is to have a ready-made but incomplete action file that is to be edited and saved under a new name for each new context menu action. This generic file can in turn be accessed each time with a launcher file (as if it were an app) or from the context menu itself (with another specific action file previously created).

Create the folder ~/.local/share/file-manager/actions.

To create the ready-made action file, do:

gedit ~/.local/share/file-manager/actions/new-action.desktop

With these lines:

[Desktop Entry]

Type=Action
ToolbarLabel=<name-of-action>
Name=<name-of-action>
Profiles=profile-zero;

#TargetContext=false
#keep the above commented if you WANT the action to appear when you select files of the type specified by the line MymeTypes; un-comment it if you DO NOT WANT your new action to appear when you select the specified files

#TargetLocation=true
#keep the above commented if you DO NOT WANT the action to appear when right clicking an empty area; remove the comment when you WANT that.

[X-Action-Profile profile-zero]
Folders=*;
Exec=
Name=Default profile
MymeTypes=
#the above line specifies the types of files for which your new action appears when you select them (remove the line 'TargetContext=false' to achieve that)

#DO NOT SAVE directly after editing!

#USE "SAVE AS" TO SAVE YOUR NEW ACTION FILE! 

And save it.


That can be accessed directly of course, but it's nicer to have an easier way to open the ready-made file. This could be done through a launcher file:

gedit ~/.local/share/applications/Nautilus_action.desktop

With this content:

[Desktop Entry]
Type=Application
Name=New Nautilus action
Icon=nautilus
Categories=System;Settings;
Exec=sh -c 'gedit ~/.local/share/file-manager/actions/new-action.desktop'

Making it executable:

chmod +x ~/.local/share/applications/Nautilus_action.desktop

After that it can be started from a menu or app launcher:

enter image description here


The above is meant to open the generic ~/.local/share/file-manager/actions/new-action.desktop, edit, and save it with a different name.

How to do it:

  • Run the launcher "New Nautilus action"

  • When the generic file is open in gedit, edit these lines that will define your new Nautilus menu action (that is: the menu name, the action-file name, and the command to execute):

ToolbarLabel=

Name=

Exec=


TargetContext=false

  • The above should be commented (have # before it) if you want the menu action to appear when you select files (of the types specified by the line MymeTypes; see below on the ! option). If you do not want it to appear on selection (a rare case when you want an action only to appear on empty area inside folders), remove the comment (#).

TargetLocation=true

  • Considering the above line, if you do not want your new menu action to appear when you right-click an empty area, add # before it; remove the comment if you need the action to appear when you click an empty area inside folders.

  • Add to the following line the mime-types of files for which your new action appears when you select them (remove/comment the line 'TargetContext=false' to achieve that):

MymeTypes=;

Add the mime types like: inode/directory for folders, audio/* for audio, etc to specify for which selection you want the action to appear; to exclude a certain mime type use ! before it. (For example: if you use an action to convert audio to mp3 you would want it to appear for all audio excepting mp3 files; in this case it should be MymeTypes=!audio/mp3;audio/*.) Look in a file's properties (Basic/Type) in Nautilus to see its mime type.

  • DO NOT SAVE THE FILE directly or your new action will overwrite the generic action file!

  • Use 'Save as' to save the file with a DIFFERENT NAME.


  • How do I pass the filename with full path as a parameter to the `exec` property? can you update your answer... is there any `%u` stuff like the ones windows has? Say for example, I have an executable shell file and I've created a .desktop file and saved it in the aforementioned location... And the shell file receives parameters using `$1`, `$2`, ... how do I send them from the context menu action event? – Crystal Paladin Sep 05 '17 at 06:35
  • Will this solution be possible in Gnome3 and nautilus 3.10.1 and above? – Crystal Paladin Sep 05 '17 at 12:53
  • @CrystalPaladin - your first question-in-comment is beyond me. To the second I'd say yes. You should consider posting specific questions instead of comments, in this way you and others will easily find such useful information later. A comment is meant as a contribution to the answer. –  Dec 08 '17 at 23:53
  • @cipricus, this does not seem to work on Fedora 27 with GNOME Nautilus 3.26.2. I believe that the last `.desktop` file you provide should able to be used to create individual menu items (changing the `exec` to a different location of course). But I get nothing in the contextual menu after adding that. Any idea why? – Rsync Mar 30 '18 at 00:04
  • @Rsync - Be sure you **have** the `nautilus-actions` tool installed (without it Nautilus lacks the actions feature). Also, be sure you have created the `~/.local/share/file-manager/actions/new-action.desktop` file, as indicated at the first step, it is the one that needs to be opened by the `Exec` line of each of the two alternative or parallel solutions (a launcher to add a new action and an action to add a new action). That line should be present in both cases (**why do you say that the location can be changed?**). You could also try the same line without `sh -c` to see if it works. –  Mar 30 '18 at 07:57
  • @Rsync - is the launcher working? –  Mar 30 '18 at 08:24
  • @Rsync - I have radically edited my answer so that it now suggests creating **only** the launcher (and not an action also, as it initially was) for creating new actions, as that was a source of possible confusion and was more difficult to troubleshoot. - Also, I have edited the initial desktop file and the explanation regarding the lines `TargetLocation` and `TargetContext`. –  Mar 30 '18 at 08:40
  • @cipricus, testing now. I had to locate `nautilus-actions`, as it appears to be deprecated in Fedora. I found a copy that didn't work due an unresolved dependence here (https://kojipkgs.fedoraproject.org//packages/nautilus-actions/), and one that installed as `noarch` here (https://kojipkgs.fedoraproject.org//packages/nautilus-actions/3.2.4/1.fc22/noarch/). Will work on your instructions now that it is installed. – Rsync Mar 31 '18 at 15:24
  • @cipricus - No joy after installing `nautilus-actions` and following the instructions above. I don't get any contextual menu entry -- not even for 'New Nautilus action'. I can run the application via Fedora's launcher and it does create the template `.desktop` file, but no menu entry on the nautilus contextual menu. – Rsync Mar 31 '18 at 15:54
  • @Rsync - Can you [create new actions through `nautilus-actions`](https://stackoverflow.com/a/47236619/4098390) itself? If that is not working, that might be the problem. –  Apr 02 '18 at 10:27