Openbox:Pipemenus:Dirsmenu

From Openbox

(Difference between revisions)
Jump to: navigation, search
m
m
Line 1: Line 1:
 
[[Image:Dirsmenu.jpg|thumb|right]]
 
[[Image:Dirsmenu.jpg|thumb|right]]
 +
 +
 +
!! More minor bug fixes to follow !!
  
 
Another pipe menu for recursive directory listing. I tried to keep the code as simple and short as possible. It uses ''find'' to get the directories and the other files in the current directory and lists them in alphabetic order while making each directory a submenu. It escapes special characters and prints the underscore correctly (unlike ''obbrowser'').
 
Another pipe menu for recursive directory listing. I tried to keep the code as simple and short as possible. It uses ''find'' to get the directories and the other files in the current directory and lists them in alphabetic order while making each directory a submenu. It escapes special characters and prints the underscore correctly (unlike ''obbrowser'').
Line 23: Line 26:
 
  # include all other types in the file list (regular, block, character, etc)
 
  # include all other types in the file list (regular, block, character, etc)
 
  my @files = sort split /\n/, `find -L \'$base_dir\' -maxdepth 1 ! -type d -and ! -type l`;
 
  my @files = sort split /\n/, `find -L \'$base_dir\' -maxdepth 1 ! -type d -and ! -type l`;
 +
 +
# tested with various special characters and only &,< and " needed to be replaced by their html codes
 +
$base_dir =~ s/&/&amp;/sg;
 +
$base_dir =~ s/</&#60;/sg;
 +
$base_dir =~ s/"/&#34;/sg;
 
   
 
   
 
  say "<openbox_pipe_menu>";
 
  say "<openbox_pipe_menu>";
Line 31: Line 39:
 
   
 
   
 
  # print directories first in alphabetic order (hidden first)
 
  # print directories first in alphabetic order (hidden first)
# also exclude . from directory list
 
 
   DIRS: foreach my $dir (@dirs) {
 
   DIRS: foreach my $dir (@dirs) {
      if ( $dir eq $base_dir ) { next DIRS; }
 
# tested with various special characters and only &,< and " needed to be replaced by their html codes
 
 
       $dir =~ s/&/&amp;/sg;
 
       $dir =~ s/&/&amp;/sg;
 
       $dir =~ s/</&#60;/sg;
 
       $dir =~ s/</&#60;/sg;
 
       $dir =~ s/"/&#34;/sg;
 
       $dir =~ s/"/&#34;/sg;
 +
# exclude . from directory list
 +
      if ( $dir eq $base_dir ) { next DIRS; }
 
  # replace the underscore with a double underscore in the label to prevent openbox from interpreting it as a keyboard accelerator
 
  # replace the underscore with a double underscore in the label to prevent openbox from interpreting it as a keyboard accelerator
 
       my $stripped_dir = basename($dir);
 
       my $stripped_dir = basename($dir);

Revision as of 19:48, 9 June 2014

Dirsmenu.jpg


!! More minor bug fixes to follow !!

Another pipe menu for recursive directory listing. I tried to keep the code as simple and short as possible. It uses find to get the directories and the other files in the current directory and lists them in alphabetic order while making each directory a submenu. It escapes special characters and prints the underscore correctly (unlike obbrowser).

dirsmenu:

#!/usr/bin/perl
# Openbox menu to recursively list directories and files.
use File::Basename;
sub say {print @_, "\n"}

# set starting directory - this changes when the script is called with an argument (used for listing subdirectories)
my $base_dir = '/';
# path to this script (needed as it calls itself to list subdirectories)
my $path = "$ENV{HOME}/obmenus";
# your file browser
my $browser = 'Thunar';

# when listing subdirectories, set the starting dir accordingly
if ( "$ARGV[0]" ne "" ) { $base_dir = "$ARGV[0]"; }

# list directories and links as submenus
my @dirs = sort split /\n/, `find -L \'$base_dir\' -maxdepth 1 -type d -or -type l`;
# include all other types in the file list (regular, block, character, etc)
my @files = sort split /\n/, `find -L \'$base_dir\' -maxdepth 1 ! -type d -and ! -type l`;

# tested with various special characters and only &,< and " needed to be replaced by their html codes
$base_dir =~ s/&/&/sg;
$base_dir =~ s/</</sg;
$base_dir =~ s/"/"/sg;

say "<openbox_pipe_menu>";
say "<item label=\"Open in $browser\">";
say " <action name=\"Execute\"><execute>$browser \'$base_dir\' </execute></action>";
say "</item>";
say "<separator />";

# print directories first in alphabetic order (hidden first)
 DIRS: foreach my $dir (@dirs) {
     $dir =~ s/&/&/sg;
     $dir =~ s/</</sg;
     $dir =~ s/"/"/sg;
# exclude . from directory list
     if ( $dir eq $base_dir ) { next DIRS; }
# replace the underscore with a double underscore in the label to prevent openbox from interpreting it as a keyboard accelerator
     my $stripped_dir = basename($dir);
     $stripped_dir =~ s/_/__/sg;
     say "<menu id=\"$dir\" label=\"$stripped_dir\" execute=\"$path/dirsmenu \'$dir\'\" />";
}

# print files next in alphabetic order (hidden first)
foreach my $file (@files) {
    $file =~ s/&/&/sg;
    $file =~ s/</</sg;
    $file =~ s/"/"/sg;
    my $stripped_file = basename("$file");
    $stripped_file =~ s/_/__/sg;
    say "<item label=\"$stripped_file\">";
    say " <action name=\"Execute\"><execute>xdg-open \'$file\' </execute></action>";
    say "</item>";
}

say "</openbox_pipe_menu>";