Openbox:Pipemenus:Dirsmenu

From Openbox

(Difference between revisions)
Jump to: navigation, search
m
Line 1: Line 1:
 
[[Image:Dirsmenuv2.jpg|thumb|right]]
 
[[Image:Dirsmenuv2.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 21: Line 18:
 
  # when listing subdirectories, set the starting dir accordingly
 
  # when listing subdirectories, set the starting dir accordingly
 
  if ( "$ARGV[0]" ne "" ) { $base_dir = "$ARGV[0]"; }
 
  if ( "$ARGV[0]" ne "" ) { $base_dir = "$ARGV[0]"; }
 +
 +
# escape possible single quotes in filename when using find
 +
my $tmp = $base_dir;
 +
$base_dir =~ s/'/'"'"'/g;
 
   
 
   
 
  # list directories and links to such as submenus
 
  # list directories and links to such as submenus
Line 27: Line 28:
 
  my @files = sort split /\n/, `find -L \'$base_dir\' -maxdepth 1 ! -type d`;
 
  my @files = sort split /\n/, `find -L \'$base_dir\' -maxdepth 1 ! -type d`;
 
   
 
   
  # tested with various special characters and only &,< and " needed to be replaced by their html codes
+
$base_dir = $tmp;
  $base_dir =~ s/&/&amp;/sg;
+
undef $tmp;
  $base_dir =~ s/</&#60;/sg;
+
  $base_dir =~ s/"/&#34;/sg;
+
  # replace some special characters by their html codes
 +
  $base_dir =~ s/&/&amp;/g;
 +
$base_dir =~ s/"/&#34;/g;
 +
$base_dir =~ s/\$/&#36;/g;
 +
  $base_dir =~ s/</&#60;/g;
 +
  $base_dir =~ s/=/&#61;/g;
 +
$base_dir =~ s/>/&#62;/g;
 +
$base_dir =~ s/\\/&#92;/g;
 +
# escape special characters in bash
 +
$base_dir =~ s/(\ |&#34;|'|`|!|^|&amp|\*|\(|\)|\[|\]|\{|\}|&#..)/\\$1/g;
 
   
 
   
 
  say "<openbox_pipe_menu>";
 
  say "<openbox_pipe_menu>";
 
  say "<item label=\"Open in $browser\">";
 
  say "<item label=\"Open in $browser\">";
  say " <action name=\"Execute\"><execute>$browser \'$base_dir\' </execute></action>";
+
  say " <action name=\"Execute\"><execute>$browser $base_dir </execute></action>";
 
  say "</item>";
 
  say "</item>";
 
  say "<separator />";
 
  say "<separator />";
Line 40: Line 50:
 
  # print directories first in alphabetic order (hidden first)
 
  # print directories first in alphabetic order (hidden first)
 
   DIRS: foreach my $dir (@dirs) {
 
   DIRS: foreach my $dir (@dirs) {
       $dir =~ s/&/&amp;/sg;
+
       $dir =~ s/&/&amp;/g;
       $dir =~ s/</&#60;/sg;
+
      $dir =~ s/"/&#34;/g;
       $dir =~ s/"/&#34;/sg;
+
      $dir =~ s/\$/&#36;/g;
# exclude . from directory list
+
       $dir =~ s/</&#60;/g;
       if ( $dir eq $base_dir ) { next DIRS; }
+
       $dir =~ s/=/&#61;/g;
 +
      $dir =~ s/>/&#62;/g;
 +
       $dir =~ s/\\/&#92;/g;
 
  # 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);
       $stripped_dir =~ s/_/__/sg;
+
       $stripped_dir =~ s/_/__/g;
       say "<menu id=\"$dir\" label=\"$stripped_dir\" execute=\"$path/dirsmenu \'$dir\'\" />";
+
# escape special characters in bash
 +
      $dir =~ s/(\ |&#34;|'|`|!|^|&amp|\*|\(|\)|\[|\]|\{|\}|&#..)/\\$1/g;
 +
# exclude . from directory list
 +
      if ( "$dir" eq "$base_dir" ) { next DIRS; }
 +
       say "<menu id=\"$dir\" label=\"$stripped_dir\" execute=\"$path/dirsmenu $dir\" />";
 
  }
 
  }
 
   
 
   
 
  # print files next in alphabetic order (hidden first)
 
  # print files next in alphabetic order (hidden first)
 
  foreach my $file (@files) {
 
  foreach my $file (@files) {
     $file =~ s/&/&amp;/sg;
+
     $file =~ s/&/&amp;/g;
     $file =~ s/</&#60;/sg;
+
    $file =~ s/"/&#34;/g;
     $file =~ s/"/&#34;/sg;
+
    $file =~ s/\$/&#36;/g;
     my $stripped_file = basename("$file");
+
     $file =~ s/</&#60;/g;
     $stripped_file =~ s/_/__/sg;
+
     $file =~ s/=/&#61;/g;
 +
    $file =~ s/>/&#62;/g;
 +
    $file =~ s/\\/&#92;/g;
 +
# replace the underscore with a double underscore in the label to prevent openbox from interpreting it as a keyboard accelerator
 +
     my $stripped_file = basename($file);
 +
     $stripped_file =~ s/_/__/g;
 +
# escape special characters in bash
 +
    $file =~ s/(\ |&#34;|'|`|!|^|&amp|\*|\(|\)|\[|\]|\{|\}|&#..)/\\$1/g;
 
     say "<item label=\"$stripped_file\">";
 
     say "<item label=\"$stripped_file\">";
     say " <action name=\"Execute\"><execute>xdg-open \'$file\' </execute></action>";
+
     say " <action name=\"Execute\"><execute>xdg-open $file </execute></action>";
 
     say "</item>";
 
     say "</item>";
 
  }
 
  }
 
   
 
   
 
  say "</openbox_pipe_menu>";
 
  say "</openbox_pipe_menu>";

Revision as of 05:45, 10 June 2014

Dirsmenuv2.jpg

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]"; }

# escape possible single quotes in filename when using find
my $tmp = $base_dir;
$base_dir =~ s/'/'"'"'/g;

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

$base_dir = $tmp;
undef $tmp;

# replace some special characters by their html codes
$base_dir =~ s/&/&/g;
$base_dir =~ s/"/"/g;
$base_dir =~ s/\$/$/g;
$base_dir =~ s/</</g;
$base_dir =~ s/=/=/g;
$base_dir =~ s/>/>/g;
$base_dir =~ s/\\/\/g;
# escape special characters in bash
$base_dir =~ s/(\ |"|'|`|!|^|&amp|\*|\(|\)|\[|\]|\{|\}|&#..)/\\$1/g;

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/&/&/g;
     $dir =~ s/"/"/g;
     $dir =~ s/\$/$/g;
     $dir =~ s/</</g;
     $dir =~ s/=/=/g;
     $dir =~ s/>/>/g;
     $dir =~ s/\\/\/g;
# 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/_/__/g;
# escape special characters in bash
     $dir =~ s/(\ |"|'|`|!|^|&amp|\*|\(|\)|\[|\]|\{|\}|&#..)/\\$1/g;
# exclude . from directory list
     if ( "$dir" eq "$base_dir" ) { next DIRS; }
     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/&/&/g;
    $file =~ s/"/"/g;
    $file =~ s/\$/$/g;
    $file =~ s/</</g;
    $file =~ s/=/=/g;
    $file =~ s/>/>/g;
    $file =~ s/\\/\/g;
# replace the underscore with a double underscore in the label to prevent openbox from interpreting it as a keyboard accelerator
    my $stripped_file = basename($file);
    $stripped_file =~ s/_/__/g;
# escape special characters in bash
    $file =~ s/(\ |"|'|`|!|^|&amp|\*|\(|\)|\[|\]|\{|\}|&#..)/\\$1/g;
    say "<item label=\"$stripped_file\">";
    say " <action name=\"Execute\"><execute>xdg-open $file </execute></action>";
    say "</item>";
}

say "</openbox_pipe_menu>";