Nautilus – Add Context Menu Action Without Nautilus-Actions

menunautilus

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.

Best Answer

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.


Related Question