How to add submenus to context menus?

I have figured out how to make a context menu in pcmanfm-qt

in .local/share/file-manager/actions/extract.desktop

[Desktop Entry]
Type=Action
Name=Extract Files Here
Icon=archiver
Profiles=profile-zero;

[X-Action-Profile profile-zero]
SelectionCount==1
MimeTypes=application/zip;application/vnd.rar;application/x-compressed-tar;application/x-tarz;application/x-7z-compressed;application/gzip
Exec=~/Scripts/extract.sh %f
Name=Extract

And i also found a pretty great example for conditional context menu entries https://forum.slitaz.org/topic/custom-actions-in-the-pcmanfm

But I cannot for the life of me figure out how to make a context menu item that has a submenu, how do i do it?

Take a look here: https://specifications.freedesktop.org/menu-spec/menu-spec-1.0.html

Press CTRL+F and type “sub” to get to the parts about sub-menus.

1 Like

what is here the menings of btw ? when i rightclick on tar package , it tells extract or extract to
…and i can make tar from right menu … on every folder

Do you mean “Extract Files Here”? It should mean extract to the current folder or a specified folder.

However, this is just the name of the menu entry. The actual name of the command is determined by the archiver you are using. The OP doesn’t specify the name of the archiver, as they are using a script that links to their actual archiver.

1 Like

It’s just my own custom script lol, it tries to smartly extract files to a subfolder based on some conditions. I should probably rename the menu entry tbh it’s confused me a couple times.

More about the script

I like it better than the lxqt-archiver based extract here function of pcmanfm-qt, both because it gives me more control and I understand how it works, and because sometimes the built in extract function doesn’t extract to subfolders and just dumps a bunch of files into the directory where the archive is located, which I practically never want (And if I would want it, I would want it only when i explicitly request it).

Here’s the script if you’re interested.

#!/bin/sh
#


#Smartly extracts files to a single subfolder; named after archive by default, but named after the folder inside the archive if it was already arranged into a subfolder within the archive.
subfolder="$(echo $@ | cut -d '.' -f1)"
mkdir "$subfolder"
cd "$subfolder"
file="../$@"

notify-send "extracting $(basename "$@")..."

for var in "$file"
do
echo filename "$var"
if [ -f "$var" ] ; then
       case "$var" in
           *.tar.bz2)   tar xvjf "$var"    ;;
           *.tar.gz)    tar xvzf "$var"    ;;
           *.bz2)       bunzip2 "$var"     ;;
           *.rar)       unrar x "$var"     ;;
           *.gz)        gunzip "$var"      ;;
           *.tar)       tar xvf "$var"     ;;
           *.tbz2)      tar xvjf "$var"    ;;
           *.tgz)       tar xvzf "$var"    ;;
           *.zip)       7za x "$var"       ;;
           *.Z)         uncompress "$var"  ;;
           *.7z)        7z x "$var"        ;;
	   	   *.lzh)		lha x "$var"		 ;;
           *)           echo "I don't know how to extract '$var'..." ;;
       esac
   else
       echo "'$var' is not a valid file!"
   fi
done

#Ensure good folder structure
if [ $(ls -1q | wc -l) == 1 ]; then
	if mv -f ./* ../ ; then
		cd ../
		rm -r "$subfolder"
	else
		mv ./*/* ./
		mv ./*/.* ./
		rm -r "$subfolder"
	fi
fi

notify-send "$(basename "$@") has been extracted."

Anyhow I asked about this elsewhere at around the same time and got this as an answer: https://github.com/lxqt/pcmanfm-qt/wiki/custom_actions#menu-definition

So to summarize. If I want the extract files (~/.local/share/file-manager/actions/extract.desktop) action from my OP to be part of a submenu, I can do:

~/.local/share/file-manager/actions/submenu.desktop

[Desktop Entry]
Type=Menu
Name=Submenu
Icon=menu
ItemsList=extract;another-action;yet-another-action

Where the extract.desktop, another-action.desktop and yet-another-action.desktop from within ~/.local/share/file-manager/actions/ would be the submenu items.

It uses the mimetypes setting from the desktop entries it refers to to determine if it shows up or not.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.