Is it possible to have a popupmenunested menu only show if a file with a particular extension is selected?

Discuss and share scripts and script files...
Post Reply
CMD8
Posts: 1
Joined: 09 Dec 2021 03:51

Is it possible to have a popupmenunested menu only show if a file with a particular extension is selected?

Post by CMD8 »

So I'm making a custom menu and I suck at scripting. I've figured out the formatting using examples from this forum have two programs in my menu so far:

Code: Select all

       $menu = <<<MENU
PeaZip;;"<xypath>\Tools\PeaZip\peazip.exe"
 Open;openwith "<xypath>\Tools\PeaZip\peazip.exe";"<xypath>\Tools\PeaZip\peazip.exe"
 Extract to Folder;openwith """<xypath>\Tools\PeaZip\peazip.exe"" -ext2folder";"<xypath>\Tools\PeaZip\peazip.exe"
 Add to Archive;openwith """<xypath>\Tools\PeaZip\peazip.exe"" -add2archive";"<xypath>\Tools\PeaZip\peazip.exe"
Notepad++;;"<xypath>\Tools\Notepad++\notepad++.exe"
 Open with Notepad++;openwith "<xypath>\Tools\Notepad++\notepad++.exe";"<xypath>\Tools\Notepad++\notepad++.exe"
MENU;

       $command = popupnested($menu);
       if ($command != '') { load $command, , "s"; }
Now I assume it's possible, but I can't yet figure out how to make it so that a menu/submenu item only appears if a file with a particular extension is selected.

So just using these two programs as examples, the idea would be that if I, say, have a .txt file selected, the "Notepad++" menu and submenu will show, but only the "Add to Archive" submenu would show (as a .txt file doesn't need to be extracted or opened with an archiving program, but I may want to add it to an archive), but if it's, say, a .rar file, all PeaZip menu/submenu will show but none of the Notepad++ menus (as I don't need to open a .rar file in Notepad++). In essence making this a more customizable version of the POM.

I'm not really looking for someone to write a whole script for me, just a simple example of something like "Show Notepad++ menu and submenu if a .txt file is selected, else hide them" so I can get an idea of what that looks like would be appreciated.

highend
Posts: 13274
Joined: 06 Feb 2011 00:33

Re: Is it possible to have a popupmenunested menu only show if a file with a particular extension is selected?

Post by highend »

Effectively?

Sectionize your menu entries and remove sections via regex (+ all section line identifiers afterwards) before you display the menu.

ONLY use a-z, 0-9 and _ for section identifiers otherwise you have to modify the regexes yourself.
All section identifiers must be unique, none an extension of an existing one.
So don't use PeaZip and PeaZip_Open_Extract but
e.g. PeaZip_Full and PeaZip_Open_Extract instead

If you follow these rules, all
$menu = regexreplace($menu, "^[ ]*?<" . $match . "(\s|\S)+?" . $match . ">[ ]*?(\r?\n|$)");
lines can be used for all section identifier entries^^

Code: Select all

    $menu = <<<MENU
<PeaZip_Full
PeaZip||"<xypath>\Tools\PeaZip\peazip.exe"
 <PeaZip_Open_Extract
 Open|openwith "<xypath>\Tools\PeaZip\peazip.exe"|"<xypath>\Tools\PeaZip\peazip.exe"
 Extract to Folder|openwith """<xypath>\Tools\PeaZip\peazip.exe"" -ext2folder"|"<xypath>\Tools\PeaZip\peazip.exe"
 PeaZip_Open_Extract>
 Add to Archive|openwith """<xypath>\Tools\PeaZip\peazip.exe"" -add2archive"|"<xypath>\Tools\PeaZip\peazip.exe"
PeaZip_Full>
<NotepadPlusPlus
Notepad++||"<xypath>\Tools\Notepad++\notepad++.exe"
 Open with Notepad++|openwith "<xypath>\Tools\Notepad++\notepad++.exe"|"<xypath>\Tools\Notepad++\notepad++.exe"
NotepadPlusPlus>
MENU;

    // Remove unwanted entries
    $ext = gpc(<curitem>, "ext");
    if ($ext LikeI "txt") {
        $match = "PeaZip_Open_Extract";
        $menu  = regexreplace($menu, "^[ ]*?<" . $match . "(\s|\S)+?" . $match . ">[ ]*?(\r?\n|$)");

    } elseif ($ext LikeI "rar") {
        $match = "NotepadPlusPlus";
        $menu  = regexreplace($menu, "^[ ]*?<" . $match . "(\s|\S)+?" . $match . ">[ ]*?(\r?\n|$)");
    }
    // Remove all other section identifier lines
    $menu = regexreplace($menu, "(^[ ]*?<[a-z0-9_]+(\r?\n)|[ ]*?[a-z0-9_]+?>[ ]*?(\r?\n|$))");

    $command = popupnested($menu, 6:=<crlf>, 7:="|");
    if ($command != "") { load $command, , "s"; }
One of my scripts helped you out? Please donate via Paypal

klownboy
Posts: 4109
Joined: 28 Feb 2012 19:27

Re: Is it possible to have a popupmenunested menu only show if a file with a particular extension is selected?

Post by klownboy »

Another approach would be to provide multiple heredoc variables for various file types, like one video, one for archives, another for images, another for text, and still another for copy and move situations. In some ways it makes it easier to modify and also group them together. You can even filter out parts of a heredoc as you wanted to do for text files (i.e., have the full text file menu along with archive menu item from the zip menu). This would need some modifying work on your part, but you get the idea.

Code: Select all

"_Initialize";
	global $mod, $loc;
	$mod = get("shift");
	$loc = ControlatPos();
	$file = self("file");

/*** PeaZip menu ***/
	$zip = <<<ZIP
"Open with PeaZip|:openwith"<xypath>\Tools\PeaZip\peazip.exe";
"Extract to Folder|:cks";openwith """<xypath>\Tools\PeaZip\peazip.exe"" -ext2folder";
"Add to Archive|:myco";run """<xypath>\Tools\PeaZip\peazip.exe"" -add2archive";
ZIP;

/*** text file menu ***/
	$text = <<<TEXT
"Open with AkelPad|:openwith";openwith "D:\Tools\AkelPad\AkelPad.exe";
"Open with NotePad|C:\Windows\notepad.exe";openwith "C:\Windows\notepad.exe";
TEXT;

/*** image file menu 1 ***/
	$image = <<<IMAGE
"Gimp|:openwith "D:\Graphics\GIMP\bin\gimp-2.10.exe", "m"; end 1;
"Irfanview|D:\Graphics\IrfanView64\i_view64.exe";openwith """D:\Graphics\IrfanView64\i_view64.exe"" <get selecteditemspathnames |> /hide=15"; end 1;
"Image Eye|D:\Graphics\Image Eye\Image Eye.exe";openwith "D:\Graphics\Image Eye\Image Eye.exe"
IMAGE;

/*** FavoritesFolders ***/
	$FavFolders = <<<FAVS
"Home [C:\]|:home";seltab 1;
"Downloads|:favfiles";goto "G:\Downloads\";
"Wallpaper|:viewthumbs";goto "G:\Wallpaper\1920x1080\";
"Photos|:props";goto "H:\Pictures\";
"XY scripts|:views";goto "D:\Tools\XYplorer\Scripts\";
"AHK scripts|:cks";goto "D:\Tools\AutoHotkey\AHKscripts\";
FAVS;

/*** Copy/Move/New menu ***/
	$copymove = <<<COMO
"To clipboard|:clip||"
"Item path/name(s)|:cliptext||1" #101;
"Item name(s)|:cliptext||1" #102;
"Image|:clipimage||1" #115;
"Copy|:copy";#150;
"Move|:moveto";#151;
"New folder|:newfolder";#231;
"New file|:newfile";#234;
COMO;

//*** Determine XY location and file type selected
	if($loc == "T") {
			if ($mod == 0) {load "<xyscripts>\XYmenu_NESTED.xys"; end 1;}
			else {load "$FavFolders<crlf>-<crlf>$copymove", ,s; end 1;}
	}
	elseif(gettokenindex($loc, "L 1|L 2", "|", "i")) {
		if(exists("<selitem>") == 1) {
			$selext = getpathcomponent("<selitem>", "ext");
			if(gettokenindex($selext, "jpg|gif|png|ico", '|', 'i')) {
				load "$image;<crlf>-<crlf>$copymove", ,s; end 1;}   //load image and copymove menus together
			elseif(gettokenindex($selext, "ini|txt|nfo|inf|reg|ps1|vbs|cfg", '|', 'i')) {
				$zip_mod = formatlist($zip, "f", <crlf>, "*to archive*");
				load "$text<crlf>-<crlf>$zip_mod<crlf>-<crlf>$copymove", ,s; end 1;}
			elseif(("$selext" LikeI "xys") AND ($mod == 1)) {load "<selitem>"; end 1;}  //no menu - automatically run an XY script on a right click on white
			elseif(gettokenindex($selext, "rar|zip", '|', 'i')) {load "$zip<crlf>-<crlf>$copymove", , s; end 1;}  //load modified zip menu and copymove menu
			elseif("$selext" LikeI "ahk") {openwith "D:\Tools\AkelPad\AkelPad.exe"; end 1;}   //no menu - example to open an AHK file for editing
			else {load "$FavFolders<crlf>-<crlf>$copymove", ,s; end 1;}
		}
		elseif(exists("<selitem>") == 2) {
			perm $folder = "<selitem>";
			load "$FavFolders<crlf>-<crlf>$copymove", ,s; end 1;
		}

	else {
		if ($mod == 1) {
			load "<xyscripts>\XYmenu_NESTED.xys"; end 1;
		}
		else {
			load "$FavFolders<crlf>-<crlf>$copymove", ,s; end 1   //this takes care of the "nothing selected" condition
		}
	}
Windows 11, 22H2 Build 22621.1555 at 100% 2560x1440

Post Reply